SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Twp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From OptimizerTwp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From Optimizer
qiw
 
Sql select statement
Sql select statementSql select statement
Sql select statement
Vivek Singh
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
Clase 13 integridad modificada
Clase 13 integridad   modificadaClase 13 integridad   modificada
Clase 13 integridad modificada
Titiushko Jazz
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)
Ehtisham Ali
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 

Mais procurados (17)

Twp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From OptimizerTwp Upgrading 10g To 11g What To Expect From Optimizer
Twp Upgrading 10g To 11g What To Expect From Optimizer
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
Db function
Db functionDb function
Db function
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Sql scripting sorcerypaper
Sql scripting sorcerypaperSql scripting sorcerypaper
Sql scripting sorcerypaper
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Clase 13 integridad modificada
Clase 13 integridad   modificadaClase 13 integridad   modificada
Clase 13 integridad modificada
 
Sql server ___________session_19(triggers)
Sql server  ___________session_19(triggers)Sql server  ___________session_19(triggers)
Sql server ___________session_19(triggers)
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
Data integrity
Data integrityData integrity
Data integrity
 
Les18
Les18Les18
Les18
 
It6312 dbms lab-ex2
It6312 dbms lab-ex2It6312 dbms lab-ex2
It6312 dbms lab-ex2
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Part36 parameter,form success
Part36 parameter,form successPart36 parameter,form success
Part36 parameter,form success
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 

Destaque

Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
Kaing Menglieng
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
Sidney Chen
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
ecomputernotes
 
Triggers-Sequences-SQL
Triggers-Sequences-SQLTriggers-Sequences-SQL
Triggers-Sequences-SQL
Patrick Seery
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - Thaipt
Framgia Vietnam
 
Data Compression In SQL
Data Compression In SQLData Compression In SQL
Data Compression In SQL
Boosh Booshan
 

Destaque (20)

MS SQL SERVER: Creating Views
MS SQL SERVER: Creating ViewsMS SQL SERVER: Creating Views
MS SQL SERVER: Creating Views
 
Joins SQL Server
Joins SQL ServerJoins SQL Server
Joins SQL Server
 
Using grouping sets in sql server 2008 tech republic
Using grouping sets in sql server 2008   tech republicUsing grouping sets in sql server 2008   tech republic
Using grouping sets in sql server 2008 tech republic
 
Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012Consultas en MS SQL Server 2012
Consultas en MS SQL Server 2012
 
Sql tuning guideline
Sql tuning guidelineSql tuning guideline
Sql tuning guideline
 
Sql xp 04
Sql xp 04Sql xp 04
Sql xp 04
 
Statistics
StatisticsStatistics
Statistics
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Sub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQLSub-queries,Groupby and having in SQL
Sub-queries,Groupby and having in SQL
 
Locking in SQL Server
Locking in SQL ServerLocking in SQL Server
Locking in SQL Server
 
Triggers-Sequences-SQL
Triggers-Sequences-SQLTriggers-Sequences-SQL
Triggers-Sequences-SQL
 
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1 "Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
"Using Indexes in SQL Server 2008" by Alexander Korotkiy, part 1
 
Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML Triggers
 
Locking And Concurrency
Locking And ConcurrencyLocking And Concurrency
Locking And Concurrency
 
Sql query analyzer & maintenance
Sql query analyzer & maintenanceSql query analyzer & maintenance
Sql query analyzer & maintenance
 
SQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query PerformanceSQL Server - Using Tools to Analyze Query Performance
SQL Server - Using Tools to Analyze Query Performance
 
View, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - ThaiptView, Store Procedure & Function and Trigger in MySQL - Thaipt
View, Store Procedure & Function and Trigger in MySQL - Thaipt
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
 
Data Compression In SQL
Data Compression In SQLData Compression In SQL
Data Compression In SQL
 

Semelhante a Sql db optimization

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 
Sql coding-standard-sqlserver
Sql coding-standard-sqlserverSql coding-standard-sqlserver
Sql coding-standard-sqlserver
lochaaaa
 
Mohan Testing
Mohan TestingMohan Testing
Mohan Testing
smittal81
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 

Semelhante a Sql db optimization (20)

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Sql coding-standard-sqlserver
Sql coding-standard-sqlserverSql coding-standard-sqlserver
Sql coding-standard-sqlserver
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Basics on SQL queries
Basics on SQL queriesBasics on SQL queries
Basics on SQL queries
 
Mohan Testing
Mohan TestingMohan Testing
Mohan Testing
 
Teradata sql-tuning-top-10
Teradata sql-tuning-top-10Teradata sql-tuning-top-10
Teradata sql-tuning-top-10
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
Oracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesOracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practices
 
Optimized cluster index generation
Optimized cluster index generationOptimized cluster index generation
Optimized cluster index generation
 
Stored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sqlStored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sql
 
PLSQL Practices
PLSQL PracticesPLSQL Practices
PLSQL Practices
 
Guide To Mastering The MySQL Query Execution Plan
Guide To Mastering The MySQL Query Execution PlanGuide To Mastering The MySQL Query Execution Plan
Guide To Mastering The MySQL Query Execution Plan
 
PLSQL Advanced
PLSQL AdvancedPLSQL Advanced
PLSQL Advanced
 
SQL Server 2008 Performance Enhancements
SQL Server 2008 Performance EnhancementsSQL Server 2008 Performance Enhancements
SQL Server 2008 Performance Enhancements
 
Db2 sql tuning and bmc catalog manager
Db2 sql tuning and bmc catalog manager Db2 sql tuning and bmc catalog manager
Db2 sql tuning and bmc catalog manager
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
Stored procedure tunning
Stored procedure tunningStored procedure tunning
Stored procedure tunning
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

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.