SlideShare a Scribd company logo
1
2 
PHIL FACTOR 
GRANT FRITCHEY 
K. BRIAN KELLEY 
MICKEY STUEWE 
IKE ELLIS 
JONATHAN ALLEN 
LOUIS DAVIDSON
Database 
Performance 
Tips for 
Developers 
As a developer, you may or may not need to go into 
the database and write queries, or design tables and 
indexes, or help determine configuration of your SQL 
Server systems. But if you do, these tips should help 
to make that a more pain free process.
4 
Contents 
Tips 1-5 
ORM Tips 
Tips 6-24 
T-SQL Tips 
Tips 25-40 
Index Tips 
Tips 41-45 
Database Design Tips 
5 
7 
12 
17
5 
ORM Tips 
More and more people are using Object to Relational Mapping 
(ORM) tools to jump the divide between application code that is 
object oriented and a database that stores information in a relational 
manner. These tools are excellent and radically improve development 
speed, but there are a few ‘gotchas’ to know about. 
1 Avoid following the ‘Hello World’ examples provided 
with your ORM tool that turns it into an Object to 
Object Mapping. Database storage is not the same as 
objects for a reason. You should still have a relational 
storage design within a relational storage engine such 
as SQL Server. 
Parameterized queries are exactly the same as stored 
procedures in terms of performance and memory 
management. Since most ORM tools can use either 
stored procedures or parameterized queries, be sure 
you’re coding to these constructs and not hard-coding 
values into your T-SQL queries. 
2
6 
Create, Read, Update, and Delete (CRUD) queries can 
all be generated from the ORM tool without the need 
for intervention. But, the Read queries generated are 
frequently very inefficient. Consider writing a stored 
procedure for complex Read queries. 
Since the code generated from the ORM can frequently 
be ad hoc, ensure that the SQL Server instance has 
‘Optimize for Ad Hoc’ enabled. This will store a plan 
stub in memory the first time a query is passed, rather 
than storing a full plan. This can help with memory 
management. 
Be sure your code is generating a parameter size 
equivalent to the data type defined within the table in 
the database. Some ORM tools size the parameter to 
the size of the value passed. This can lead to serious 
performance problems. 
3 
4 
5
7 
T-SQL Tips 
While much of your code may be generated, at least some of it will 
have to be written by hand. If you are writing some or all of your 
T-SQL code, these tips will help you avoid problems. 
SELECT * is not always a bad thing, but it’s a good idea 
to only move the data you really need to move and only 
when you really need it, in order to avoid network, disk, 
and memory contention on your server. 
Keep transactions as short as possible 
and never use them unnecessarily. 
The longer a lock is held the more 
likely it is that another user will be 
blocked. Never hold a transaction 
open after control is passed back 
to the application – use optimistic 
locking instead. 
6 
7 
For small sets of data that are 
infrequently updated such as lookup 
values, build a method of caching 
them in memory on your application 
server rather than constantly querying 
them in the database. 
8
8 
If doing processing within a transaction, leave the 
updates until last if possible, to minimize the need for 
exclusive locks. 
Cursors within SQL Server can cause severe 
performance bottlenecks. Avoid them. The WHILE loop 
within SQL Server is just as bad as a cursor. 
Ensure your variables and parameters are the same data 
types as the columns. An implicit or explicit conversion 
can lead to table scans and slow performance. 
9 
10 
11 
A function on columns in the WHERE clause or JOIN 
criteria means that SQL Server can’t use indexes 
appropriately and will lead to table scans and slow 
performance. 
12
9 
Don’t use DISTINCT, ORDER BY, or UNION 
unnecessarily. 
Table variables have no statistics within SQL Server. 
This makes them useful for working in situations where 
a statement level recompile can slow performance. But, 
that lack of statistics makes them very inefficient where 
you need to do searches or joins. Use table variables only 
where appropriate. 
13 
14 
Multi-statement user-defined functions work through 
table variables. Because of this, they also don’t work well 
in situations where statistics are required. If a join or 
filtering is required, avoid using them, especially if you 
are working with more than approximately 50 rows in 
the data set. 
15
10 
Query hints are actually commands to the query 
optimizer to control how a query plan is generated. 
These should be used very sparingly and only where 
appropriate. 
One of the most abused query hints is NO_LOCK. This 
can lead to extra or missing rows in data sets. Instead 
of using NO_LOCK consider using a snapshot isolation 
level such as READ_COMMITTED_SNAPSHOT. 
16 
17 
Avoid creating stored procedures that have a wide range 
of data supplied to them as parameters. These are 
compiled to use just one query plan. 18 
Try not to interleave data definition language with your 
data manipulation language queries within SQL Server. 
This can lead to recompiles which hurts performance. 19 
Temporary tables have statistics which get updated as 
data is inserted into them. As these updates occur, you 
can get recompiles. Where possible, substitute table 
variables to avoid this issue. 
20
11 
If possible, avoid NULL values in your database. If not, 
use the appropriate IS NULL and IS NOT NULL code. 21 
A view is meant to mask or modify how tables are 
presented to the end user. These are fine constructs. But 
when you start joining one view to another or nesting 
views within views, performance will suffer. Refer only 
to tables within a view. 
22 
Use extended 
events to monitor 
the queries in your 
system in order 
to identify slow 
running queries. 
If you’re on a 2005 
or earlier system 
you’ll need to use a 
server-side trace. 
24 
If you need to insert many rows at once into a table, 
use, where possible, the multi-row VALUES clause in 
INSERT statements. 23
12 
Index Tips 
Indexing tables is not an exact science. It requires some trial and 
error combined with lots of testing to get things just right. Even then, 
the performance metrics will change over time as you add more and 
more data. 
You get exactly one clustered index on a table. Ensure 
you have it in the right place. First choice is the most 
frequently accessed column, which may or may not 
be the primary key. Second choice is a column that 
structures the storage in a way that helps performance. 
This is a must for partitioning data. 
Clustered indexes work well on columns that are used 
a lot for ‘range’ WHERE clauses such as BETWEEN and 
LIKE, where it is frequently used in ORDER BY clauses 
or in GROUP BY clauses. 
25 
26
13 
If clustered indexes are narrow (involve few columns) 
then this will mean that less storage is needed for non-clustered 
indexes for that table. 
You do not have to make the primary key the clustered 
index. This is default behavior but can be directly 
controlled. 
You should have a clustered index on every table in 
the database. There are exceptions, but the exceptions 
should be exceptional. 
27 
28 
29 
Avoid using a column in a clustered index that has 
values that are frequently updated. 30
14 
Only create non-clustered indexes on tables when you 
know they’ll be used through testing. You can seriously 
hurt performance by creating too many indexes on a 
table. 
Keep your indexes as narrow as possible. This means 
reducing the number and size of the columns used in 
the index key. This helps make the index more efficient. 
31 
32 
Always index your foreign key columns if you are likely 
to delete rows from the referenced table. This avoids a 
table scan. 33
15 
A clustered index on a GUID can lead to serious 
fragmentation of the index due to the random 
nature of the GUID. You can use the function 
NEWSEQUENTIALID() to generate a GUID that will 
not lead to as much fragmentation. 
Performance is enhanced when indexes are placed on 
columns used in WHERE, JOIN, ORDER BY, GROUP, 
and TOP. Always test to ensure that the index does help 
performance. 
34 
35 
If a non-clustered index is useful to your queries, but 
doesn’t have all the columns needed by the query, you 
can consider using the INCLUDE option to store the 
extra columns at the leaf level of the index. 
36
16 
If temporary tables are in use, you can add indexes to 
those tables to enhance their performance. 
Where possible, make the indexes unique. This is 
especially true of the clustered index (one of the reasons 
that the primary key is by default clustered). A unique 
index absolutely performs faster than a non-unique 
index, even with the same values. 
37 
38 
Ensure that the selectivity of the data in the indexes is 
high. Very few unique values makes an index much less 
likely to be used well by the query optimizer. 
It is almost always better to let SQL Server update 
statistics automatically. 
39 
40
17 
Database Design Tips 
Again, you may not be delving much into this, but there are a few tips 
to keep in mind. 
While it is possible to over-normalize a database, 
under-normalization is much more prevalent. This 
leads to repetition of data, inefficient storage, and poor 
performance. Data normalization is a performance 
tuning technique as well as a storage mechanism. 
Referential integrity constraints such as foreign keys 
actually help performance, because the optimizer can 
recognize these enforced constraints and make better 
choices for joins and other data access. 
41 
42
18 
Make sure your database doesn’t hold ‘historic’ data 
that is no longer used. Archive it out, either into a 
special ‘archive’ database, a reporting OLAP database, 
or on file. Large tables mean longer table scans and 
deeper indexes. This in turn can mean that locks are 
held for longer. Admin tasks such as Statistics 
updates, DBCC checks, and index builds take longer, 
as do backups. 
Separate out the reporting functions from the OLTP 
production functions. OLTP databases usually have 
short transactions with a lot of updates whereas 
reporting databases, such as OLAP and data warehouse 
systems, have longer data-heavy queries. If possible, put 
them on different servers. 
44 
45 
Unique constraints also help performance because the 
optimizer can use these in different ways to enhance its 
query tuning. 43
19 
Tools 
SQL Prompt 
Write, refactor, and explore SQL effortlessly with this 
SQL Server Management Studio plug-in. 
SQL Compare 
Compare database schemas and deploy database 
schema changes. 
SQL Source Control 
Connect your databases to your version control system 
within SQL Server Management Studio. 
SQL Backup Pro 
Schedule backup jobs, verify with DBCC CHECKDB, and 
compress backups by up to 95%. 
SQL Index Manager 
Analyze, manage, and fix database index fragmentation.
20 
Books 
SQL Server Execution Plans 
by Grant Fritchey 
Learn the basics of capturing plans, how to interrupt 
them in their various forms, graphical or XML, and then 
how to use the information you find. Diagnose the most 
common causes of poor query performance so you 
can optimize your SQL queries and improve your 
indexing strategy. 
SQL Server Concurrency: Locking, Blocking 
and Row Versioning 
by Kalen Delaney 
Your application can have world-class indexes and 
queries, but they won’t help you if you can’t get your data 
because another application has it locked. That’s why 
every DBA and developer must understand SQL Server 
concurrency, and how to troubleshoot any issues. 
SQL Server Transaction Log Management 
by Tony Davis and Gail Shaw 
Tony Davis and Gail Shaw strive to offer just the right level 
of detail so that every DBA can perform all of the most 
important aspects of transaction log management.
21 
Troubleshooting SQL Server: A Guide for the 
Accidental DBA 
by Jonathan Kehayias and Ted Krueger 
Three SQL Server MVPs provide fascinating insight into 
the most common SQL Server problems, why they occur, 
and how they can be diagnosed using tools such as 
Performance Monitor, Dynamic Management Views, and 
server-side tracing performance, so you can optimize 
your SQL queries and improve your indexing strategy. 
Defensive Database Programming 
by Alex Kuznetsov 
The goal of defensive database programming is to 
help you to produce resilient T-SQL code that robustly 
and gracefully handles cases of unintended use, 
and is resilient to common changes to the database 
environment.

More Related Content

What's hot (6)

PPTX
Web Cloud Computing SQL Server - Ferrara University
antimo musone
 
PPT
Understanding System Performance
Teradata
 
PPT
Triad 2010 excel_chapter_1
Dalia Saeed
 
PPTX
SQL Server 2016 new features
SpanishPASSVC
 
PDF
Obiee11g working with partitions
Amit Sharma
 
DOCX
Learn SAS Programming
SASTechies
 
Web Cloud Computing SQL Server - Ferrara University
antimo musone
 
Understanding System Performance
Teradata
 
Triad 2010 excel_chapter_1
Dalia Saeed
 
SQL Server 2016 new features
SpanishPASSVC
 
Obiee11g working with partitions
Amit Sharma
 
Learn SAS Programming
SASTechies
 

Viewers also liked (20)

PPT
Showdown: IBM DB2 versus Oracle Database for OLTP
comahony
 
PPTX
Ibm db2 case study
Ishvitha Badhri
 
PDF
Concurrency in SQL Server (SQL Night #24)
Antonios Chatzipavlis
 
PPT
Business Case: IBM DB2 versus Oracle Database - Conor O'Mahony
comahony
 
PPT
Chapter 12 transactions and concurrency control
AbDul ThaYyal
 
PDF
Intranet query tuning (example)
쀑선 êłœ
 
PPTX
Grant Fritchey - Query Tuning In Azure SQL Database
Red Gate Software
 
PDF
Query Tuning Azure SQL Databases
Grant Fritchey
 
PDF
Microsoft SQL Server Query Tuning
Mark Ginnebaugh
 
ODP
Basic Query Tuning Primer - Pg West 2009
mattsmiley
 
PDF
SQL Server Query Tuning Tips - Get it Right the First Time
Dean Richards
 
PDF
Oracle Query Tuning Tips - Get it Right the First Time
Dean Richards
 
PDF
Redshift performance tuning
Carlos del Cacho
 
PPT
07 Using Oracle-Supported Package in Application Development
rehaniltifat
 
PDF
ŰȘŰ­Ù„ÛŒÙ„ ۭۧ۳ۧ۳ۧŰȘ ۯ۱ ŰŽŰšÚ©Ù‡ Ù‡Ű§ÛŒ ۧۏŰȘÙ…Ű§Űčی
Hamed Azizi
 
PPT
05 Creating Stored Procedures
rehaniltifat
 
PDF
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 
PPTX
Scientific Research Steps Part 1
Ainul Yaqin
 
DOC
Transcript of Presentation(Simple & Compound Interest) for Audience
rehaniltifat
 
PDF
Webinar 2013 advanced_query_tuning
晓 摹
 
Showdown: IBM DB2 versus Oracle Database for OLTP
comahony
 
Ibm db2 case study
Ishvitha Badhri
 
Concurrency in SQL Server (SQL Night #24)
Antonios Chatzipavlis
 
Business Case: IBM DB2 versus Oracle Database - Conor O'Mahony
comahony
 
Chapter 12 transactions and concurrency control
AbDul ThaYyal
 
Intranet query tuning (example)
쀑선 êłœ
 
Grant Fritchey - Query Tuning In Azure SQL Database
Red Gate Software
 
Query Tuning Azure SQL Databases
Grant Fritchey
 
Microsoft SQL Server Query Tuning
Mark Ginnebaugh
 
Basic Query Tuning Primer - Pg West 2009
mattsmiley
 
SQL Server Query Tuning Tips - Get it Right the First Time
Dean Richards
 
Oracle Query Tuning Tips - Get it Right the First Time
Dean Richards
 
Redshift performance tuning
Carlos del Cacho
 
07 Using Oracle-Supported Package in Application Development
rehaniltifat
 
ŰȘŰ­Ù„ÛŒÙ„ ۭۧ۳ۧ۳ۧŰȘ ۯ۱ ŰŽŰšÚ©Ù‡ Ù‡Ű§ÛŒ ۧۏŰȘÙ…Ű§Űčی
Hamed Azizi
 
05 Creating Stored Procedures
rehaniltifat
 
Oracle SQL Performance Tuning and Optimization v26 chapter 1
Kevin Meade
 
Scientific Research Steps Part 1
Ainul Yaqin
 
Transcript of Presentation(Simple & Compound Interest) for Audience
rehaniltifat
 
Webinar 2013 advanced_query_tuning
晓 摹
 
Ad

Similar to Tips for Database Performance (20)

PPTX
Query Optimization in SQL Server
Rajesh Gunasundaram
 
PPTX
Database Performance Tuning
Arno Huetter
 
PDF
Speed up sql
Kaing Menglieng
 
PDF
A Review of Data Access Optimization Techniques in a Distributed Database Man...
Editor IJCATR
 
PDF
A Review of Data Access Optimization Techniques in a Distributed Database Man...
Editor IJCATR
 
PPT
Performance Tuning And Optimization Microsoft SQL Database
Tung Nguyen Thanh
 
PPTX
Optimizing Application Performance - 2022.pptx
JasonTuran2
 
PPTX
SQL Server 2012 Best Practices
Microsoft TechNet - Belgium and Luxembourg
 
PDF
Making your RDBMS fast!
VictorSzoltysek
 
PPTX
Database optimization
EsraaAlattar1
 
PPTX
My Query is slow, now what?
Gianluca Sartori
 
PPT
Myth busters - performance tuning 102 2008
paulguerin
 
PPTX
Quick & Easy SQL Tips
Ike Ellis
 
PPTX
Sql good practices
Deepak Mehtani
 
PDF
Database development coding standards
Alessandro Baratella
 
PPTX
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
PDF
SQL Database Performance Tuning for Developers
BRIJESH KUMAR
 
PPTX
Sql performance tuning
Leo Mark Villar
 
PDF
Query Tuning for Database Pros & Developers
Code Mastery
 
PDF
Sql db optimization
Nikhildas P C
 
Query Optimization in SQL Server
Rajesh Gunasundaram
 
Database Performance Tuning
Arno Huetter
 
Speed up sql
Kaing Menglieng
 
A Review of Data Access Optimization Techniques in a Distributed Database Man...
Editor IJCATR
 
A Review of Data Access Optimization Techniques in a Distributed Database Man...
Editor IJCATR
 
Performance Tuning And Optimization Microsoft SQL Database
Tung Nguyen Thanh
 
Optimizing Application Performance - 2022.pptx
JasonTuran2
 
SQL Server 2012 Best Practices
Microsoft TechNet - Belgium and Luxembourg
 
Making your RDBMS fast!
VictorSzoltysek
 
Database optimization
EsraaAlattar1
 
My Query is slow, now what?
Gianluca Sartori
 
Myth busters - performance tuning 102 2008
paulguerin
 
Quick & Easy SQL Tips
Ike Ellis
 
Sql good practices
Deepak Mehtani
 
Database development coding standards
Alessandro Baratella
 
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
SQL Database Performance Tuning for Developers
BRIJESH KUMAR
 
Sql performance tuning
Leo Mark Villar
 
Query Tuning for Database Pros & Developers
Code Mastery
 
Sql db optimization
Nikhildas P C
 
Ad

More from Kesavan Munuswamy (18)

PDF
Surviving Migration To Office 365 an it pros guide ebook
Kesavan Munuswamy
 
PDF
Windows_Server_2016_Virtualization White Paper By Veeam
Kesavan Munuswamy
 
PDF
MSFT Cloud Architecture Information Protection
Kesavan Munuswamy
 
PDF
Ms cloud identity and access infographic 2015
Kesavan Munuswamy
 
PDF
Ms cloud design patterns infographic 2015
Kesavan Munuswamy
 
PDF
Microsoft azure infographic 2015 2.5
Kesavan Munuswamy
 
PDF
Cloud Design Patterns Book from Microsoft
Kesavan Munuswamy
 
PDF
Azure security infographic 2014 sec
Kesavan Munuswamy
 
PDF
MCSA Exam paper 70-412 PDF
Kesavan Munuswamy
 
PDF
Windows Server 2012 Exam Paper 70-411 PDF
Kesavan Munuswamy
 
PDF
MCSA Server 2012 Exam Paper 1- Ms 70 410
Kesavan Munuswamy
 
PDF
Inside the sql server query optimizer
Kesavan Munuswamy
 
PDF
Step by Step Windows Azure pack for windows server 2012 R2 Guide v1
Kesavan Munuswamy
 
PDF
SQL Server Source Control Basics
Kesavan Munuswamy
 
PDF
Fundamentals of sql server 2012 replication e book
Kesavan Munuswamy
 
PDF
SQL Server Backup and Restore
Kesavan Munuswamy
 
PDF
Extending Role Security in Analysis Services for SQL Server
Kesavan Munuswamy
 
PDF
SQL High Availability solutions E Book
Kesavan Munuswamy
 
Surviving Migration To Office 365 an it pros guide ebook
Kesavan Munuswamy
 
Windows_Server_2016_Virtualization White Paper By Veeam
Kesavan Munuswamy
 
MSFT Cloud Architecture Information Protection
Kesavan Munuswamy
 
Ms cloud identity and access infographic 2015
Kesavan Munuswamy
 
Ms cloud design patterns infographic 2015
Kesavan Munuswamy
 
Microsoft azure infographic 2015 2.5
Kesavan Munuswamy
 
Cloud Design Patterns Book from Microsoft
Kesavan Munuswamy
 
Azure security infographic 2014 sec
Kesavan Munuswamy
 
MCSA Exam paper 70-412 PDF
Kesavan Munuswamy
 
Windows Server 2012 Exam Paper 70-411 PDF
Kesavan Munuswamy
 
MCSA Server 2012 Exam Paper 1- Ms 70 410
Kesavan Munuswamy
 
Inside the sql server query optimizer
Kesavan Munuswamy
 
Step by Step Windows Azure pack for windows server 2012 R2 Guide v1
Kesavan Munuswamy
 
SQL Server Source Control Basics
Kesavan Munuswamy
 
Fundamentals of sql server 2012 replication e book
Kesavan Munuswamy
 
SQL Server Backup and Restore
Kesavan Munuswamy
 
Extending Role Security in Analysis Services for SQL Server
Kesavan Munuswamy
 
SQL High Availability solutions E Book
Kesavan Munuswamy
 

Recently uploaded (20)

PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PPTX
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 

Tips for Database Performance

  • 1. 1
  • 2. 2 PHIL FACTOR GRANT FRITCHEY K. BRIAN KELLEY MICKEY STUEWE IKE ELLIS JONATHAN ALLEN LOUIS DAVIDSON
  • 3. Database Performance Tips for Developers As a developer, you may or may not need to go into the database and write queries, or design tables and indexes, or help determine configuration of your SQL Server systems. But if you do, these tips should help to make that a more pain free process.
  • 4. 4 Contents Tips 1-5 ORM Tips Tips 6-24 T-SQL Tips Tips 25-40 Index Tips Tips 41-45 Database Design Tips 5 7 12 17
  • 5. 5 ORM Tips More and more people are using Object to Relational Mapping (ORM) tools to jump the divide between application code that is object oriented and a database that stores information in a relational manner. These tools are excellent and radically improve development speed, but there are a few ‘gotchas’ to know about. 1 Avoid following the ‘Hello World’ examples provided with your ORM tool that turns it into an Object to Object Mapping. Database storage is not the same as objects for a reason. You should still have a relational storage design within a relational storage engine such as SQL Server. Parameterized queries are exactly the same as stored procedures in terms of performance and memory management. Since most ORM tools can use either stored procedures or parameterized queries, be sure you’re coding to these constructs and not hard-coding values into your T-SQL queries. 2
  • 6. 6 Create, Read, Update, and Delete (CRUD) queries can all be generated from the ORM tool without the need for intervention. But, the Read queries generated are frequently very inefficient. Consider writing a stored procedure for complex Read queries. Since the code generated from the ORM can frequently be ad hoc, ensure that the SQL Server instance has ‘Optimize for Ad Hoc’ enabled. This will store a plan stub in memory the first time a query is passed, rather than storing a full plan. This can help with memory management. Be sure your code is generating a parameter size equivalent to the data type defined within the table in the database. Some ORM tools size the parameter to the size of the value passed. This can lead to serious performance problems. 3 4 5
  • 7. 7 T-SQL Tips While much of your code may be generated, at least some of it will have to be written by hand. If you are writing some or all of your T-SQL code, these tips will help you avoid problems. SELECT * is not always a bad thing, but it’s a good idea to only move the data you really need to move and only when you really need it, in order to avoid network, disk, and memory contention on your server. Keep transactions as short as possible and never use them unnecessarily. The longer a lock is held the more likely it is that another user will be blocked. Never hold a transaction open after control is passed back to the application – use optimistic locking instead. 6 7 For small sets of data that are infrequently updated such as lookup values, build a method of caching them in memory on your application server rather than constantly querying them in the database. 8
  • 8. 8 If doing processing within a transaction, leave the updates until last if possible, to minimize the need for exclusive locks. Cursors within SQL Server can cause severe performance bottlenecks. Avoid them. The WHILE loop within SQL Server is just as bad as a cursor. Ensure your variables and parameters are the same data types as the columns. An implicit or explicit conversion can lead to table scans and slow performance. 9 10 11 A function on columns in the WHERE clause or JOIN criteria means that SQL Server can’t use indexes appropriately and will lead to table scans and slow performance. 12
  • 9. 9 Don’t use DISTINCT, ORDER BY, or UNION unnecessarily. Table variables have no statistics within SQL Server. This makes them useful for working in situations where a statement level recompile can slow performance. But, that lack of statistics makes them very inefficient where you need to do searches or joins. Use table variables only where appropriate. 13 14 Multi-statement user-defined functions work through table variables. Because of this, they also don’t work well in situations where statistics are required. If a join or filtering is required, avoid using them, especially if you are working with more than approximately 50 rows in the data set. 15
  • 10. 10 Query hints are actually commands to the query optimizer to control how a query plan is generated. These should be used very sparingly and only where appropriate. One of the most abused query hints is NO_LOCK. This can lead to extra or missing rows in data sets. Instead of using NO_LOCK consider using a snapshot isolation level such as READ_COMMITTED_SNAPSHOT. 16 17 Avoid creating stored procedures that have a wide range of data supplied to them as parameters. These are compiled to use just one query plan. 18 Try not to interleave data definition language with your data manipulation language queries within SQL Server. This can lead to recompiles which hurts performance. 19 Temporary tables have statistics which get updated as data is inserted into them. As these updates occur, you can get recompiles. Where possible, substitute table variables to avoid this issue. 20
  • 11. 11 If possible, avoid NULL values in your database. If not, use the appropriate IS NULL and IS NOT NULL code. 21 A view is meant to mask or modify how tables are presented to the end user. These are fine constructs. But when you start joining one view to another or nesting views within views, performance will suffer. Refer only to tables within a view. 22 Use extended events to monitor the queries in your system in order to identify slow running queries. If you’re on a 2005 or earlier system you’ll need to use a server-side trace. 24 If you need to insert many rows at once into a table, use, where possible, the multi-row VALUES clause in INSERT statements. 23
  • 12. 12 Index Tips Indexing tables is not an exact science. It requires some trial and error combined with lots of testing to get things just right. Even then, the performance metrics will change over time as you add more and more data. You get exactly one clustered index on a table. Ensure you have it in the right place. First choice is the most frequently accessed column, which may or may not be the primary key. Second choice is a column that structures the storage in a way that helps performance. This is a must for partitioning data. Clustered indexes work well on columns that are used a lot for ‘range’ WHERE clauses such as BETWEEN and LIKE, where it is frequently used in ORDER BY clauses or in GROUP BY clauses. 25 26
  • 13. 13 If clustered indexes are narrow (involve few columns) then this will mean that less storage is needed for non-clustered indexes for that table. You do not have to make the primary key the clustered index. This is default behavior but can be directly controlled. You should have a clustered index on every table in the database. There are exceptions, but the exceptions should be exceptional. 27 28 29 Avoid using a column in a clustered index that has values that are frequently updated. 30
  • 14. 14 Only create non-clustered indexes on tables when you know they’ll be used through testing. You can seriously hurt performance by creating too many indexes on a table. Keep your indexes as narrow as possible. This means reducing the number and size of the columns used in the index key. This helps make the index more efficient. 31 32 Always index your foreign key columns if you are likely to delete rows from the referenced table. This avoids a table scan. 33
  • 15. 15 A clustered index on a GUID can lead to serious fragmentation of the index due to the random nature of the GUID. You can use the function NEWSEQUENTIALID() to generate a GUID that will not lead to as much fragmentation. Performance is enhanced when indexes are placed on columns used in WHERE, JOIN, ORDER BY, GROUP, and TOP. Always test to ensure that the index does help performance. 34 35 If a non-clustered index is useful to your queries, but doesn’t have all the columns needed by the query, you can consider using the INCLUDE option to store the extra columns at the leaf level of the index. 36
  • 16. 16 If temporary tables are in use, you can add indexes to those tables to enhance their performance. Where possible, make the indexes unique. This is especially true of the clustered index (one of the reasons that the primary key is by default clustered). A unique index absolutely performs faster than a non-unique index, even with the same values. 37 38 Ensure that the selectivity of the data in the indexes is high. Very few unique values makes an index much less likely to be used well by the query optimizer. It is almost always better to let SQL Server update statistics automatically. 39 40
  • 17. 17 Database Design Tips Again, you may not be delving much into this, but there are a few tips to keep in mind. While it is possible to over-normalize a database, under-normalization is much more prevalent. This leads to repetition of data, inefficient storage, and poor performance. Data normalization is a performance tuning technique as well as a storage mechanism. Referential integrity constraints such as foreign keys actually help performance, because the optimizer can recognize these enforced constraints and make better choices for joins and other data access. 41 42
  • 18. 18 Make sure your database doesn’t hold ‘historic’ data that is no longer used. Archive it out, either into a special ‘archive’ database, a reporting OLAP database, or on file. Large tables mean longer table scans and deeper indexes. This in turn can mean that locks are held for longer. Admin tasks such as Statistics updates, DBCC checks, and index builds take longer, as do backups. Separate out the reporting functions from the OLTP production functions. OLTP databases usually have short transactions with a lot of updates whereas reporting databases, such as OLAP and data warehouse systems, have longer data-heavy queries. If possible, put them on different servers. 44 45 Unique constraints also help performance because the optimizer can use these in different ways to enhance its query tuning. 43
  • 19. 19 Tools SQL Prompt Write, refactor, and explore SQL effortlessly with this SQL Server Management Studio plug-in. SQL Compare Compare database schemas and deploy database schema changes. SQL Source Control Connect your databases to your version control system within SQL Server Management Studio. SQL Backup Pro Schedule backup jobs, verify with DBCC CHECKDB, and compress backups by up to 95%. SQL Index Manager Analyze, manage, and fix database index fragmentation.
  • 20. 20 Books SQL Server Execution Plans by Grant Fritchey Learn the basics of capturing plans, how to interrupt them in their various forms, graphical or XML, and then how to use the information you find. Diagnose the most common causes of poor query performance so you can optimize your SQL queries and improve your indexing strategy. SQL Server Concurrency: Locking, Blocking and Row Versioning by Kalen Delaney Your application can have world-class indexes and queries, but they won’t help you if you can’t get your data because another application has it locked. That’s why every DBA and developer must understand SQL Server concurrency, and how to troubleshoot any issues. SQL Server Transaction Log Management by Tony Davis and Gail Shaw Tony Davis and Gail Shaw strive to offer just the right level of detail so that every DBA can perform all of the most important aspects of transaction log management.
  • 21. 21 Troubleshooting SQL Server: A Guide for the Accidental DBA by Jonathan Kehayias and Ted Krueger Three SQL Server MVPs provide fascinating insight into the most common SQL Server problems, why they occur, and how they can be diagnosed using tools such as Performance Monitor, Dynamic Management Views, and server-side tracing performance, so you can optimize your SQL queries and improve your indexing strategy. Defensive Database Programming by Alex Kuznetsov The goal of defensive database programming is to help you to produce resilient T-SQL code that robustly and gracefully handles cases of unintended use, and is resilient to common changes to the database environment.