Escolar Documentos
Profissional Documentos
Cultura Documentos
html
http://www.deepinopensource.com/informatica-tutorialsbeginners/
Perfromance Tuning in SQL Server 2008
Query Optimization
1.
2.
3.
Remove * from SELECT and use columns which are only necessary in code
Remove any unnecessary joins from table
Always use WHERE Clause in SELECT Queries while we dont need all the rows to be
returned.
4. Avoid SELECT * FROM OrderTable WHERE LOWER(UserName)='telsa' , because case
insensitive.
5. The operators shown below are in their decreasing order of their performance.
=
>,>=,<, <=
LIKE
<>
ie, better to go for '=' rather than 'LIKE' or '<>'
6.
NOT IN, then this is going to offer poor performance, this can be avoided by using
EXISTS or NOT EXISTS.
7. When there is a choice to use IN or EXIST, we should go with EXIST clause for better
performance.
8. SELECT * FROM OrderTable WHERE Status = 1 AND OrderID IN (222,444,111,555)
Takes more time than
SELECT * FROM OrderTable with (INDEX=IX_OrderID) WHERE Status = 1 AND OrderID IN
(222,444,111,555 6)
9. While there is case to use IN or BETWEEN clauses in the query, it is always advisable to
use BETWEEN for better result.
10. Always avoid the use of SUBSTRING function in the query.
11. Provide the least likely true expressions first in the AND. Because if the first condition is
false the result also false, ie it never want to check the second condition of AND
operation .
12. It is sometimes better to combine queries using UNION ALL instead of using many OR
clauses.
13. SELECT * FROM CustomerTable WHERE City = 'Wichita' OPTION(FAST n). In some
scenario's we have to display only 10-15 result set in the web page . But in database it
having more than Ten Thousand row. In this time the data base will give the 'n' result set
very fast, and rest of the data will update soon.
14. Avoid Expensive Operators Such as NOT LIKE
15. WITH NOLOCK and WITH READUNCOMMITTED
16. Avoid Long-Running Transactions. it has to do by the sys admin.
17. Use Set NOCOUNT ON in stored procedures.
18. You can rewrite a subquery to use JOIN and achieve better performance.
19. Performance increase by adding a unique key for table. (it can index the table by unique
key)
GROUP BY DeptID.
[adsenseyu1]
4. Write an SQL Query find number of employees according to
gender whose DOB is between 01/01/1960 to 31/12/1975.
SELECT COUNT(*), sex from Employees
31/12/1975 GROUP BY sex
WHERE
Employees WHERE
Salary>=10000;
7. Select all record from emp table where deptno =10 or 40.
select * from emp where deptno=30 or deptno=10;
9.How can I retrive all records of emp1 those should not present
in emp2?
Select * from emp) Minus (Select * from emp1
10.How to fetch only common records from two tables emp and
emp1?
(Select * from emp) Intersect (Select * from emp1)