SlideShare a Scribd company logo
1 of 39
Download to read offline
ColdFusion Summit 2016
Building Better
SQL Server Databases
Who is this guy?
Eric Cobb
- Started in IT in 1999 as a "webmaster“
- Developer for 14 years
- Microsoft Certified Solutions Expert (MCSE)
- Data Platform
- Data Management and Analytics
- Now a full time DBA
Blog: http://www.sqlnuggets.com
Blog Twitter: @sqlnugg
Personal Twitter: @cfgears
What are we going to learn?
• How SQL Server stores and retrieves data
• How your database design can impact resources and performance
• Design tips to incorporate into your database development
• Common T-SQL mistakes and things to avoid
• Primarily focusing on OLTP databases
A PEEK UNDERTHE HOOD OF SQL SERVER
A BRIEF OVERVIEW OF HOW SQL SERVER STORES AND RETRIEVES DATA
A Peek UnderThe Hood
Tables & Data Pages
A Peek UnderThe Hood
Tables & Data Pages
A Peek UnderThe Hood
Process of Requesting Data
A Peek UnderThe Hood
Process of Requesting Data
A Peek UnderThe Hood
Process of Requesting Data
A Peek UnderThe Hood
Process of Requesting Data
A Peek UnderThe Hood
Storing Data in Pages
A Peek UnderThe Hood
Storing Data in Pages
A Peek UnderThe Hood
Storing Data in Pages
How is the data stored in a Page?
Unordered (Heap)
• Query optimizer reads all the rows in the table (table scan), to find the rows that meet the criteria of a
query
• A table scan generates many disk I/O operations and can be resource intensive
• Heaps should generally be avoided, although can be useful when inserting large amounts of data in
ETL/Bulk processes
Ordered (Clustered Index)
• Tells SQL Server how to physically sort the records on disk
• The most important index you can apply to a table
• Data pages are ordered, for faster data retrieval
• Query optimizer searches the index key columns and finds the location of the rows needed by the query
• Searching the index is much faster than scanning the entire table
• There is only ever 1 clustered index on a table
A Peek UnderThe Hood
Storing Data in Pages
How do I create Clustered Indexes?
Primary Key = Clustered Index (usually)
• SQL Server automatically creates a clustered index on your Primary Key column if a clustered index does
not already exist on the table
• If you do not want the Primary Key to be your Clustered Index, you can create your Clustered Index on a
different column
Clustered Index (Primary Key) Tips:
• Use a naturally occurring incremental value
• Keep as small and narrow as possible (single columns are preferred)
• Avoid using character data types for a Clustered Index
A Peek UnderThe Hood
Storing Data in Pages
A Peek UnderThe Hood
Page Splits
A Peek UnderThe Hood
Page Splits
A Peek UnderThe Hood
Page Splits
A Peek UnderThe Hood
Page Splits
A Peek UnderThe Hood
Page Splits
A Peek UnderThe Hood
Page Splits
A Peek UnderThe Hood
Page Splits
How can we avoid Page Splits?
You can’t avoid them, but you can minimize them with good table designs
• Choose a good Clustered Index (Primary Key) for your table
• Should be unique, narrow, static, and incremental
• Good Clustered Index examples:
• A numeric identity column (smallint, int, bigint)
• A composite key of date and identity – in that order (date, identity)
• A pseudo sequential GUID (using the NEWSEQUENTIALID() function in SQL Server)
• Not recommended, but the best you can do if you absolutely have to use a GUID
• Clustered Indexes to avoid:
• Unique Identifier (GUID) generate from an application or with SQL Server’s NEWID() function
• Character columns (CHAR, VARCHAR, NVARCHAR, etc…)
• Combination of multiple character columns (LastName, FirstName, MiddlieInitial)
• Columns that undergo frequent changes
A Peek UnderThe Hood
Overflow Pages
A Peek UnderThe Hood
Overflow Pages
A Peek UnderThe Hood
Overflow Pages
BUILDING BETTERTABLES
DESIGNING TABLES WITH EFFICIENCY IN MIND
Building BetterTables
UsingThe Right DataTypes
Data Types Are Important!
Choose your table column data types wisely
• They can affect the performance of your database as it grows
Know your data, use the appropriate data type for the data you are storing
• The more accurate your data type is, the more efficiently SQL Server can handle your data.
Use the smallest data type possible (within reason)
• The smaller the column, the less data you have to store and retrieve, which leads to faster reads and writes
• The longest city name in the U.S. is Rancho Santa Margarita, California; it’s 22 chars, don’t use VARCHAR(MAX)
• The true name of Bangkok, Thailand is: Krungthepmahanakhon Amonrattanakosin Mahintharayutthaya
Mahadilokphop Noppharatratchathaniburirom Udomratchaniwetmahasathan Amonphimanawatansathit
Sakkathattiyawitsanukamprasit. (176 chars)
Building BetterTables
UsingThe Right DataTypes
CHAR vs VARCHAR
• CHAR(n): Fixed-length string data, and the storage size is n bytes.
• VARCHAR(n): Variable-length string data, the storage size is the actual length of the data entered + 2
bytes.
• If you know the length of the string will always be the same, use CHAR to avoid the additional 2 bytes
added to every VARCHAR record
NCHAR vs NVARCHAR
• If you have databases that support multiple languages, consider using the Unicode NCHAR or
NVARCHAR data types to minimize character conversion issues
• Carefully evaluate whether you really need NCHAR or NVARCHAR
• NCHAR(n): Fixed-length Unicode string data, and the storage size is two times n bytes
• NVARCHAR(n): Variable-length Unicode string data, and the storage size, in bytes, is two times the
actual length of data entered + 2 bytes
Building BetterTables
UsingThe Right DataTypes
DECLARE
@var1 CHAR(10) = 'abc',
@var2 NCHAR(10) = 'abc',
@var3 VARCHAR(10) = 'abc',
@var4 NVARCHAR(10) = 'abc'
SELECT
DATALENGTH(@var1) AS [char],
DATALENGTH(@var2) AS [nchar],
DATALENGTH(@var3) AS [varchar],
DATALENGTH(@var4) AS [nvarchar]
Building BetterTables
UsingThe Right DataTypes
Numeric Data Types
Choose the appropriate Data Type for the range of numbers you will be storing
Data Type Range Storage
BIGINT -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 (Quintillion)
8 Bytes
INT -2,147,483,648 to 2,147,483,647 (Billion) 4 Bytes
SMALLINT -32,768 to 32,767 2 Bytes
TINYINT 0 to 255 1 Byte
Building BetterTables
UsingThe Right DataTypes
Date and Time Data Types
Choose the appropriate Data Type for the range of dates you will be storing
Data Type Range Storage
TIME 00:00:00.0000000 through 23:59:59.9999999 3 - 5 Bytes
DATE 0001-01-01 through 9999-12-31 3 Bytes
SMALLDATETIME 1900-01-01 through 2079-06-06 4 Bytes
DATETIME 1753-01-01 through 9999-12-31 8 Bytes
Building BetterTables
UsingThe Right DataTypes
Why does this matter?
Performance
• Smaller data sets = faster queries
• Optimized data pages = optimized resource usage (Remember the Buffer Pool?)
Scalability
• Helps you build better, more scalable applications
• Don’t think in terms of 1 row of data, think about millions
• Scalable applications do not happen by accident
Time spent on proper database design is well worth it
• Minor changes can have a major impact
• It can take more effort to rebuild an existing application than it does to originally design one correctly
Building BetterTables
UsingThe Right DataTypes
Why does this matter?
* Taken from Kimberly Tripp’s Pluralsight Course:
SQL Server: Why Physical Database Design Matters
http://www.pluralsight.com/courses/sqlserver-why-physical-db-design-matters
T-SQLTIPS
T-SQL HABITS THAT CAN CAUSE PROBLEMS
T-SQLTips
NOLOCK
Allows a Dirty Read
• Does not issue locks to prevent other transactions from modifying data being read
• Allows other transactions to modify the data while you’re trying to read it
• Data returned to the SELECT statement may or may not actually exist in the database, and in some cases it may
cause a query to return the same row multiple times or even skip rows
When should I use NOLOCK?
• If your query doesn’t necessarily need to return precise figures, and can tolerate some inconsistencies
• If you are querying data that does not get modified often
But NOLOCK makes my query faster!
• It makes your query faster because it is ignoring the safeguards put in place to ensure that your query is returning
accurate data
If you need 100% accurate results from your query, do not use NOLOCK
T-SQLTips
Stored Procedures
Do not name your stored procedures with the “sp_” prefix!
• This is reserved for system stored procedures
• SQL Server first checks the Master database for these procedures
Use SET NOCOUNT
• Can improve stored procedure performance
• Turns off the messages that SQL Server sends back to the client after each T-SQL statement is executed
T-SQLTips
Query Performance Killers
• Try not to use ORDER BY or DISTINCT in your queries, as it adds a lot of extra overhead. It is
more efficient to sort/filter the data in your application
• Format Dates in your application instead of in your SELECT statements
• Avoid Cursors and Loops in your T-SQL statements, as it forces row-by-row operations
• Using Functions in WHERE clauses and JOINS should be avoided
• Do Not Use SELECT *
• Can cause the optimizer to ignore indexes on the table, forcing a full table scan
• The more fields you return, the worse your performance is going to be (especially when ordering)
• Avoid Data Type Mismatches (aka Implicit Conversions)
• Variables used in WHERE clauses should match the data type of the columns they’re compared with
• Columns used in JOIN conditions should have matching data types
Thank you!

More Related Content

What's hot

Zapping ever faster: how Zap sped up by two orders of magnitude using RavenDB
Zapping ever faster: how Zap sped up by two orders of magnitude using RavenDBZapping ever faster: how Zap sped up by two orders of magnitude using RavenDB
Zapping ever faster: how Zap sped up by two orders of magnitude using RavenDBOren Eini
 
Javascript on Server-Side
Javascript on Server-SideJavascript on Server-Side
Javascript on Server-SideASIMYILDIZ
 
Lessons from the Trenches - Building Enterprise Applications with RavenDB
Lessons from the Trenches - Building Enterprise Applications with RavenDBLessons from the Trenches - Building Enterprise Applications with RavenDB
Lessons from the Trenches - Building Enterprise Applications with RavenDBOren Eini
 
SenchaCon 2016: Oracle Forms Modernisation - Owen Pagan
SenchaCon 2016: Oracle Forms Modernisation - Owen PaganSenchaCon 2016: Oracle Forms Modernisation - Owen Pagan
SenchaCon 2016: Oracle Forms Modernisation - Owen PaganSencha
 
Apache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeApache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeAndrus Adamchik
 
I3 - Running SharePoint 2016 in Azure the do's and dont's - Jasjit Chopra
I3 - Running SharePoint 2016 in Azure the do's and dont's - Jasjit ChopraI3 - Running SharePoint 2016 in Azure the do's and dont's - Jasjit Chopra
I3 - Running SharePoint 2016 in Azure the do's and dont's - Jasjit ChopraSPS Paris
 
Making sense of Microsoft Identities in a Hybrid world
Making sense of Microsoft Identities in a Hybrid worldMaking sense of Microsoft Identities in a Hybrid world
Making sense of Microsoft Identities in a Hybrid worldJason Himmelstein
 
How Shopify Scales Rails
How Shopify Scales RailsHow Shopify Scales Rails
How Shopify Scales Railsjduff
 
Building & Testing Scalable Rails Applications
Building & Testing Scalable Rails ApplicationsBuilding & Testing Scalable Rails Applications
Building & Testing Scalable Rails Applicationsevilmike
 
Adobe AEM for Business Heads
Adobe AEM for Business HeadsAdobe AEM for Business Heads
Adobe AEM for Business HeadsYash Mody
 
Intro to SPA using JavaScript & ASP.NET
Intro to SPA using JavaScript & ASP.NETIntro to SPA using JavaScript & ASP.NET
Intro to SPA using JavaScript & ASP.NETAlan Hecht
 
PowerShell for the Anxious ITPro
PowerShell for the Anxious ITProPowerShell for the Anxious ITPro
PowerShell for the Anxious ITProJason Himmelstein
 
The Need For Speed - NxtGen Cambridge
The Need For Speed - NxtGen CambridgeThe Need For Speed - NxtGen Cambridge
The Need For Speed - NxtGen CambridgePhil Pursglove
 
Navigating the turbulence on takeoff: Setting up SharePoint on Azure IaaS the...
Navigating the turbulence on takeoff: Setting up SharePoint on Azure IaaS the...Navigating the turbulence on takeoff: Setting up SharePoint on Azure IaaS the...
Navigating the turbulence on takeoff: Setting up SharePoint on Azure IaaS the...Jason Himmelstein
 
Why real integration developers ride Camels
Why real integration developers ride CamelsWhy real integration developers ride Camels
Why real integration developers ride CamelsChristian Posta
 
SenchaCon 2016: Turbocharge your Ext JS App - Per Minborg, Anselm McClain, Jo...
SenchaCon 2016: Turbocharge your Ext JS App - Per Minborg, Anselm McClain, Jo...SenchaCon 2016: Turbocharge your Ext JS App - Per Minborg, Anselm McClain, Jo...
SenchaCon 2016: Turbocharge your Ext JS App - Per Minborg, Anselm McClain, Jo...Sencha
 
O365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
O365Con18 - Automate your Tasks through Azure Functions - Elio StruyfO365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
O365Con18 - Automate your Tasks through Azure Functions - Elio StruyfNCCOMMS
 
Building Ext JS Using HATEOAS - Jeff Stano
Building Ext JS Using HATEOAS - Jeff StanoBuilding Ext JS Using HATEOAS - Jeff Stano
Building Ext JS Using HATEOAS - Jeff StanoSencha
 

What's hot (20)

Zapping ever faster: how Zap sped up by two orders of magnitude using RavenDB
Zapping ever faster: how Zap sped up by two orders of magnitude using RavenDBZapping ever faster: how Zap sped up by two orders of magnitude using RavenDB
Zapping ever faster: how Zap sped up by two orders of magnitude using RavenDB
 
Javascript on Server-Side
Javascript on Server-SideJavascript on Server-Side
Javascript on Server-Side
 
Building Advanced RESTFul services
Building Advanced RESTFul servicesBuilding Advanced RESTFul services
Building Advanced RESTFul services
 
RavenDB 3.5
RavenDB 3.5RavenDB 3.5
RavenDB 3.5
 
Lessons from the Trenches - Building Enterprise Applications with RavenDB
Lessons from the Trenches - Building Enterprise Applications with RavenDBLessons from the Trenches - Building Enterprise Applications with RavenDB
Lessons from the Trenches - Building Enterprise Applications with RavenDB
 
SenchaCon 2016: Oracle Forms Modernisation - Owen Pagan
SenchaCon 2016: Oracle Forms Modernisation - Owen PaganSenchaCon 2016: Oracle Forms Modernisation - Owen Pagan
SenchaCon 2016: Oracle Forms Modernisation - Owen Pagan
 
Apache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeApache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM Alternative
 
I3 - Running SharePoint 2016 in Azure the do's and dont's - Jasjit Chopra
I3 - Running SharePoint 2016 in Azure the do's and dont's - Jasjit ChopraI3 - Running SharePoint 2016 in Azure the do's and dont's - Jasjit Chopra
I3 - Running SharePoint 2016 in Azure the do's and dont's - Jasjit Chopra
 
Making sense of Microsoft Identities in a Hybrid world
Making sense of Microsoft Identities in a Hybrid worldMaking sense of Microsoft Identities in a Hybrid world
Making sense of Microsoft Identities in a Hybrid world
 
How Shopify Scales Rails
How Shopify Scales RailsHow Shopify Scales Rails
How Shopify Scales Rails
 
Building & Testing Scalable Rails Applications
Building & Testing Scalable Rails ApplicationsBuilding & Testing Scalable Rails Applications
Building & Testing Scalable Rails Applications
 
Adobe AEM for Business Heads
Adobe AEM for Business HeadsAdobe AEM for Business Heads
Adobe AEM for Business Heads
 
Intro to SPA using JavaScript & ASP.NET
Intro to SPA using JavaScript & ASP.NETIntro to SPA using JavaScript & ASP.NET
Intro to SPA using JavaScript & ASP.NET
 
PowerShell for the Anxious ITPro
PowerShell for the Anxious ITProPowerShell for the Anxious ITPro
PowerShell for the Anxious ITPro
 
The Need For Speed - NxtGen Cambridge
The Need For Speed - NxtGen CambridgeThe Need For Speed - NxtGen Cambridge
The Need For Speed - NxtGen Cambridge
 
Navigating the turbulence on takeoff: Setting up SharePoint on Azure IaaS the...
Navigating the turbulence on takeoff: Setting up SharePoint on Azure IaaS the...Navigating the turbulence on takeoff: Setting up SharePoint on Azure IaaS the...
Navigating the turbulence on takeoff: Setting up SharePoint on Azure IaaS the...
 
Why real integration developers ride Camels
Why real integration developers ride CamelsWhy real integration developers ride Camels
Why real integration developers ride Camels
 
SenchaCon 2016: Turbocharge your Ext JS App - Per Minborg, Anselm McClain, Jo...
SenchaCon 2016: Turbocharge your Ext JS App - Per Minborg, Anselm McClain, Jo...SenchaCon 2016: Turbocharge your Ext JS App - Per Minborg, Anselm McClain, Jo...
SenchaCon 2016: Turbocharge your Ext JS App - Per Minborg, Anselm McClain, Jo...
 
O365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
O365Con18 - Automate your Tasks through Azure Functions - Elio StruyfO365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
O365Con18 - Automate your Tasks through Azure Functions - Elio Struyf
 
Building Ext JS Using HATEOAS - Jeff Stano
Building Ext JS Using HATEOAS - Jeff StanoBuilding Ext JS Using HATEOAS - Jeff Stano
Building Ext JS Using HATEOAS - Jeff Stano
 

Viewers also liked

Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectColdFusionConference
 
Security And Access Control For APIS using CF API Manager
Security And Access Control For APIS using CF API ManagerSecurity And Access Control For APIS using CF API Manager
Security And Access Control For APIS using CF API ManagerColdFusionConference
 
Monetizing Business Models: ColdFusion and APIS
Monetizing Business Models: ColdFusion and APISMonetizing Business Models: ColdFusion and APIS
Monetizing Business Models: ColdFusion and APISColdFusionConference
 
Developer Insights for Application Upgrade to ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016Developer Insights for Application Upgrade to ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016ColdFusionConference
 
ColdFusion Keynote: Building the Agile Web Since 1995
ColdFusion Keynote: Building the Agile Web Since 1995ColdFusion Keynote: Building the Agile Web Since 1995
ColdFusion Keynote: Building the Agile Web Since 1995ColdFusionConference
 
Become a Security Rockstar with ColdFusion 2016
Become a Security Rockstar with ColdFusion 2016Become a Security Rockstar with ColdFusion 2016
Become a Security Rockstar with ColdFusion 2016ColdFusionConference
 
Super Fast Application development with Mura CMS
Super Fast Application development with Mura CMSSuper Fast Application development with Mura CMS
Super Fast Application development with Mura CMSColdFusionConference
 
Build your own secure and real-time dashboard for mobile and web
Build your own secure and real-time dashboard for mobile and webBuild your own secure and real-time dashboard for mobile and web
Build your own secure and real-time dashboard for mobile and webColdFusionConference
 
10 Reasons ColdFusion PDFs should rule the world
10 Reasons ColdFusion PDFs should rule the world10 Reasons ColdFusion PDFs should rule the world
10 Reasons ColdFusion PDFs should rule the worldColdFusionConference
 

Viewers also liked (19)

Api manager preconference
Api manager preconferenceApi manager preconference
Api manager preconference
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an Architect
 
Don't just pdf, Smart PDF
Don't just pdf, Smart PDFDon't just pdf, Smart PDF
Don't just pdf, Smart PDF
 
Cf ppt vsr
Cf ppt vsrCf ppt vsr
Cf ppt vsr
 
ColdFusion in Transit action
ColdFusion in Transit actionColdFusion in Transit action
ColdFusion in Transit action
 
Security And Access Control For APIS using CF API Manager
Security And Access Control For APIS using CF API ManagerSecurity And Access Control For APIS using CF API Manager
Security And Access Control For APIS using CF API Manager
 
Monetizing Business Models: ColdFusion and APIS
Monetizing Business Models: ColdFusion and APISMonetizing Business Models: ColdFusion and APIS
Monetizing Business Models: ColdFusion and APIS
 
Developer Insights for Application Upgrade to ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016Developer Insights for Application Upgrade to ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016
 
Where is cold fusion headed
Where is cold fusion headedWhere is cold fusion headed
Where is cold fusion headed
 
ColdFusion Keynote: Building the Agile Web Since 1995
ColdFusion Keynote: Building the Agile Web Since 1995ColdFusion Keynote: Building the Agile Web Since 1995
ColdFusion Keynote: Building the Agile Web Since 1995
 
Become a Security Rockstar with ColdFusion 2016
Become a Security Rockstar with ColdFusion 2016Become a Security Rockstar with ColdFusion 2016
Become a Security Rockstar with ColdFusion 2016
 
Testing automaton
Testing automatonTesting automaton
Testing automaton
 
Super Fast Application development with Mura CMS
Super Fast Application development with Mura CMSSuper Fast Application development with Mura CMS
Super Fast Application development with Mura CMS
 
Securing applications
Securing applicationsSecuring applications
Securing applications
 
Why Everyone else writes bad code
Why Everyone else writes bad codeWhy Everyone else writes bad code
Why Everyone else writes bad code
 
Instant ColdFusion with Vagrant
Instant ColdFusion with VagrantInstant ColdFusion with Vagrant
Instant ColdFusion with Vagrant
 
Restful services with ColdFusion
Restful services with ColdFusionRestful services with ColdFusion
Restful services with ColdFusion
 
Build your own secure and real-time dashboard for mobile and web
Build your own secure and real-time dashboard for mobile and webBuild your own secure and real-time dashboard for mobile and web
Build your own secure and real-time dashboard for mobile and web
 
10 Reasons ColdFusion PDFs should rule the world
10 Reasons ColdFusion PDFs should rule the world10 Reasons ColdFusion PDFs should rule the world
10 Reasons ColdFusion PDFs should rule the world
 

Similar to Building better SQL Server Databases

Build a modern data platform.pptx
Build a modern data platform.pptxBuild a modern data platform.pptx
Build a modern data platform.pptxIke Ellis
 
MySQL: Know more about open Source Database
MySQL: Know more about open Source DatabaseMySQL: Know more about open Source Database
MySQL: Know more about open Source DatabaseMahesh Salaria
 
Data modeling trends for analytics
Data modeling trends for analyticsData modeling trends for analytics
Data modeling trends for analyticsIke Ellis
 
Query Optimization in SQL Server
Query Optimization in SQL ServerQuery Optimization in SQL Server
Query Optimization in SQL ServerRajesh Gunasundaram
 
MySQL: Know more about open Source Database
MySQL: Know more about open Source DatabaseMySQL: Know more about open Source Database
MySQL: Know more about open Source DatabaseMahesh Salaria
 
Introduction to SharePoint for SQLserver DBAs
Introduction to SharePoint for SQLserver DBAsIntroduction to SharePoint for SQLserver DBAs
Introduction to SharePoint for SQLserver DBAsSteve Knutson
 
Tableau Seattle BI Event How Tableau Changed My Life
Tableau Seattle BI Event How Tableau Changed My LifeTableau Seattle BI Event How Tableau Changed My Life
Tableau Seattle BI Event How Tableau Changed My LifeRussell Spangler
 
Oracle Database 12c - Features for Big Data
Oracle Database 12c - Features for Big DataOracle Database 12c - Features for Big Data
Oracle Database 12c - Features for Big DataAbishek V S
 
SQL Server Tips & Tricks
SQL Server Tips & TricksSQL Server Tips & Tricks
SQL Server Tips & TricksIke Ellis
 
AWS June 2016 Webinar Series - Amazon Redshift or Big Data Analytics
AWS June 2016 Webinar Series - Amazon Redshift or Big Data AnalyticsAWS June 2016 Webinar Series - Amazon Redshift or Big Data Analytics
AWS June 2016 Webinar Series - Amazon Redshift or Big Data AnalyticsAmazon Web Services
 
Data Warehouse Best Practices
Data Warehouse Best PracticesData Warehouse Best Practices
Data Warehouse Best PracticesEduardo Castro
 
Data Modeling on Azure for Analytics
Data Modeling on Azure for AnalyticsData Modeling on Azure for Analytics
Data Modeling on Azure for AnalyticsIke Ellis
 
How to Design a Good Database
How to Design a Good DatabaseHow to Design a Good Database
How to Design a Good DatabaseNur Hidayat
 
Introduction to CQL and Data Modeling with Apache Cassandra
Introduction to CQL and Data Modeling with Apache CassandraIntroduction to CQL and Data Modeling with Apache Cassandra
Introduction to CQL and Data Modeling with Apache CassandraJohnny Miller
 
Taming the shrew, Optimizing Power BI Options
Taming the shrew, Optimizing Power BI OptionsTaming the shrew, Optimizing Power BI Options
Taming the shrew, Optimizing Power BI OptionsKellyn Pot'Vin-Gorman
 
Columnstore indexes in sql server 2014
Columnstore indexes in sql server 2014Columnstore indexes in sql server 2014
Columnstore indexes in sql server 2014Antonios Chatzipavlis
 

Similar to Building better SQL Server Databases (20)

Build a modern data platform.pptx
Build a modern data platform.pptxBuild a modern data platform.pptx
Build a modern data platform.pptx
 
MySQL: Know more about open Source Database
MySQL: Know more about open Source DatabaseMySQL: Know more about open Source Database
MySQL: Know more about open Source Database
 
Data modeling trends for analytics
Data modeling trends for analyticsData modeling trends for analytics
Data modeling trends for analytics
 
Query Optimization in SQL Server
Query Optimization in SQL ServerQuery Optimization in SQL Server
Query Optimization in SQL Server
 
Breaking data
Breaking dataBreaking data
Breaking data
 
MySQL: Know more about open Source Database
MySQL: Know more about open Source DatabaseMySQL: Know more about open Source Database
MySQL: Know more about open Source Database
 
Introduction to Amazon Athena
Introduction to Amazon AthenaIntroduction to Amazon Athena
Introduction to Amazon Athena
 
Introduction to SharePoint for SQLserver DBAs
Introduction to SharePoint for SQLserver DBAsIntroduction to SharePoint for SQLserver DBAs
Introduction to SharePoint for SQLserver DBAs
 
Rdbms
RdbmsRdbms
Rdbms
 
Tableau Seattle BI Event How Tableau Changed My Life
Tableau Seattle BI Event How Tableau Changed My LifeTableau Seattle BI Event How Tableau Changed My Life
Tableau Seattle BI Event How Tableau Changed My Life
 
Mysql For Developers
Mysql For DevelopersMysql For Developers
Mysql For Developers
 
Oracle Database 12c - Features for Big Data
Oracle Database 12c - Features for Big DataOracle Database 12c - Features for Big Data
Oracle Database 12c - Features for Big Data
 
SQL Server Tips & Tricks
SQL Server Tips & TricksSQL Server Tips & Tricks
SQL Server Tips & Tricks
 
AWS June 2016 Webinar Series - Amazon Redshift or Big Data Analytics
AWS June 2016 Webinar Series - Amazon Redshift or Big Data AnalyticsAWS June 2016 Webinar Series - Amazon Redshift or Big Data Analytics
AWS June 2016 Webinar Series - Amazon Redshift or Big Data Analytics
 
Data Warehouse Best Practices
Data Warehouse Best PracticesData Warehouse Best Practices
Data Warehouse Best Practices
 
Data Modeling on Azure for Analytics
Data Modeling on Azure for AnalyticsData Modeling on Azure for Analytics
Data Modeling on Azure for Analytics
 
How to Design a Good Database
How to Design a Good DatabaseHow to Design a Good Database
How to Design a Good Database
 
Introduction to CQL and Data Modeling with Apache Cassandra
Introduction to CQL and Data Modeling with Apache CassandraIntroduction to CQL and Data Modeling with Apache Cassandra
Introduction to CQL and Data Modeling with Apache Cassandra
 
Taming the shrew, Optimizing Power BI Options
Taming the shrew, Optimizing Power BI OptionsTaming the shrew, Optimizing Power BI Options
Taming the shrew, Optimizing Power BI Options
 
Columnstore indexes in sql server 2014
Columnstore indexes in sql server 2014Columnstore indexes in sql server 2014
Columnstore indexes in sql server 2014
 

More from ColdFusionConference

Herding cats managing ColdFusion servers with commandbox
Herding cats managing ColdFusion servers with commandboxHerding cats managing ColdFusion servers with commandbox
Herding cats managing ColdFusion servers with commandboxColdFusionConference
 
Everyones invited! Meet accesibility requirements with ColdFusion
Everyones invited! Meet accesibility requirements with ColdFusionEveryones invited! Meet accesibility requirements with ColdFusion
Everyones invited! Meet accesibility requirements with ColdFusionColdFusionConference
 
Getting started with mobile application development
Getting started with mobile application developmentGetting started with mobile application development
Getting started with mobile application developmentColdFusionConference
 

More from ColdFusionConference (11)

Rest ful tools for lazy experts
Rest ful tools for lazy expertsRest ful tools for lazy experts
Rest ful tools for lazy experts
 
Herding cats managing ColdFusion servers with commandbox
Herding cats managing ColdFusion servers with commandboxHerding cats managing ColdFusion servers with commandbox
Herding cats managing ColdFusion servers with commandbox
 
Realtime with websockets
Realtime with websocketsRealtime with websockets
Realtime with websockets
 
Instant ColdFusion with Vagrant
Instant ColdFusion with VagrantInstant ColdFusion with Vagrant
Instant ColdFusion with Vagrant
 
Hidden gems in cf2016
Hidden gems in cf2016Hidden gems in cf2016
Hidden gems in cf2016
 
Everyones invited! Meet accesibility requirements with ColdFusion
Everyones invited! Meet accesibility requirements with ColdFusionEveryones invited! Meet accesibility requirements with ColdFusion
Everyones invited! Meet accesibility requirements with ColdFusion
 
Getting started with mobile application development
Getting started with mobile application developmentGetting started with mobile application development
Getting started with mobile application development
 
Bring api manager into your stack
Bring api manager into your stackBring api manager into your stack
Bring api manager into your stack
 
Keep Applications Online
Keep Applications OnlineKeep Applications Online
Keep Applications Online
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
ColdFusion Craftsmanship
ColdFusion CraftsmanshipColdFusion Craftsmanship
ColdFusion Craftsmanship
 

Recently uploaded

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 

Recently uploaded (20)

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 

Building better SQL Server Databases

  • 3. Who is this guy? Eric Cobb - Started in IT in 1999 as a "webmaster“ - Developer for 14 years - Microsoft Certified Solutions Expert (MCSE) - Data Platform - Data Management and Analytics - Now a full time DBA Blog: http://www.sqlnuggets.com Blog Twitter: @sqlnugg Personal Twitter: @cfgears
  • 4. What are we going to learn? • How SQL Server stores and retrieves data • How your database design can impact resources and performance • Design tips to incorporate into your database development • Common T-SQL mistakes and things to avoid • Primarily focusing on OLTP databases
  • 5. A PEEK UNDERTHE HOOD OF SQL SERVER A BRIEF OVERVIEW OF HOW SQL SERVER STORES AND RETRIEVES DATA
  • 6. A Peek UnderThe Hood Tables & Data Pages
  • 7. A Peek UnderThe Hood Tables & Data Pages
  • 8. A Peek UnderThe Hood Process of Requesting Data
  • 9. A Peek UnderThe Hood Process of Requesting Data
  • 10. A Peek UnderThe Hood Process of Requesting Data
  • 11. A Peek UnderThe Hood Process of Requesting Data
  • 12. A Peek UnderThe Hood Storing Data in Pages
  • 13. A Peek UnderThe Hood Storing Data in Pages
  • 14. A Peek UnderThe Hood Storing Data in Pages How is the data stored in a Page? Unordered (Heap) • Query optimizer reads all the rows in the table (table scan), to find the rows that meet the criteria of a query • A table scan generates many disk I/O operations and can be resource intensive • Heaps should generally be avoided, although can be useful when inserting large amounts of data in ETL/Bulk processes Ordered (Clustered Index) • Tells SQL Server how to physically sort the records on disk • The most important index you can apply to a table • Data pages are ordered, for faster data retrieval • Query optimizer searches the index key columns and finds the location of the rows needed by the query • Searching the index is much faster than scanning the entire table • There is only ever 1 clustered index on a table
  • 15. A Peek UnderThe Hood Storing Data in Pages How do I create Clustered Indexes? Primary Key = Clustered Index (usually) • SQL Server automatically creates a clustered index on your Primary Key column if a clustered index does not already exist on the table • If you do not want the Primary Key to be your Clustered Index, you can create your Clustered Index on a different column Clustered Index (Primary Key) Tips: • Use a naturally occurring incremental value • Keep as small and narrow as possible (single columns are preferred) • Avoid using character data types for a Clustered Index
  • 16. A Peek UnderThe Hood Storing Data in Pages
  • 17. A Peek UnderThe Hood Page Splits
  • 18. A Peek UnderThe Hood Page Splits
  • 19. A Peek UnderThe Hood Page Splits
  • 20. A Peek UnderThe Hood Page Splits
  • 21. A Peek UnderThe Hood Page Splits
  • 22. A Peek UnderThe Hood Page Splits
  • 23. A Peek UnderThe Hood Page Splits How can we avoid Page Splits? You can’t avoid them, but you can minimize them with good table designs • Choose a good Clustered Index (Primary Key) for your table • Should be unique, narrow, static, and incremental • Good Clustered Index examples: • A numeric identity column (smallint, int, bigint) • A composite key of date and identity – in that order (date, identity) • A pseudo sequential GUID (using the NEWSEQUENTIALID() function in SQL Server) • Not recommended, but the best you can do if you absolutely have to use a GUID • Clustered Indexes to avoid: • Unique Identifier (GUID) generate from an application or with SQL Server’s NEWID() function • Character columns (CHAR, VARCHAR, NVARCHAR, etc…) • Combination of multiple character columns (LastName, FirstName, MiddlieInitial) • Columns that undergo frequent changes
  • 24. A Peek UnderThe Hood Overflow Pages
  • 25. A Peek UnderThe Hood Overflow Pages
  • 26. A Peek UnderThe Hood Overflow Pages
  • 27. BUILDING BETTERTABLES DESIGNING TABLES WITH EFFICIENCY IN MIND
  • 28. Building BetterTables UsingThe Right DataTypes Data Types Are Important! Choose your table column data types wisely • They can affect the performance of your database as it grows Know your data, use the appropriate data type for the data you are storing • The more accurate your data type is, the more efficiently SQL Server can handle your data. Use the smallest data type possible (within reason) • The smaller the column, the less data you have to store and retrieve, which leads to faster reads and writes • The longest city name in the U.S. is Rancho Santa Margarita, California; it’s 22 chars, don’t use VARCHAR(MAX) • The true name of Bangkok, Thailand is: Krungthepmahanakhon Amonrattanakosin Mahintharayutthaya Mahadilokphop Noppharatratchathaniburirom Udomratchaniwetmahasathan Amonphimanawatansathit Sakkathattiyawitsanukamprasit. (176 chars)
  • 29. Building BetterTables UsingThe Right DataTypes CHAR vs VARCHAR • CHAR(n): Fixed-length string data, and the storage size is n bytes. • VARCHAR(n): Variable-length string data, the storage size is the actual length of the data entered + 2 bytes. • If you know the length of the string will always be the same, use CHAR to avoid the additional 2 bytes added to every VARCHAR record NCHAR vs NVARCHAR • If you have databases that support multiple languages, consider using the Unicode NCHAR or NVARCHAR data types to minimize character conversion issues • Carefully evaluate whether you really need NCHAR or NVARCHAR • NCHAR(n): Fixed-length Unicode string data, and the storage size is two times n bytes • NVARCHAR(n): Variable-length Unicode string data, and the storage size, in bytes, is two times the actual length of data entered + 2 bytes
  • 30. Building BetterTables UsingThe Right DataTypes DECLARE @var1 CHAR(10) = 'abc', @var2 NCHAR(10) = 'abc', @var3 VARCHAR(10) = 'abc', @var4 NVARCHAR(10) = 'abc' SELECT DATALENGTH(@var1) AS [char], DATALENGTH(@var2) AS [nchar], DATALENGTH(@var3) AS [varchar], DATALENGTH(@var4) AS [nvarchar]
  • 31. Building BetterTables UsingThe Right DataTypes Numeric Data Types Choose the appropriate Data Type for the range of numbers you will be storing Data Type Range Storage BIGINT -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (Quintillion) 8 Bytes INT -2,147,483,648 to 2,147,483,647 (Billion) 4 Bytes SMALLINT -32,768 to 32,767 2 Bytes TINYINT 0 to 255 1 Byte
  • 32. Building BetterTables UsingThe Right DataTypes Date and Time Data Types Choose the appropriate Data Type for the range of dates you will be storing Data Type Range Storage TIME 00:00:00.0000000 through 23:59:59.9999999 3 - 5 Bytes DATE 0001-01-01 through 9999-12-31 3 Bytes SMALLDATETIME 1900-01-01 through 2079-06-06 4 Bytes DATETIME 1753-01-01 through 9999-12-31 8 Bytes
  • 33. Building BetterTables UsingThe Right DataTypes Why does this matter? Performance • Smaller data sets = faster queries • Optimized data pages = optimized resource usage (Remember the Buffer Pool?) Scalability • Helps you build better, more scalable applications • Don’t think in terms of 1 row of data, think about millions • Scalable applications do not happen by accident Time spent on proper database design is well worth it • Minor changes can have a major impact • It can take more effort to rebuild an existing application than it does to originally design one correctly
  • 34. Building BetterTables UsingThe Right DataTypes Why does this matter? * Taken from Kimberly Tripp’s Pluralsight Course: SQL Server: Why Physical Database Design Matters http://www.pluralsight.com/courses/sqlserver-why-physical-db-design-matters
  • 35. T-SQLTIPS T-SQL HABITS THAT CAN CAUSE PROBLEMS
  • 36. T-SQLTips NOLOCK Allows a Dirty Read • Does not issue locks to prevent other transactions from modifying data being read • Allows other transactions to modify the data while you’re trying to read it • Data returned to the SELECT statement may or may not actually exist in the database, and in some cases it may cause a query to return the same row multiple times or even skip rows When should I use NOLOCK? • If your query doesn’t necessarily need to return precise figures, and can tolerate some inconsistencies • If you are querying data that does not get modified often But NOLOCK makes my query faster! • It makes your query faster because it is ignoring the safeguards put in place to ensure that your query is returning accurate data If you need 100% accurate results from your query, do not use NOLOCK
  • 37. T-SQLTips Stored Procedures Do not name your stored procedures with the “sp_” prefix! • This is reserved for system stored procedures • SQL Server first checks the Master database for these procedures Use SET NOCOUNT • Can improve stored procedure performance • Turns off the messages that SQL Server sends back to the client after each T-SQL statement is executed
  • 38. T-SQLTips Query Performance Killers • Try not to use ORDER BY or DISTINCT in your queries, as it adds a lot of extra overhead. It is more efficient to sort/filter the data in your application • Format Dates in your application instead of in your SELECT statements • Avoid Cursors and Loops in your T-SQL statements, as it forces row-by-row operations • Using Functions in WHERE clauses and JOINS should be avoided • Do Not Use SELECT * • Can cause the optimizer to ignore indexes on the table, forcing a full table scan • The more fields you return, the worse your performance is going to be (especially when ordering) • Avoid Data Type Mismatches (aka Implicit Conversions) • Variables used in WHERE clauses should match the data type of the columns they’re compared with • Columns used in JOIN conditions should have matching data types