Thursday, May 27, 2021

Procedure Optimization Technique

1. The simplest guidance is do not create the stored procedure with prefix "sp_". I prefer to create the procedures with a convention like <projectabbreviation_spProcedureName>.
2. Include the SET NOCOUNT ON statement as the first statement of the procedure.
3. Do not write "Select count(*) from Table" statement to get the count of the records. Alternatively, use "Select count (PrimaryKeyColumn) from Table".
4. Try to avoid dynamic SQL queries as much as possible.
5. Prefer to have the table variables instead of temp tables. Keep minimal use of temp tables.
6. Avoid the use of the cursors to loop through the records. Instead keep the records in the table variable or temp tables.
Use schema name with an object name.
7. Prefer table joins over the use of subqueries in the where conditions.
8 Transfer the relevant records from the primary tables to the table variable and do further processing with the table variable, thereby disconnecting the primary tables reference.
9. Try to avoid using NOT LIKE operator.
10.Create Non-clustered indexes, wherever it is necessary
11. Use the sp_executesql stored procedure instead of the EXECUTE statement.
12. Create views for repeatedly used select queries.
13. Keep only the required columns as part of the final SELECT query instead of saying "SELECT * FROM..".
14 Instead of keeping a very large stored procedure, try to break it into smaller sub-procedures, wherever possible.
15 When you fetch the records from the stored procedure and display in the grid format on the front-end, prefer to have custom paging logic when the underlying results set contains many records.

No comments:

Followers

Link