Database Optimization Tips




                    Nikhildas.P.C
                    nikhildasp@gmail.com
Topics

1.   Transact – SQL optimization tips
2.   SQL Server optimization tips
1. Transact – SQL optimization tips

1.   Try to restrict the queries result set by
     using the WHERE clause.
2.   Try to restrict the queries result set by
     returning only the particular columns
     from the table, not all table’s columns.
3.   Use views and stored procedures
     instead of heavy-duty queries.
4.   Try to avoid using SQL Server cursors,
     whenever possible.
5.   If you need to return the total table’s
     row count, you can use alternative way
     instead of SELECT COUNT(*) statement.
1. Transact – SQL optimization tips

1.   Use table variables instead of temporary
     tables.
2.   Try to avoid the HAVING clause,
     whenever possible.
3.   Try to avoid using the DISTINCT clause,
     whenever possible.
4.   Include SET NOCOUNT ON statement
     into your stored procedures to stop the
     message indicating the number of rows
     affected by a T-SQL statement.
1. Transact – SQL optimization tips

1.   Use the select statements with TOP
     keyword or the SET ROWCOUNT
     statement, if you need to return only
     the first n rows.
2.   Use the FAST number_rows table hint if
     you need to quickly return
     ‘number_rows’ rows.
3.   Try to use UNION ALL statement instead
     of UNION, whenever possible.
4.   Do not use optimizer hints in your
     queries.
1. Transact – SQL optimization tips

1.   Try to avoid using DDL (Data Definition
     Language) statements inside your
     stored procedure.
2.   If you have a very large stored
     procedure, try to break down this stored
     procedure into several sub-procedures,
     and call them from a controlling stored
     procedure.
3.   Use the sp_executesql stored procedure
     instead of the EXECUTE statement.
4.   Try to use constraints instead of
     triggers, whenever possible.
1. Transact – SQL optimization tips

1.   Don’t use the prefix “sp_” in the stored
     procedure name if you need to create a
     stored procedure to run in a database
     other than the master database.
2.   Consider returning the integer value as
     an RETURN statement instead of an
     integer value as part of a recordset.
3.   Call stored procedure using its fully
     qualified name.
4.   Try to avoid using temporary tables
     inside your stored procedure.
1. Transact – SQL optimization tips

1.   Include the SET ANSI_WARNINGS ON,
     SET ANSI_NULLS ON statement into
     your stored procedures.
2.   Always add an Debug parameter to your
     stored procedures. This can be a BIT
     data type.
3.   If your stored procedure always returns
     a single row resultset, consider
     returning the resultset using OUTPUT
     parameters instead of a SELECT
     statement
1. Transact – SQL optimization tips
1.   Use Alias name with Column Name in
     select statement.
2.   Use appropriate Join (Inner,Outer,Self)
3.   Use Where condition before Joining (Eg.
     Select D.CustName from ( select ID
     from CustMaster where CustID = 12346
     ) M Inner Join CustDetails D on M.ID =
     D.ID
4.   Use Between instead of >= <=, if
     possible
5.   Use Order by,where,join For Indexed
     Column.
6.   Avoid functions with WHERE clause.
1. Transact – SQL optimization tips
1.   Do not use column numbers in the
     ORDER BY clause - Eg. Order by 2
2.   ANSI-Standard Join clauses instead of
     the old style joins.
3.   Do not call functions repeatedly within
     your stored procedures, triggers,
     functions and batches.(Eg. Len (Name)
     call the LEN function once, and store the
     result in a variable for later use.)
4.   If you need to delete all tables rows,
     consider using TRUNCATE TABLE
     instead of DELETE command.
1. Transact – SQL optimization tips
1.   Do not let your front-end applications
     query/manipulate the data directly
     using SELECT or INSERT/UPDATE /
     DELETE statements. Instead, create
     stored procedures and let your
     applications access these stored
     procedures.
2.   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."
1. Transact – SQL optimization tips
1.   Don't Compare null value with =,<>
     (Eg.IF @DDate <> NULL) Use – IF
     @Ddate Is Null
2.   A Table should have
     . primary key
     . minimum of one clustered index
     . appropriate amount of non-clustered
     index
     . Non-clustered index should be created
     on columns of table based on query
     which is running
1. Transact – SQL optimization tips
1.   Following priority order should be
     followed when any index is created a)
     WHERE clause, b) JOIN clause, c)
     ORDER BY clause, d) SELECT clause.
2.   Check if there is at least 30% HHD is
     empty – it improves the performance a
     bit.
3.   Use Error Tracing in SP,Fn,trigger (Eg.
     Try Catch or @@ERROR global variable)
4.   Use BEGIN TRANSACTION, COMMIT
     TRANSACTION and ROLLBACK
     TRANSACTION.
1. Transact – SQL optimization tips
1.   Avoid using dynamic SQL statements
     inside stored procedures, i.e., Do not
     use SQL statements to create SQL
     statements.
2.   Be careful while SELECT-ing strings with
     LIKE clause. If it is not used wisely, it
     will give rise to performance problems.
3.   Use Update Statistics with bulk
     insertion, deletion query.
4.   Use Covering indexes.
1. Transact – SQL optimization tips
1.   Use indexed Views.
2.    Use Apply clause, if Possible
3.   Use Computed Column
4.   Use Profiler,Database engine Tuning
     advisor,Execution plan to monitor
     performance.
5.   Try to write code neatly with proper
     commenting and program flow picture.
THANKS

Sql db optimization

  • 1.
    Database Optimization Tips Nikhildas.P.C nikhildasp@gmail.com
  • 2.
    Topics 1. Transact – SQL optimization tips 2. SQL Server optimization tips
  • 3.
    1. Transact –SQL optimization tips 1. Try to restrict the queries result set by using the WHERE clause. 2. Try to restrict the queries result set by returning only the particular columns from the table, not all table’s columns. 3. Use views and stored procedures instead of heavy-duty queries. 4. Try to avoid using SQL Server cursors, whenever possible. 5. If you need to return the total table’s row count, you can use alternative way instead of SELECT COUNT(*) statement.
  • 4.
    1. Transact –SQL optimization tips 1. Use table variables instead of temporary tables. 2. Try to avoid the HAVING clause, whenever possible. 3. Try to avoid using the DISTINCT clause, whenever possible. 4. Include SET NOCOUNT ON statement into your stored procedures to stop the message indicating the number of rows affected by a T-SQL statement.
  • 5.
    1. Transact –SQL optimization tips 1. Use the select statements with TOP keyword or the SET ROWCOUNT statement, if you need to return only the first n rows. 2. Use the FAST number_rows table hint if you need to quickly return ‘number_rows’ rows. 3. Try to use UNION ALL statement instead of UNION, whenever possible. 4. Do not use optimizer hints in your queries.
  • 6.
    1. Transact –SQL optimization tips 1. Try to avoid using DDL (Data Definition Language) statements inside your stored procedure. 2. If you have a very large stored procedure, try to break down this stored procedure into several sub-procedures, and call them from a controlling stored procedure. 3. Use the sp_executesql stored procedure instead of the EXECUTE statement. 4. Try to use constraints instead of triggers, whenever possible.
  • 7.
    1. Transact –SQL optimization tips 1. Don’t use the prefix “sp_” in the stored procedure name if you need to create a stored procedure to run in a database other than the master database. 2. Consider returning the integer value as an RETURN statement instead of an integer value as part of a recordset. 3. Call stored procedure using its fully qualified name. 4. Try to avoid using temporary tables inside your stored procedure.
  • 8.
    1. Transact –SQL optimization tips 1. Include the SET ANSI_WARNINGS ON, SET ANSI_NULLS ON statement into your stored procedures. 2. Always add an Debug parameter to your stored procedures. This can be a BIT data type. 3. If your stored procedure always returns a single row resultset, consider returning the resultset using OUTPUT parameters instead of a SELECT statement
  • 9.
    1. Transact –SQL optimization tips 1. Use Alias name with Column Name in select statement. 2. Use appropriate Join (Inner,Outer,Self) 3. Use Where condition before Joining (Eg. Select D.CustName from ( select ID from CustMaster where CustID = 12346 ) M Inner Join CustDetails D on M.ID = D.ID 4. Use Between instead of >= <=, if possible 5. Use Order by,where,join For Indexed Column. 6. Avoid functions with WHERE clause.
  • 10.
    1. Transact –SQL optimization tips 1. Do not use column numbers in the ORDER BY clause - Eg. Order by 2 2. ANSI-Standard Join clauses instead of the old style joins. 3. Do not call functions repeatedly within your stored procedures, triggers, functions and batches.(Eg. Len (Name) call the LEN function once, and store the result in a variable for later use.) 4. If you need to delete all tables rows, consider using TRUNCATE TABLE instead of DELETE command.
  • 11.
    1. Transact –SQL optimization tips 1. Do not let your front-end applications query/manipulate the data directly using SELECT or INSERT/UPDATE / DELETE statements. Instead, create stored procedures and let your applications access these stored procedures. 2. 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."
  • 12.
    1. Transact –SQL optimization tips 1. Don't Compare null value with =,<> (Eg.IF @DDate <> NULL) Use – IF @Ddate Is Null 2. A Table should have . primary key . minimum of one clustered index . appropriate amount of non-clustered index . Non-clustered index should be created on columns of table based on query which is running
  • 13.
    1. Transact –SQL optimization tips 1. Following priority order should be followed when any index is created a) WHERE clause, b) JOIN clause, c) ORDER BY clause, d) SELECT clause. 2. Check if there is at least 30% HHD is empty – it improves the performance a bit. 3. Use Error Tracing in SP,Fn,trigger (Eg. Try Catch or @@ERROR global variable) 4. Use BEGIN TRANSACTION, COMMIT TRANSACTION and ROLLBACK TRANSACTION.
  • 14.
    1. Transact –SQL optimization tips 1. Avoid using dynamic SQL statements inside stored procedures, i.e., Do not use SQL statements to create SQL statements. 2. Be careful while SELECT-ing strings with LIKE clause. If it is not used wisely, it will give rise to performance problems. 3. Use Update Statistics with bulk insertion, deletion query. 4. Use Covering indexes.
  • 15.
    1. Transact –SQL optimization tips 1. Use indexed Views. 2. Use Apply clause, if Possible 3. Use Computed Column 4. Use Profiler,Database engine Tuning advisor,Execution plan to monitor performance. 5. Try to write code neatly with proper commenting and program flow picture.
  • 16.