T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

16
T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

Transcript of T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

Page 1: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

T-SQL : Bad Habits to Kick

Aaron BertrandSQL Sentry, Inc.

Page 2: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

2

Who is Aaron Bertrand?

• Senior Consultant at SQL Sentry www.sqlsentry.net [email protected] • Microsoft MVP since 1997-98• Blog: www.sqlblog.com • Twitter: @AaronBertrand

AD-204 | T-SQL : Bad Habits to Kick

Page 3: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

3

Agenda

12 simple slides with one common goal:Improving at least one bad habit.

These are mostly just opinions; No right or wrong answer.

AD-204 | T-SQL : Bad Habits to Kick

Page 4: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

4

1. SELECT * / omitting column list

• Needless lookups/scans, I/O, network load• Predictability / change management• Today’s tools negate carpal tunnel excuse

AD-204 | T-SQL : Bad Habits to Kick

Page 5: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

5

2. Declaring variables without length

Guess the results:

DECLARE @x VARCHAR = 'foo';SELECT @x;

SELECT CONVERT(VARCHAR, 'foo');

AD-204 | T-SQL : Bad Habits to Kick

Page 6: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

6

3. Choosing the wrong data type

Don’t choose:• String/numeric types for date/time data• TIME in place of an interval• DATETIME if DATE/SMALLDATETIME will do• NVARCHAR(MAX) for URL, zip, phone, e-

mail• VARCHAR for proper names

AD-204 | T-SQL : Bad Habits to Kick

Page 7: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

7

4. Not using schema prefix

Being explicit prevents confusion or worse• Object resolution works harder without it• Leads to multiple cached plans for same query

Even if all objects belong to dbo, specify• Eventually, you or 3rd party will use schemas

AD-204 | T-SQL : Bad Habits to Kick

Page 8: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

8

5. Using inconsistent naming conventions

Examples I’ve seen in a single system:• GetCustomerDetails• Customer_Update• Create_Customer

Styles vary – even your own changes over time• The convention you choose isn’t the point

AD-204 | T-SQL : Bad Habits to Kick

Page 9: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

9

6. Using loops to populate large tables

WHILE…INSERT 1,000,000 times is log intensiveMuch better constructs:• Numbers table• Recursive CTEs• Cross joins from catalog views

If you must use a loop• Batch by committing every <n> rows

AD-204 | T-SQL : Bad Habits to Kick

Page 10: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

10

7. Mishandling date range queries

Avoid non-sargeable clauses that come from:• YEAR() and other functions against

columns• CONVERT() on both sides of clauses

BETWEEN is ok for DATE but not DATETIME

Do not try to calculate “end of today”:• Use >= today AND < tomorrowAD-204 | T-SQL : Bad Habits to Kick

Page 11: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

11

8. Using SELECT/RETURN vs. OUTPUTIn general…

• SELECT is for multiple rows/columns• OUTPUT is for limited number of scalar values• RETURN is for status/error codes, NOT DATA!

AD-204 | T-SQL : Bad Habits to Kick

Page 12: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

12

9. Using old-style joins

Do not use old-style inner joins (FROM x, y, z)• Easy to accidentally derive Cartesian product• Not deprecated, but not recommended either

Do not use old-style outer joins (*= / =*)• Deprecated syntax • Unpredictable results

AD-204 | T-SQL : Bad Habits to Kick

Page 13: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

13

10. Using cursors with default options• Cursors are often bad, but not evil• Avoid heavy locking behavior - my syntax:

DECLARE c CURSORLOCAL STATIC READ_ONLY FORWARD_ONLYFOR …

AD-204 | T-SQL : Bad Habits to Kick

Page 14: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

14

11. Using ORDER BY [ordinal]

• Underlying structure/query changes• OK habit for ad hoc stuff, not production code• Keystrokes are only downside to being explicit• IntelliSense / 3rd party tools negate this

anyway

AD-204 | T-SQL : Bad Habits to Kick

Page 15: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

15

12. Assuming ORDER without ORDER BY

Popular myth: “table has natural order”• Without ORDER BY, there is no guaranteed

order• TOP unfortunately has two meanings:

1. Which rows to include2. How to order them

• To separate, use a CTE or nested subquery

AD-204 | T-SQL : Bad Habits to Kick

Page 16: T-SQL : Bad Habits to Kick Aaron Bertrand SQL Sentry, Inc.

16

13 -> …plenty of others…

Search for “bad habits to kick” athttp://sqlblog.com/

AD-204 | T-SQL : Bad Habits to Kick