SQL Server Best Practices
1. Drop temp table variable in procedure end of procedure.
2. Don't use indexes for the column(s) are defined as Text,NText or Image data types.
3. Create index on integer data type columns is best .
4. use the sp_executesql stored procedure instead of the EXECUTE statement.
5. Execute Sp using exec dbo.ProcName.
6. Use stored procedures instead of views.
7. If you use LIKE in your WHERE clause, try to use one or more leading characters in the clause
8. When you have a choice of using the IN or the BETWEEN clauses in your Transact-SQL, you will generally want to use the BETWEEN clause, as it is much more efficient
9. If possible, try to avoid using the SUBSTRING function in your WHERE clauses.
10. Practice to avoid nchar and nvarchar data type since both the data types takes just double memory as char and varchar
11. Practice to use Table varible in place of Temp table since Temp table resides in the TempDb database. Hence use of Temp tables required interaction with TempDb database that is a little bit time taking task.
12. Practice to use TRY-CATCH for handling errors in T-SQL statements. Sometimes an error in a running transaction may cause deadlock if you have no handle error by using TRY-CATCH
13. Use BEGIN , COMMIT and ROLLBACK in sp to avoid unnecessary
Performance Related:
1. Set NO count on
2. Set Statistics io off
3. For better performance, if you need a temporary table in your Transact-SQL code, consider using a table variable instead of creating a conventional temporary table.
4. Unwanted joining tables removed
5. Don't use select distinct for single row selection instead of that use Select Top 1
6. Don't use select count(*) form row exists or not checking instead of that use Select Top 1.
7. If you need to verify the existence of a record in a table, don't use SELECT COUNT (*) in your Transact-SQL code to identify it. This is very inefficient and wastes server resources. Instead, use the Transact-SQL IF EXISTS to determine if the record in question exists, which is much more efficient.
8. Use dynamic query and condition checking.
9. Avoid sub query and use join for inserting one table record into another table.
10. Use clustered index and multiple column non clustered indexes for order by and group by function.
11. Removed un used indexes from tables.
12. Don't add indexes to small tables.
13. Index should be added to which column frequently used Top,Order by,Distinct,Where,Group By
14. Don't add more indexes to OLTP tables it gives increases overhead on table .
15. Do not add the same index more than once on a table with different names.
16. Avoid Index for rarely queried but insertion, updating is frequent.
17. Where clause with AND operator with index column filter boost performance.
18. Try to create stored procedure inline sql querys.
19. Avoid function and UDF in where class
20. Instead of cursor use some loop (while)
21. If possible, avoid using SQL Server cursors. They generally use a lot of SQL Server resources and reduce the performance and scalability of your applications.
22. Use foreign key to avoid duplication and correct data insert and delete from a table.
23. Foreign key also give some performance wise improvement.(Execution Plan).
24. Keep Procedure T-Sql lines as short as possible it will reduce no of locks so speed will automatically improve.
25. Don't Use SP_ in front of Stored Procedures.
26. Remove Un used lines in stored procedure affect performance speed so remove it
27. If you use input parameters in your stored procedures, you should validate all of them at the beginning of your stored procedure. This way, if there is a validation problem and the client application needs to be notified of the problem, it happens before any stored procedure processing takes place, preventing wasted effort and boosting performance.
28. I you use BULK INSERT to import data into SQL Server, then use the TABLOCK hint along with it. This will prevent SQL Server from running out of locks during very large imports and will also boost performance due to the reduction of lock contention
29. If you need to store large strings of data and they are less than 8000 characters, use a VARCHAR data type instead of a TEXT data type. TEXT data types have extra overhead that drag down performance.
30. Don't use the DATETIME data type as a primary key. From a performance perspective, it is more efficient to use a data type that uses less space.
31. Don't use Upper Lower function in where clause it will reduce speed of executions.
32. Choose appropriate SQL Data Type to store your data since it also helps in to improve the query performance.
33. Don't use ORDER BY in your SELECT statements unless you really need to, as it adds a lot of extra overhead. For example, perhaps it may be more efficient to sort the data at the client than at the server.
34. Practice to use EXISTS to check existence instead of IN since EXISTS is faster than IN.
No comments:
Post a Comment