SlideShare a Scribd company logo
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

More Related Content

PPT
Mca ii-dbms-u-v-transaction management
PPTX
Trigger in mysql
DOCX
Abap performance tunning tips
PPTX
Sql server ___________session_15(data integrity)
PPTX
3. ddl create
PPTX
2. DML_INSERT_DELETE_UPDATE
PDF
5. Group Functions
DOCX
Forall & bulk binds
Mca ii-dbms-u-v-transaction management
Trigger in mysql
Abap performance tunning tips
Sql server ___________session_15(data integrity)
3. ddl create
2. DML_INSERT_DELETE_UPDATE
5. Group Functions
Forall & bulk binds

What's hot (17)

PDF
Twp Upgrading 10g To 11g What To Expect From Optimizer
 
PPTX
SQL Tutorial for Beginners
PDF
Sql select statement
PDF
Db function
PPTX
Lab5 sub query
PDF
Sql scripting sorcerypaper
PPTX
02 database oprimization - improving sql performance - ent-db
PPT
Review of SQL
PPTX
Clase 13 integridad modificada
PPTX
Sql server ___________session_19(triggers)
PPTX
Procedures and triggers in SQL
PPT
Data integrity
PPT
DOCX
It6312 dbms lab-ex2
PPTX
Lab1 select statement
PPT
Part36 parameter,form success
PPT
Myth busters - performance tuning 101 2007
Twp Upgrading 10g To 11g What To Expect From Optimizer
 
SQL Tutorial for Beginners
Sql select statement
Db function
Lab5 sub query
Sql scripting sorcerypaper
02 database oprimization - improving sql performance - ent-db
Review of SQL
Clase 13 integridad modificada
Sql server ___________session_19(triggers)
Procedures and triggers in SQL
Data integrity
It6312 dbms lab-ex2
Lab1 select statement
Part36 parameter,form success
Myth busters - performance tuning 101 2007
Ad

Viewers also liked (20)

PPTX
MS SQL SERVER: Creating Views
PDF
Using grouping sets in sql server 2008 tech republic
PPTX
Consultas en MS SQL Server 2012
PPT
Sql tuning guideline
PPS
Sql xp 04
PPTX
Statistics
PPT
e computer notes - Subqueries
PPTX
Sub-queries,Groupby and having in SQL
PPSX
Locking in SQL Server
RTF
Triggers-Sequences-SQL
PDF
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
PPT
Lecture 4. MS SQL. DML Triggers
PPT
Locking And Concurrency
PPTX
Sql query analyzer & maintenance
PDF
SQL Server - Using Tools to Analyze Query Performance
PPTX
View, Store Procedure & Function and Trigger in MySQL - Thaipt
PPT
SQL subquery
PPTX
Database Transactions and SQL Server Concurrency
PPTX
Data Compression In SQL
MS SQL SERVER: Creating Views
Using grouping sets in sql server 2008 tech republic
Consultas en MS SQL Server 2012
Sql tuning guideline
Sql xp 04
Statistics
e computer notes - Subqueries
Sub-queries,Groupby and having in SQL
Locking in SQL Server
Triggers-Sequences-SQL
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
Lecture 4. MS SQL. DML Triggers
Locking And Concurrency
Sql query analyzer & maintenance
SQL Server - Using Tools to Analyze Query Performance
View, Store Procedure & Function and Trigger in MySQL - Thaipt
SQL subquery
Database Transactions and SQL Server Concurrency
Data Compression In SQL
Ad

Similar to Sql db optimization (20)

PDF
Tips for Database Performance
PPTX
Quick & Easy SQL Tips
PPTX
Quick & Easy SQL Tips
PDF
Database development coding standards
PDF
Sql coding-standard-sqlserver
PDF
SQL Database Performance Tuning for Developers
PPTX
Database Performance Tuning
PPTX
Query Optimization in SQL Server
PPTX
Database optimization
PPTX
SQL Server 2012 Best Practices
PPTX
Sql good practices
PPTX
Stored procedure tunning
PPT
Sql server performance tuning and optimization
PPTX
Database training for developers
PDF
10 sql tips
PPTX
Tips & Tricks SQL in the City Seattle 2014
PDF
Speed up sql
PDF
Backpack Tools4 Sql Dev
PDF
Sql query tips or query optimization
PPTX
Stored procedure tuning and optimization t sql
Tips for Database Performance
Quick & Easy SQL Tips
Quick & Easy SQL Tips
Database development coding standards
Sql coding-standard-sqlserver
SQL Database Performance Tuning for Developers
Database Performance Tuning
Query Optimization in SQL Server
Database optimization
SQL Server 2012 Best Practices
Sql good practices
Stored procedure tunning
Sql server performance tuning and optimization
Database training for developers
10 sql tips
Tips & Tricks SQL in the City Seattle 2014
Speed up sql
Backpack Tools4 Sql Dev
Sql query tips or query optimization
Stored procedure tuning and optimization t sql

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Empathic Computing: Creating Shared Understanding
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
KodekX | Application Modernization Development
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Electronic commerce courselecture one. Pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Chapter 3 Spatial Domain Image Processing.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Review of recent advances in non-invasive hemoglobin estimation
Programs and apps: productivity, graphics, security and other tools
NewMind AI Weekly Chronicles - August'25 Week I
Empathic Computing: Creating Shared Understanding
Dropbox Q2 2025 Financial Results & Investor Presentation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KodekX | Application Modernization Development
sap open course for s4hana steps from ECC to s4
Cloud computing and distributed systems.
Teaching material agriculture food technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Electronic commerce courselecture one. Pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Mobile App Security Testing_ A Comprehensive Guide.pdf

Sql db optimization

  • 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.