SQL SERVER Database Coding Standards and Guidelines Part 2
SQL SERVER Database Coding Standards and Guidelines Part 2
Part 2
blog.sqlauthority.com/2007/06/05/sql-server-database-coding-standards-and-guidelines-part-2/
Pinal Dave
Coding Standards
Use Derived tables or CTE (Common Table Expressions) wherever possible, as they perform better 6
Or use TRY/CATCH
1/5
Always use a column list in your INSERT statements.
This helps avoid problems when the table structure changes (like adding or dropping a column).
Minimize the use of NULLs, as they often confuse front-end applications, unless the applications are coded
intelligently to eliminate NULLs or convert the NULLs into some other form.
Avoid using TEXT or NTEXT datatypes for storing large textual data.9
Access tables in the same order in your stored procedures and triggers consistently. 11
Do not call functions repeatedly within your stored procedures, triggers, functions and batches. 12
Default constraints must be defined at the column level.
Avoid wild-card characters at the beginning of a word while searching using the LIKE keyword, as these
results in an index scan, which defeats the purpose of an index.
Define all constraints, other than defaults, at the table level.
When a result set is not needed, use syntax that does not return a result set. 13
Avoid rules, database level defaults that must be bound or user-defined data types. While these are
legitimate database constructs, opt for constraints and column defaults to hold the database consistent for
development and conversion coding.
Constraints that apply to more than one column must be defined at the table level.
Use the CHAR data type for a column only when the column is non-nullable.14
Do not use white space in identifiers.
The RETURN statement is meant for returning the execution status only, but not data.
Reference:
2/5
5) Use the graphical execution plan in Query Analyzer or SHOWPLAN_TEXT or SHOWPLAN_ALL commands to
analyze your queries. Make sure your queries do an Index seek instead of an Index scan or a Table scan. A
table scan or an index scan is a highly undesirable and should be avoided where possible.
6) Consider the following query to find the second highest offer price from the Items table:
1 SELECT MAX(Price)
2 FROM Products
3 WHERE ID IN
4 (
5 SELECT TOP 2 ID
6
FROM Products
7
ORDER BY Price DESC
8
)
The same query can be re-written using a derived table, as shown below, and it performs generally twice as fast as
the above query:
3/5
1 SELECT MAX(Price)
2 FROM
3 (
4 SELECT TOP 2 Price
5 FROM Products
6
ORDER BY Price DESC
7
)
7) This suppresses messages like (1 row(s) affected) after executing INSERT, UPDATE, DELETE and SELECT
statements. Performance is improved due to the reduction of network traffic.
8) Try to avoid server side cursors as much as possible. Always stick to a set-based approach instead of a
procedural approach for accessing and manipulating data. Cursors can often be avoided by using SELECT
statements instead. If a cursor is unavoidable, use a WHILE loop instead. For a WHILE loop to replace a cursor,
however, you need a column (primary key or unique key) to identify each row uniquely.
9) You cannot directly write or update text data using the INSERT or UPDATE statements. Instead, you have to use
special statements like READTEXT, WRITETEXT and UPDATETEXT. So, if you dont have to store more than 8KB
of text, use the CHAR(8000) or VARCHAR(8000) datatype instead.
10) Dynamic SQL tends to be slower than static SQL, as SQL Server must generate an execution plan at runtime. IF
and CASE statements come in handy to avoid dynamic SQL.
11) This helps to avoid deadlocks. Other things to keep in mind to avoid deadlocks are:
12) You might need the length of a string variable in many places of your procedure, but dont call the LEN function
whenever its needed. Instead, call the LEN function once and store the result in a variable for later use.
13)
1 IF EXISTS (
2 SELECT 1
3 FROM Products
4 WHERE ID =
50)
Instead Of:
4/5
1 IF EXISTS (
2 SELECT COUNT(ID)
3 FROM Products
4 WHERE ID = 50)
14) CHAR(100), when NULL, will consume 100 bytes, resulting in space wastage. Preferably, use VARCHAR(100)
in this situation. Variable-length columns have very little processing overhead compared with fixed-length columns.
5/5