20 SQL Server Query Optimization Tips When You Begin Tuning

Tosska Technologies
3 min readAug 25, 2021

Experienced database professionals often note how hesitant they were as beginners although they don’t understand the reasons behind it. However, this fear forces them to learn on the go without realizing when they’ve learned a good practice or tip that will help in database optimization later.

In this article, we will talk about a long list of such SQL Server query optimization tips, most of which are just as useful for other platforms as well. These bring a range of advantages from saving time and resources to improving overall database performance and speeding up queries. Make sure to save these tips for future reference:

1. Avoid applying the asterisk (*) in the statements you write. This is because a “*”, especially when used with SELECT, results in an overload on the bandwidth, input-output, and the database.

2. The sequence of the columns associated with indexes is more helpful when it remains the same on JOIN and WHERE clauses.

3. Don’t overuse VIEWs and JOINs. In fact, utilize them only when they are absolutely necessary for SQL Server performance tuning or otherwise maintaining database performance. Keep a check on your WHERE clause, using TOP as required.

4. Place critical statements in stored procedures so you can check whether their performance improves at any point.

5. Never use cursors, no matter how important they may seem.

6. Make a habit of placing limitations on the number of columns and rows you must fetch from the database. This will help you save to disk, network, and memory resources.

7. Assess server health regularly to know in case it gets affected with the problem of lack of disk space and reserve a minimum of thirty percent disk space to prepare in advance. This will also keep you from wasting time searching all over the database when performance issues arise.

8. Since SQL Server is case insensitive, you don’t have to use functions to ensure case sensitivity, such as the use of UPPER/LOWER functions while comparing VARCHARs.

9. You can make your slow queries use an index (if it isn’t doing so already) through WITH (INDEX=<name of index>) immediately after you declare the table with the FROM clause. This is one of the simplest yet most overlooked SQL Server query optimization tips.

10. Using BETWEEN or EXISTS/NOT EXISTS is preferable over IN/NOT IN is recommended because IN/NOT IN places an unnecessary load on the database.

11. Try not to use functions in your queries, especially those like SUBSTRING. Use LIKE in their place.

12. Keep wildcards on the right of the VARCHAR data type if you have to apply the LIKE operator.

13. Statements containing all of their operations connected by ANDs and present within the WHERE clause are executed from the left to right. This means any operations on the right will be ignored if the operation before them returns false because the AND output cannot be changed. Therefore, you will gain a result quicker if you begin your WHERE clause with operations more likely to return false (since they will save time).

14. Use the HAVING clause sparingly as well. Also, filter as many results as you can on the WHERE clause in case your query has a HAVING clause.

15. If you need some data quickly during SQL Server performance tuning and its completeness is not a priority, utilize the FAST option.

16. Wherever you can, substitute UNION with UNION ALL because it will help you get rid of duplicate rows that are taking up server resources.

17. Reduce the use of subqueries or at least nest them within a specific block.

18. Reduce the number of operations you would do in your WHERE clauses.

19. Increase the use of variable tables while reducing the number of temporary tables.

20. Use functions for code that requires use in multiple sections. At the same time, don’t use them too many times.

Conclusion: Keeping these twenty points in mind will help you ensure optimal SQL Server performance tuning. And, if you are looking for more such tips and techniques, reach out to our experts today!

--

--

Tosska Technologies

Tosska Technologies provides the finest SQL Server database maintenance services.