“Oh My God! What did I do?” Chances are you have heard, or even uttered, this expression. This demo-oriented session will have many examples where developers were dumbfounded by their own mistakes. The goal of this session is to learn which small details can be dangerous to the production environment and SQL Server as a whole. We will talk about common bad habits and how to avoid them. There is good chance you will remember your early days during the session. Learning some of these tricks may save your current job.
2. Agenda
• TSQL Worst Practices
– TSQL Usages that kills performance
– TSQL Usages that produce unexpected results
2
3. Using NOT IN()
• Use NOT EXISTS() instead of NOT IN()
• When using NOT IN() be careful for NULL
values
3
4. COUNT() with LEFT JOIN
• COUNT(*) with LEFT JOIN may produce
incorrect results
• Use COUNT(column) from RIGHT side table
instead of COUNT(*)
4
5. Using Incorrect Data Type
• Incorrect data types in WHERE clause causes
TABLE SCAN due to implicit conversion
5
6. Functions in WHERE Clause
• Functions around columns in a WHERE clause
causes TABLE SCAN
6
7. ORDER BY with Ordinal Position
• Avoid using ordinal position in ORDER BY
• If an ordinal position is specified in an
expression, it may not produce the expected
results
• If an expression is used in ORDER BY, it will be
evaluated when FILTERS are evaluated
7
8. SET XACT_ABORT ON
• SET XACT_ABORT ON in stored procedures to
rollback open transactions in case of
unhandled errors
8