SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Writing Efficient Queries – Part 1 Using SQL Server Internals to Improve Data Access Eddie Wuerch MCT, MCITP Principal, Data Management ExactTarget eddie@indydba.com
Disk I/O – Key Points Disk I/O (reads and writes) is usually the slowest component of a system Compare: Memory and CPU speeds are reported in GHz: billions of actions per second Disk I/O rates are simply measured in IOPS: Input/output Operations Per Second Fast disks on mostly sequential workloads can get 100-150 IOPS Because of disk access rates, our first tuning goal is to reduce overall I/O …so let’s look at those data pages, then examine how to process less of them
Disk I/O – Key Points - II SQL Server is not a black box Most data is in structured storage There are many ways to access the data, some ways are significantly faster – and cause less impact on other processes - than others Understanding SQL Server data storage internals will guide you to the faster ways
Data Storage in SQL Server The base unit of SQL Server data storage is the page All data – system and user – is stored in pages Each page is 8KB (8192 bytes) Pages are allocated from files in 64KB (8-page) extents
Page Processing Pages are read from disk and processed in memory as an entire 8KB unit Extents are often read in from disk as a single block to reduce I/O All data is processed in memory, pulled from disk first (processing put on hold) if data is not in memory
Page Types System Page Types Space Management: File Header, PFS, GAM, SGAM Change Management: DCM, BCM Data Page Types In-row data  Index  LOB data and Row-overflow data All pages are 8KB
In-Row Data Pages 96-byte Page Header Page Header ,[object Object]
Rows written serially
Starts at 97th byteRow 1… Row 2… Row 3… Row 4… ,[object Object]
Starts at end of page, moves backwards
Records first-byte offset of each row4… 3… 2… 1…
Disk Access Methods Think of a phone book, with each entry as a record Ordered by Last Name, First Name, MI Two ways to find a record: Use Last Name, First Name to find a number (Index Seek) Look through the entire phone book, one page at a time, scanning each row for data (Table Scan)
Clustered Index Represents the table itself Index specifies the physical ordering of that data Only 1 allowed per table May be unique, does not have to be the primary key Non-clustered index Additional index of data Over 200 allowed May be unique Index Types
The phone book example If a table has a clustered index, the pointer to each row in the table is the clustered index key The leaf level of the nonclustered index contains the nonclustered keys and the clustered index keys Nonclustered indexes may also include additional non-indexed columns, will be stored at the leaf level of the index
Index Pages A-K : Page 4 L-U : Page 5 V-Z : Page 6  4 5 6 A : Page 22 B : Page 23 C : Page 24  D…. Baa : Page 276 Baba : Page 277 Base : Page 278 Ba… 22 23 24 25 26 27 274 275 276 277 278 279 280 281 282 283
Index Lookups
Index Lookups - revisted Nonclustered index Separate trip through the clustered index for each ncl entry! Clustered index (table)
Operational Join Types Merge Joins Hash Joins Loop Joins
Join Type Comparison Loop Merge Hash ,[object Object]
One trip through one table
One trip through the other table for each entry in the first table
Generally the slowest of the three typesOne trip through each table Requires indexes on both sides, at least one of them must be unique Usually the fastest join type Works well for very large joins Builds join data in tempdb
Join and Indexing Tips When defining an index, if the data is unique, then declare the index as unique Join on keys Provide arguments in WHERE clauses to match available indexes Cluster tables on range scans Look for covering indexes
So How Do I Know? SET STATISTICS IO ON

Weitere ähnliche Inhalte

Was ist angesagt?

Inverted page tables basic
Inverted page tables basicInverted page tables basic
Inverted page tables basic
Sanoj Kumar
 
Fundamental File Processing Operations
Fundamental File Processing OperationsFundamental File Processing Operations
Fundamental File Processing Operations
Rico
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashing
Jeet Poria
 

Was ist angesagt? (20)

MS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database ConceptsMS Sql Server: Introduction To Database Concepts
MS Sql Server: Introduction To Database Concepts
 
Structure of the page table
Structure of the page tableStructure of the page table
Structure of the page table
 
File organization
File organizationFile organization
File organization
 
Data indexing presentation
Data indexing presentationData indexing presentation
Data indexing presentation
 
Inverted page tables basic
Inverted page tables basicInverted page tables basic
Inverted page tables basic
 
Operating System- Multilevel Paging, Inverted Page Table
Operating System- Multilevel Paging, Inverted Page TableOperating System- Multilevel Paging, Inverted Page Table
Operating System- Multilevel Paging, Inverted Page Table
 
Concept of computer files
Concept of computer filesConcept of computer files
Concept of computer files
 
Performance Improvements
Performance ImprovementsPerformance Improvements
Performance Improvements
 
File organisation
File organisationFile organisation
File organisation
 
Data Organization in InnoDB
Data Organization in InnoDBData Organization in InnoDB
Data Organization in InnoDB
 
Falando de MySQL
Falando de MySQLFalando de MySQL
Falando de MySQL
 
File organisation
File organisationFile organisation
File organisation
 
File organization continued
File organization continuedFile organization continued
File organization continued
 
File structures
File structuresFile structures
File structures
 
[Www.pkbulk.blogspot.com]dbms12
[Www.pkbulk.blogspot.com]dbms12[Www.pkbulk.blogspot.com]dbms12
[Www.pkbulk.blogspot.com]dbms12
 
Sql server basics
Sql server basicsSql server basics
Sql server basics
 
Lecture storage-buffer
Lecture storage-bufferLecture storage-buffer
Lecture storage-buffer
 
Fundamental File Processing Operations
Fundamental File Processing OperationsFundamental File Processing Operations
Fundamental File Processing Operations
 
Hardware implementation of page table
Hardware implementation of page table Hardware implementation of page table
Hardware implementation of page table
 
Indexing and hashing
Indexing and hashingIndexing and hashing
Indexing and hashing
 

Andere mochten auch (7)

Presentation New
Presentation NewPresentation New
Presentation New
 
3 Kodu
3 Kodu3 Kodu
3 Kodu
 
1 Win7 For Devs Fund Search
1 Win7 For Devs Fund Search1 Win7 For Devs Fund Search
1 Win7 For Devs Fund Search
 
3 App Compat Win7
3 App Compat Win73 App Compat Win7
3 App Compat Win7
 
SQL Programming
SQL ProgrammingSQL Programming
SQL Programming
 
4 Making Movies
4 Making Movies4 Making Movies
4 Making Movies
 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data miningMS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
 

Ähnlich wie Indy pass writing efficient queries – part 1 - indexing

Myth busters - performance tuning 103 2008
Myth busters - performance tuning 103 2008Myth busters - performance tuning 103 2008
Myth busters - performance tuning 103 2008
paulguerin
 
The thinking persons guide to data warehouse design
The thinking persons guide to data warehouse designThe thinking persons guide to data warehouse design
The thinking persons guide to data warehouse design
Calpont
 
Implementing the Databese Server session 02
Implementing the Databese Server session 02Implementing the Databese Server session 02
Implementing the Databese Server session 02
Guillermo Julca
 
CS 542 Putting it all together -- Storage Management
CS 542 Putting it all together -- Storage ManagementCS 542 Putting it all together -- Storage Management
CS 542 Putting it all together -- Storage Management
J Singh
 
Ch 7 Physical D B Design
Ch 7  Physical D B  DesignCh 7  Physical D B  Design
Ch 7 Physical D B Design
guest8fdbdd
 

Ähnlich wie Indy pass writing efficient queries – part 1 - indexing (20)

Mysql For Developers
Mysql For DevelopersMysql For Developers
Mysql For Developers
 
Database Performance
Database PerformanceDatabase Performance
Database Performance
 
SQLServer Database Structures
SQLServer Database Structures SQLServer Database Structures
SQLServer Database Structures
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 
Myth busters - performance tuning 103 2008
Myth busters - performance tuning 103 2008Myth busters - performance tuning 103 2008
Myth busters - performance tuning 103 2008
 
Types of Databases
Types of DatabasesTypes of Databases
Types of Databases
 
Data storage and indexing
Data storage and indexingData storage and indexing
Data storage and indexing
 
The thinking persons guide to data warehouse design
The thinking persons guide to data warehouse designThe thinking persons guide to data warehouse design
The thinking persons guide to data warehouse design
 
Implementing the Databese Server session 02
Implementing the Databese Server session 02Implementing the Databese Server session 02
Implementing the Databese Server session 02
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
 
Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09Brad McGehee Intepreting Execution Plans Mar09
Brad McGehee Intepreting Execution Plans Mar09
 
Inside SQL Server In-Memory OLTP
Inside SQL Server In-Memory OLTPInside SQL Server In-Memory OLTP
Inside SQL Server In-Memory OLTP
 
database.pdf
database.pdfdatabase.pdf
database.pdf
 
CS 542 Putting it all together -- Storage Management
CS 542 Putting it all together -- Storage ManagementCS 542 Putting it all together -- Storage Management
CS 542 Putting it all together -- Storage Management
 
Storage struct
Storage structStorage struct
Storage struct
 
Pandi
PandiPandi
Pandi
 
Sql Server Performance Tuning
Sql Server Performance TuningSql Server Performance Tuning
Sql Server Performance Tuning
 
Ch 7 Physical D B Design
Ch 7  Physical D B  DesignCh 7  Physical D B  Design
Ch 7 Physical D B Design
 
Mass storage systemsos
Mass storage systemsosMass storage systemsos
Mass storage systemsos
 
Sql server performance tuning and optimization
Sql server performance tuning and optimizationSql server performance tuning and optimization
Sql server performance tuning and optimization
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
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
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Indy pass writing efficient queries – part 1 - indexing

  • 1. Writing Efficient Queries – Part 1 Using SQL Server Internals to Improve Data Access Eddie Wuerch MCT, MCITP Principal, Data Management ExactTarget eddie@indydba.com
  • 2. Disk I/O – Key Points Disk I/O (reads and writes) is usually the slowest component of a system Compare: Memory and CPU speeds are reported in GHz: billions of actions per second Disk I/O rates are simply measured in IOPS: Input/output Operations Per Second Fast disks on mostly sequential workloads can get 100-150 IOPS Because of disk access rates, our first tuning goal is to reduce overall I/O …so let’s look at those data pages, then examine how to process less of them
  • 3. Disk I/O – Key Points - II SQL Server is not a black box Most data is in structured storage There are many ways to access the data, some ways are significantly faster – and cause less impact on other processes - than others Understanding SQL Server data storage internals will guide you to the faster ways
  • 4. Data Storage in SQL Server The base unit of SQL Server data storage is the page All data – system and user – is stored in pages Each page is 8KB (8192 bytes) Pages are allocated from files in 64KB (8-page) extents
  • 5. Page Processing Pages are read from disk and processed in memory as an entire 8KB unit Extents are often read in from disk as a single block to reduce I/O All data is processed in memory, pulled from disk first (processing put on hold) if data is not in memory
  • 6. Page Types System Page Types Space Management: File Header, PFS, GAM, SGAM Change Management: DCM, BCM Data Page Types In-row data Index LOB data and Row-overflow data All pages are 8KB
  • 7.
  • 9.
  • 10. Starts at end of page, moves backwards
  • 11. Records first-byte offset of each row4… 3… 2… 1…
  • 12. Disk Access Methods Think of a phone book, with each entry as a record Ordered by Last Name, First Name, MI Two ways to find a record: Use Last Name, First Name to find a number (Index Seek) Look through the entire phone book, one page at a time, scanning each row for data (Table Scan)
  • 13. Clustered Index Represents the table itself Index specifies the physical ordering of that data Only 1 allowed per table May be unique, does not have to be the primary key Non-clustered index Additional index of data Over 200 allowed May be unique Index Types
  • 14. The phone book example If a table has a clustered index, the pointer to each row in the table is the clustered index key The leaf level of the nonclustered index contains the nonclustered keys and the clustered index keys Nonclustered indexes may also include additional non-indexed columns, will be stored at the leaf level of the index
  • 15. Index Pages A-K : Page 4 L-U : Page 5 V-Z : Page 6 4 5 6 A : Page 22 B : Page 23 C : Page 24 D…. Baa : Page 276 Baba : Page 277 Base : Page 278 Ba… 22 23 24 25 26 27 274 275 276 277 278 279 280 281 282 283
  • 17. Index Lookups - revisted Nonclustered index Separate trip through the clustered index for each ncl entry! Clustered index (table)
  • 18. Operational Join Types Merge Joins Hash Joins Loop Joins
  • 19.
  • 20. One trip through one table
  • 21. One trip through the other table for each entry in the first table
  • 22. Generally the slowest of the three typesOne trip through each table Requires indexes on both sides, at least one of them must be unique Usually the fastest join type Works well for very large joins Builds join data in tempdb
  • 23. Join and Indexing Tips When defining an index, if the data is unique, then declare the index as unique Join on keys Provide arguments in WHERE clauses to match available indexes Cluster tables on range scans Look for covering indexes
  • 24. So How Do I Know? SET STATISTICS IO ON
  • 25. So How Do I Know?
  • 26. So How Do I Know? sys.dm_db_index_usage_stats User_seeks User_scans User_lookups User_updates Sys.dm_db_missing_index_* Not magic, has limitations Many similar index entries with different INCLUDE statements may indicate a need to revisit the clustered index design
  • 27. So How Do I Know? Scan-indicating waits Lots of PAGEIOLATCH_SH and PAGEIOLATCH_EX waits are generated by tables scans that read from disk CX_PACKET waits – related to parallellism often caused by scanning large tables (don’t reduce MAXDOP: fix the scan!) Other processes with SOS_SCHEDULER_YIELD or high signal wait times may be mitigated by reducing CPU load of scans
  • 28. So How Do I Know? TempDB activity in instances without much use of temp tables or table variables SELECT * FROM sys.dm_io_virtual_file_stats(DB_ID(‘TempDB’), NULL) Must track over time, perform time-slice analysis May indicate additional worktable sort and hash-match activity Tracking this for all of your databases shows the amount of I/O your systems are performing, and if the disk systems are keeping up
  • 29. Resources Microsoft White Papers SQL Server 2000 I/O Basics (http://technet.microsoft.com/en-us/library/cc966500.aspx) SQL Server I/O Basics, Chapter 2 (http://technet.microsoft.com/en-us/library/cc917726.aspx) SQL Server Waits and Queues (download) (http://technet.microsoft.com/en-us/library/cc966413.aspx) The Waits and Queues document is highly recommended tuning or analyzing workloads
  • 30. Resources Inside SQL Server Book Series SQL 2005 The Storage Engine (Kalen Delaney) Query Tuning and Optimization (Delany, et. al.) T-SQL Querying (Ben-Gan, Kollar, Sarka) SQL 2008 Microsoft SQL Server 2008 Internals (Delaney, Randal, Tripp, Cunningham) T-SQL Querying (Ben-Gan, Kollar, Sarka)