SELECT * FROM Table WHERE LOWER(Name)='India'
Instead of writing it like the below
SELECT * FROM Table WHERE Name='India'
Of course both the queries does the same work but 2nd one is better and retrieves rows more speedy than the first query. Because Sql Server is not case sensitive
Tip 3: While running a query, the operators used with the WHERE clause directly affect the performance. The operators shown below are in their decreasing order of their performance.
Tip 4 : When we are writing queries containing NOT IN, then this is going to offer poor performance as the optimizer need to use nested tables scan to perform this activity. This can be avoided by using EXISTS or NOT EXISTS.
When there is a choice to use IN or EXIST, we should go with EXIST clause for better performance.
Tip 5: It is always best practice to use the Index seek while the columns are covered by an index, this will force the Query Optimizer to use the index while using IN or OR clauses as a part of our WHERE clause.
SELECT * FROM Table
WHERE Status = 1 AND ID IN (406,530,956)
Takes more time than
SELECT * FROM Table (INDEX=IX_ID)
WHERE Status = 1 AND ID IN (406,530,956) |
Tip 6: While we use IN, in the sql query it better to use one or more leading characters in the clause instead of using the wildcard character at the starting.
SELECT * FROM Table WHERE Name LIKE 'm%'
SELECT * FROM Table WHERE Name LIKE '%m'
In the first query the Query optimizer is having the ability to use an index to perform the query and there by reducing the load on sql server. But in the second query, no suitable index can be created while running the query.
Tip 7: While there is case to use IN or BETWEEN clause in the query, it is always advisable to use BETWEEN for better result.
SELECT * FROM Table
WHERE ID BETWEEN (5000 AND 5005)
Performs better than
SELECT * FROM Table
WHERE ID IN (5000,5001,5002,5003,5004,5005)
Tip 8: Always avoid the use of SUBSTRING function in the query.
SELECT * FROM Table WHERE Name LIKE 'n%'
Is much better than writing
SELECT * FROM Table WHERE SUBSTRING(Name,1,1)='n'
Tip 9 : The queries having WHERE clause connected by AND operators are evaluated from left to right in the order they are written. So certain things should be taken care of like
Tip 10: Its sometimes better to combine queries using UNION ALL instead of using many OR clauses.
SELECT ID, FirstName, LastName FROM Table
WHERE City = 'Wichita' or ZIP = '67201' or State= 'Kansas'
The above query to use and index, it is required to have indexes on all the 3 columns.
The same query can be written as
SELECT ID, FirstName, LastName FROM Table WHERE City = 'Wichita'
UNION ALL
SELECT ID, FirstName, LastName FROM Table WHERE ZIP = '67201'
UNION ALL
SELECT ID, FirstName, LastName FROM Table WHERE State= 'Kansas'
Both the queries will provide same results but if there is only an index on City and no indexes on the zip or state, then the first query will not use the index and a table scan is performed. But the 2nd one will use the index as the part of the query.
Tip 11: While the select statement contains a HAVING clause, its better to make the WHERE clause to do most of the works (removing the undesired rows) for the Query instead of letting the HAVING clause to do the works.
e.g. in a SELECT statement with GROUP BY and HAVING clause, things happens like first WHERE clause will select appropriate rows then GROUP BY divide them to group of rows and finally the HAVING clause have less works to perform, which will boost the performance.
Tip 12: Let’s take 2 situations
We can use a hint like
SELECT * FROM Table WHERE City = 'Wichita' OPTION(FAST n)
where n = number of rows that we want to display as fast as possible. This hint helps to return the specified number of rows as fast as possible without bothering about the time taken by the overall query.
-------------------------------------------------------------------------------------------------------------------------- SQL Tuning/SQL Optimization Techniques:
1) The sql query becomes faster if you use the actual columns names in SELECT statement instead of than '*'.
For Example: Write the query as
SELECT id, first_name, last_name, age, subject FROM student_details;
Instead of:
SELECT * FROM student_details;
2) HAVING clause is used to filter the rows after all the rows are selected. It is just like a filter. Do not use HAVING clause for any other purposes.
For Example: Write the query as SELECT subject, count(subject)
Instead of:
SELECT subject, count(subject)
3) Sometimes you may have more than one subqueries in your main query. Try to minimize the number of subquery block in your query.
For Example: Write the query as SELECT name
Instead of:
SELECT name
4) Use operator EXISTS, IN and table joins appropriately in your query.
a) Usually IN has the slowest performance. b) IN is efficient when most of the filter criteria is in the sub-query. c) EXISTS is efficient when most of the filter criteria is in the main query.
For Example: Write the query as
Select * from product p
Instead of:
Select * from product p
5) Use EXISTS instead of DISTINCT when using joins which involves tables having one-to-many relationship.
For Example: Write the query as SELECT d.dept_id, d.dept
Instead of:
SELECT DISTINCT d.dept_id, d.dept
6) Try to use UNION ALL in place of UNION.
For Example: Write the query as SELECT id, first_name
Instead of:
SELECT id, first_name, subject
7) Be careful while using conditions in WHERE clause.
For Example: Write the query as SELECT id, first_name, age FROM student_details WHERE age > 10;
Instead of:
SELECT id, first_name, age FROM student_details WHERE age != 10;
Write the query as
SELECT id, first_name, age
Instead of:
SELECT id, first_name, age
Write the query as
SELECT id, first_name, age
Instead of:
SELECT id, first_name, age
Write the query as
SELECT product_id, product_name
Instead of:
SELECT product_id, product_name
Write the query as
SELECT id, name, salary
Instead of:
SELECT id, name, salary
Use non-column expression on one side of the query because it will be processed earlier.
Write the query as
SELECT id, name, salary
Instead of:
SELECT id, name, salary
Write the query as
SELECT id, first_name, age
Instead of:
SELECT id, first_name, age
8) Use DECODE to avoid the scanning of same rows or joining the same table repetitively. DECODE can also be made used in place of GROUP BY or ORDER BY clause.
For Example: Write the query as SELECT id FROM employee
Instead of:
SELECT DECODE(location,'Bangalore',id,NULL) id FROM employee
9) To store large binary objects, first place them in the file system and add the file path in the database.
10) To write queries which provide efficient performance follow the general SQL standard rules.
a) Use single case for all SQL verbs
b) Begin all SQL verbs on a new line c) Separate all words with a single space d) Right or left aligning verbs within the initial SQL verb
-----------------------------------------------------------------------------------------------------------------------------
Here are the different performance optimization tips and topics for this article:
|
Wednesday, 18 September 2013
Sql Server Query Performance Tuning / Sql Query Optimization Tips in SQL Server
Labels:
TUNING
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment