Introduction
Good user experience (UX) is one of the most important factor of a good software. UX also includes the performance of the app. For example, if you need for data to be loaded on a screen, that is just a bad design. For any data-centric app, the backend database performance is very critical. If a database can't return data fast enough, it will affect everything including the front-end responsiveness.
SQL is the language to access and work with RDBMS such as SQL Server. If your SQL is written bad, it will certainly affect the performance if the query. So, to build good data-centric apps, its important to understand SQL queries and their performance. In this article, I will share some tips to increase performance of SQL queries.
Here is a list of tips on how to increase performance of SQL.
1. Do not use * with select statement
SQL Server converts * into all column names of the table before query execution. Instead of doing this, pass the name of the columns that are really required in query results.
--Bad practice
SELECT * FROM Table1
--Good practice
SELECT Column1, Column2, Column3 FROM Table1
2. Use EXISTS instead of IN
EXISTS returns true if a sub query contains any rows else returns false. EXISTS is faster than the IN Query because for the IN Query SQL first collects all the data of the sub query. With EXISTS, the sub query does not produce actual query results. EXISTS is faster than IN only when sub query result is very large. IN is faster than EXISTS, when sub query result is very small
--Bad practice
SELECT Column1, Column2, Column3 FROM Table1 WHERE Column1 IN (SELECT Column1 FROM Table2)
--Good practice
SELECT Column1, Column2, Column3 FROM Table1 WHERE EXISTS (SELECT Column1 FROM Table2 Where Table2.Column1 = Table1.Column1)
3. Select Appropriate Data Type of table columns
The selection of appropriate data types may help us to improve the SQL query performance. For example, I have an Employee Table and it has a code field. The length of the code may vary from 3 to 8. In this case, instead of selecting CHAR (8) we can use the VARCHAR (8) data type. Try to choose the smallest data type that works for each column. Try choosing an appropriate data type of a column to avoid explicit and implicit conversions, because both are costly in terms of time to take for conversion.
4. Use proper join type
Select proper join type and join order. An Outer join is a more expensive process than the inner join. An Outer join must do everything that an Inner join does plus extra work for null extending results.
5. Use Indexed Views
When we have a SQL Query that contains multiple joins between tables that do not change frequently (example lookup table), we can define an indexed view for better performance. An Index view is a view stored physically like a table. The Index view is updated by SQL Server itself when a table is modified that is used as part of the index view.
6. Do not use Count (*)
Do not use Count (*) when you require a row count. Instead of this, you can use Count (1) or Count (ColumnName).
--Bad practice
SELECT COUNT(*) FROM Table1
--Good practice
SELECT COUNT(1) FROM Table1
SELECT COUNT(Column1) FROM Table1
7. Avoid use of cursors
A Cursor is used to perform a function row by row. The Cursor forces the database engine to repeatedly fetch the rows, managing the locks and transmit the results. Forward-only and read-only cursors are faster and uses the least resources. If there is a primary key on a table then we can use a while loop. Try to avoid the use of a cursor on temp tables.
8. Use a Table variable or CTE (Common Table Expression) instead of Temp Table whenever possible
Temp tables are stored physically in TempDB and they are permanent tables that are deleted after the session ends. CTE and Temp variables are created within memory. Note that CTE is not a replacement of a Table variable and a Temp table.
Refer: http://www.c-sharpcorner.com/UploadFile/ff2f08/common-table-expression-cte-in-sql-server/
9. Use SET NOCOUNT ON in Stored Procedure
The SET NOCOUNT ON statement prevents SQL Server from sending a message after each statement in a Stored Procedure. These messages are sent using a DONE_IN_PROC token. Each message contains the number of the row affected the row by the last executed SQL statement.
--Example
CREATE PROCEDURE [dbo].[MyTestProcedure]
(
@para1 INT
)
AS
BEGIN
SET NOCOUNT ON
-- Body of stored procedure.
END
10. Do not use "SP_" prefix with any user define Stored Procedure name
In SQL Server, the master database has a Stored Procedure with the "sp_" prefix, so SQL Server always looks first in the master database. If we use the "sp_" prefix for any user define Stored Procedure and put it in a database other than master, the master database is still checked first.
11. Use Stored Procedure for Complex Query and frequently used query
A Stored Procedure in SQL Server is a group of T-SQL statements. By default a Stored Procedure is precompiled, in other words a stored procedure is compiled when it executes the first time and also creates an execution plan that for subsequent calls of the Stored Procedure because the query processor does not need to create a new plan hence it takes less time to execute.
12. Use Parameterized Query
The SQL Server saves execution plan for parameterized queries. This allows it to be reused on later execution.

Vishal YelvePosted Dec 16, 2022, 2:53 PM
Great article
k_arletto-newsletterPosted Jul 3, 2019, 6:15 AM
Sorry,I didn't understand how you can use the EXISTS statement in your example and get the same result as the Bad practice query where you indicated IN clause!Then the query in good practice is not correct, It goes in error! Please correct the query so we can understand how to use EXISTS and have the same result as IN Thanks in advance
Rajanikant HawaldarPosted May 30, 2019, 1:02 AM
Useful article, thanks
Hemant SrivastavaPosted Oct 11, 2013, 3:37 PM
Nice info
Prasenjit DeyPosted Sep 20, 2013, 8:51 AM
Good article
Jignesh TrivediPosted Sep 20, 2013, 2:42 AM
Hi Sam thanks again. I know that VARCAHAR (8) is not save significant amount of space. This is just an example. I agree with you this is not a good example. Good example is use VARCHAR instead of using TEXT Data type whenever required.
Jignesh TrivediPosted Sep 20, 2013, 2:38 AM
Hi Sam, I am very thanks to you for comment on my article. I was thing from customer point of view while writing statement “Performance is the main concern of any application". Performance is also concern while everything is written into the assembly. I agree other factor like developers efficiency; environment is also concern for the application. If I am customer of any Bank and I want to do some transaction online but bank's site performance is very slow. Is it acceptable? At this time I am not thing about the developer or manager or any other resources.
Jignesh TrivediPosted Sep 20, 2013, 2:26 AM
hi ed, Correct we must use join in this case this is just an example...
Sam HobbsPosted Sep 19, 2013, 8:13 PM
For VARCHAR, how many bytes are used for length? Are you sure that VARCHAR (8) saves a significant amount of space compared to CHAR (8)? What if most data is 7 or 8 characters? A better example would be data that is normally 8 characters but can be as large as 80 characters.
Sam HobbsPosted Sep 19, 2013, 8:09 PM
I disagree that "Performance is the main concern of any application". If it were then everything would be written using assembly (machine) language. Performance is a concern of many applications but not the main concern of all applications. In my opinion, programmer efficiency is also a concern. In other words, it might be more efficient to let the computer save programmer time. I think many (most?) managers would prefer to let the computer do few milliseconds of work if it means the programmer has time to do more work.
EdPosted Sep 19, 2013, 12:10 PM
Join the tables instead.
EdPosted Sep 19, 2013, 12:09 PM
Wrong wrong wrong. Dont use: SELECT Column1, Column2, Column3 FROM Table1 WHERE Column1 IN (SELECT Column1 FROM Table2)
Dinesh BeniwalPosted Sep 19, 2013, 8:28 AM
Good Work Jignesh
Sanjeeb LenkaPosted Sep 19, 2013, 8:24 AM
Good collection...... Keep posting