SlideShare a Scribd company logo
www.dageop.com
Optimising
Queries
®
OQ-01 Query Optimiser Architecture
DR. SUBRAMANI
PARAMASIVAM
(MANI)
About
me
Dr. SubraMANI Paramasivam
PhD., MCT, MCSE, MCITP, MCP, MCTS, MCSA
CEO, Principal Consultant & Trainer
@ DAGEOP (UK)
Email: mani@dageop.com
Blog: http://dataap.org/blog
Follow
Us
https://www.facebook.com/pages/YOUR-SQL-MAN-LTD/
http://www.youtube.com/user/YourSQLMAN
https://twitter.com/dageop
https://uk.linkedin.com/in/dageop
Proud Sponsor
• SQLBits
• SQL Saturdays
• MCT Summit
• SQL Server Geeks
Summit
• Data Awareness
Programme
• Dageop’s Data Day
®
www.DataAP.org
SPEAKER
Contents
• OQ-01 Query Optimiser Architecture
• Phases
• Strategies
• Data access plans
• Auto-parameterisation
• Avoiding recompilation of queries
www.dageop.com
Optimizing Queries
OQ-01 Query Optimiser Architecture
www.dageop.com
Optimizing Queries
Optimising Queries
www.dageop.com
Optimizing Queries
Optimising Queries
• Optimising Query is very important task to maintain the server
resources.
• Most of the time server is hit by performance issues.
• Clean the SQL Server Cache.
• Well written T-SQL Queries will help to optimise.
www.dageop.com
Optimizing Queries
Query Optimiser Architecture
www.dageop.com
Optimizing Queries
Introduction to Query Optimiser Architecture
2 Major Components in SQL Server
•SQL Server Database Engine
• Reading data between disk and memory
•Relational Engine (Query Processor)
• Accepts queries, analyse & executes the plan.
www.dageop.com
Optimizing Queries
Introduction to Query Optimiser Architecture
• Analyse Execution Plans (Cannot consider every plan)
• Estimate the cost of each plan
• Select the plan with low cost
www.dageop.com
Optimizing Queries
Plan1
Plan2
Plan3
Plan4
Plan5
Plan6
Plan7
Plan1 – 80%
Plan2 – 60%
Plan3 – 10%
Plan5 – 90%
Plan6 – 70%
Plan3 – 10%
Analyse Estimate Select
COST X 2
Plan & itself
BETTER UNDERSTANDING
of Query Optimiser is a must
for both DBA & DEVELOPERS.
Main Challenges are
Cardinality & Cost Estimations
www.dageop.com
Optimizing Queries
Query Optimiser
• 1st Part - Searching or enumerating candidate plans
• 2nd Part - Estimates the cost of each physical operator in that plan
(I/O, CPU, and memory)
• This cost estimation depends mostly on the algorithm used by the
physical operator, as well as the estimated number of records that
will need to be processed (Cardinality Estimation).
PRIMARY WAY to interact with Query Optimizer is through
EXECUTION PLANS
www.dageop.com
Optimizing Queries
Query Optimiser
• Execution Plan
• Trees consisting of a number of physical operators
• Physical operators
• Index Scan
• Hash Aggregate
• result operator (root element in the plan)
• Nested Loops Join
• Merge Join
• Hash Join
• Algorithms
• Can be saved as .sqlplan (Can be viewed in SSMS)
www.dageop.com
Optimizing Queries
Query Optimiser
• Operators
• Logical
• Relational algebraic operation
• Conceptually describes what operation needs to be performed
• Physical
• Implement the operation described by logical operators
• Access columns, rows, index, table, views, calculations, aggregations, data integrity
checks
• 3 methods
• Init() – Initializes a connection
• GetNext() – 0 to many calls to get data
• Close() – Some cleanup and close the connection.
• Query Optimizer chooses efficient physical operator based on logical.
www.dageop.com
Optimizing Queries
Query Optimiser
www.dageop.com
Optimizing Queries
Logical Physical Logical & Physical
Bitmap Create Assert Aggregate
Branch Repartition Bitmap Clustered Index Scan
Cache Clustered Index Delete Clustered Index Scan
Distinct Clustered Index Insert Clustered Index Update
Distinct Sort Clustered Index Merge Collapse
Distribute Streams Hash Match Compute Scalar
Eager Spool Merge Join Concatenation
Flow Distinct Nested Loops Cursor
Full Outer Join Nonclustered Index Delete Inserted Scan
Gather Streams Index Insert Log Row Scan
Inner Join Index Spool Merge Interval
Insert Nonclustered Index Update Index Scan
Lazy Spool Online Index Insert Index Seek
Left Anti Semi Join Parallelism Parameter Table Scan
Left Outer Join RID Lookup Remote Delete
Left Semi Join Stream Aggregate Remote Index Scan
Partial Aggregate Table Delete Remote Index Seek
Repartition Streams Table Insert Remote Insert
Right Anti Semi Join Table Merge Remote Query
Right Outer Join Table Spool Remote Scan
Right Semi Join Table Update Remote Update
Row Count Spool Segment
Segment Repartition Sequence
Union Sequence Project
Update Sort
Split
Switch
Table Scan
Table-valued Function
Top
Window Spool
Query Optimiser
• Invalid Plans
• Removal of an index
• Removal of a constraint,
• Significant changes made to the contents of the database
• SQL Server under memory pressure
• Changing configuration options - max degree of parallelism
• Discard the plan cache and new optimization generated
www.dageop.com
Optimizing Queries
Query Optimiser
• Highly complex pieces of software
• Even 30 years of research, still has technical challenges.
HINTS
• override the operations of the Query Optimizer
• caution and only as a last option
• Influence Query Optimizer to use certain plans
www.dageop.com
Optimizing Queries
Query Optimiser - Interaction
www.dageop.com
Optimizing Queries
Query Optimizer
Execution Plans
Trees, Algorithm, Physical operator
ACTUAL ESTIMATED
Graphics, Text, XML format
DEMO
www.dageop.com
Optimizing Queries
Phases
www.dageop.com
Optimizing Queries
Phases
• Once we execute the SQL Statement, it will follow certain
procedures from SQL Statement to Query Result
www.dageop.com
Optimizing Queries
Parsing Query
Compilation
Query
Optimization
Query
Execution
Phases
• If the Query is already in the plan cache then it will not generate any
new execution plan for that query.
• Parsing:
• The Query’s syntax will be validated and query is transformed in a tree.
Checks the objects for its existence which are used in the query.
• After the query validated, the final tree is formed.
• Query Compilation:
• Query Tree will be compiled here.
www.dageop.com
Optimizing Queries
Phases
• Query Optimization
• The query optimizer takes as input the compiled query tree generated in the
previous step and investigates several access strategies before it decides how
to process the given query.
• It will analyse the query to find most efficient execution plan.
• Query Execution
• After the execution plan is generated, it is permanently stored and executed
• Note: For Some statements, parsing and optimization can be avoided if the
database engines know that there is only one viable plan. This is called trivial
plan optimization.
www.dageop.com
Optimizing Queries
Strategies
www.dageop.com
Optimizing Queries
Strategies
• As a DBA, we need to choose right strategy for each SQL statement like
scripts should be well written before executing the SQL statement.
• Statistics will be used to understand the performance of each query.
• Enable Set Statistics IO before query run. It will display the following
information
• How many scans were performed
• How many logical reads (reads in cache) were performed
• How many physical reads (reads on disk) were performed
• How many pages were placed in the cache in anticipation of future reads (read-ahead reads)
www.dageop.com
Optimizing Queries
Strategies
• We can understand the query performance from statistics IO, If the
query is good then the logical reads of the query result should be
lower and few physical reads and scans.
• Enable Set Statistics Time, it will display the execution time of the
query. It purely depends on the total activity of the server.
• Enable show execution plan, to see how the query performed.
• These are things will helpful for choosing the right strategy.
www.dageop.com
Optimizing Queries
www.dageop.com
Optimizing Queries
Strategies
Choose the right strategies based on below information
from SQL Server
DEMO
www.dageop.com
Optimizing Queries
Data Access Plans
www.dageop.com
Optimizing Queries
Data Access Plans
• If we need to optimize the data access plans then we need to start
from creating files for database, Index on the tables and T SQL
Statements.
• Steps:
• Organize the file groups and files
• Apply partitioning in big tables
• Create the appropriate indexes/covering indexes
• Defragment the indexes
• Identify inefficient TSQL
• Diagnose performance problems
www.dageop.com
Optimizing Queries
Data Access Plans
• Organize the file groups and files
• Initially two files will be created while created a database (.mdf & .ldf).
• .mdf file : Primary data file for each database. All system objects will be stored in this
file, including the user defined objects if .ndf file is not there.
• .ndf file: These are secondary data files, these are optional. These files will have user
created objects.
• .ldf file: These are the transaction log files. This could be one or more files for single
database.
• File Group:
• Database files are logically grouped for better performance and improved
administration on large databases. When a new SQL Server database is created, the
primary file group is created and the primary data file is included in the primary file
group. Also, the primary group is marked as the default group.
www.dageop.com
Optimizing Queries
Data Access Plans
• To obtain the performance of the data access, Primary file group must
be separate and it should be only for system objects.
• Need to create one more file called secondary data file for user
defined objects.
• Separating the system objects will improve the performance enhance
the ability to access tables in cases of serious data failures.
• For frequently accessed tables containing indexes, put the tables and
the indexes in separate file groups. This would enable reading the
index and table data faster
www.dageop.com
Optimizing Queries
Data Access Plans
• Apply partitioning in big tables
• Table partitioning is nothing but splitting the large table into multiple small
tables so that queries have to scan less amount for data retrieving.
• Consider partitioning big fat tables into different file groups where each file
inside the file group is spread into separate physical disks (so that the table
spans across different files in different physical disks). This would enable the
database engine to read/write data operations faster.
• Partitioning is very much needed for history tables.
• 2 types
• Physical
• Logical
www.dageop.com
Optimizing Queries
Data Access Plans
• Create the appropriate indexes/covering indexes
• Create Non-Clustered index on frequently used columns
• Create index on column which is used for joining the tables
• Index for foreign key columns
• Create covering index for particular columns which are using frequently.
• Defragment the indexes
• Once index has created , it should maintain properly to avoid
defragmentation.
• Maintaining the index will lead to performance gain.
www.dageop.com
Optimizing Queries
Data Access Plans
• Identify inefficient TSQL
• Don’t use * in the select statements. Mention column names while retrieving
the data. It will improve the performance of the query
• Avoid Deadlocks between two objects.
• Diagnose the performance issue
• SQL Server has many tools to Monitor and diagnose the issues.
• Accessing the data will be more easier.
www.dageop.com
Optimizing Queries
DEMO
www.dageop.com
Optimizing Queries
Auto-Parameterisation
www.dageop.com
Optimizing Queries
Auto-Parameterisation
• SQL Server Query Optimizer might decide to parameterize some of
the queries.
• In this case the specific parameter will not make any impact on the
execution plan. It will return the same execution plan.
• In SQL Server 2005 forced parameterization has been introduced and
is disabled by default and can be enabled in database level.
• To differentiate from forced parameterization, auto-parameterization
is also referred as simple parameterization.
www.dageop.com
Optimizing Queries
DEMO
www.dageop.com
Optimizing Queries
Avoiding Recompilation of Queries
www.dageop.com
Optimizing Queries
Avoiding Recompilation of Queries
• In SQL Server 2005 recompiles the stored procedures, only the
statement that causes recompilation is compiled, rather than the
entire procedure.
• Recompilation will occur in following ways,
• On Schema change of objects
• On Change of the SET options
• On statistics change of tables.
www.dageop.com
Optimizing Queries
Avoiding Recompilation of Queries
• On Schema Change of Objects
• Adding and dropping column, constraints, index, indexed view and trigger.
• On change of the SET options
• When executing the stored procedure, the compiled plan is created and it will store
the environment setting of a connection (SET OPTION) .
• Recompilation will occur, if the stored procedure run on different environment and
with different SET option then it will not use the existing plan which it is created first
time.
• On statistics change of tables
• SQL server maintains a modification counter for each table and index.
• If the counter values exceed the defined threshold, the previously create compiled
plans is considered stale plan and new plan will be created.
www.dageop.com
Optimizing Queries
Avoiding Recompilation of Queries
• Temporary table modification counter threshold is 6. Stored
procedure will be recompiled when stored procedure create a temp
table insert 6 or more rows into this table.
• For Permanent table the counter threshold is 500.
• We can increase the temp table counter threshold to 500 as same as
permanent table.
• Use table variable instead of Temporary table.
www.dageop.com
Optimizing Queries
DEMO
www.dageop.com
Optimizing Queries
Review
Query Optimiser Architecture
Phases
Strategies
Data access plans
Auto-parameterisation
Avoiding recompilation of queries
www.dageop.com
Optimizing Queries
Q & A
www.dageop.com
Optimizing Queries
®
www.dageop.com

More Related Content

What's hot (20)

PDF
Introduction to Impala
markgrover
 
PDF
SQL and Machine Learning on Hadoop
Mukund Babbar
 
PDF
Parallelize R Code Using Apache Spark
Databricks
 
PDF
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Zohar Elkayam
 
PPTX
GPORCA: Query Optimization as a Service
PivotalOpenSourceHub
 
PPTX
Consuming External Content and Enriching Content with Apache Camel
therealgaston
 
PDF
Rapid Cluster Computing with Apache Spark 2016
Zohar Elkayam
 
PDF
Data Warehouse 2.0: Master Techniques for EPM Guys (Powered by ODI)
Rodrigo Radtke de Souza
 
PDF
Koalas: How Well Does Koalas Work?
Databricks
 
PDF
Deep Dive into GPU Support in Apache Spark 3.x
Databricks
 
PDF
Cloudera Impala - San Diego Big Data Meetup August 13th 2014
cdmaxime
 
PDF
In Memory Data Pipeline And Warehouse At Scale - BerlinBuzzwords 2015
Iulia Emanuela Iancuta
 
PDF
Orca: A Modular Query Optimizer Architecture for Big Data
EMC
 
PDF
Easy, Scalable, Fault-tolerant stream processing with Structured Streaming in...
DataWorks Summit
 
PDF
Koalas: Making an Easy Transition from Pandas to Apache Spark
Databricks
 
PPTX
Impala 2.0 - The Best Analytic Database for Hadoop
Cloudera, Inc.
 
PDF
Improving Python and Spark Performance and Interoperability with Apache Arrow...
Databricks
 
PPT
Spark_Part 1
Shashi Prakash
 
PDF
PostgreSQL Extension APIs are Changing the Face of Relational Databases | PGC...
Teresa Giacomini
 
PDF
Introducing Postgres Plus Advanced Server 9.4
EDB
 
Introduction to Impala
markgrover
 
SQL and Machine Learning on Hadoop
Mukund Babbar
 
Parallelize R Code Using Apache Spark
Databricks
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Zohar Elkayam
 
GPORCA: Query Optimization as a Service
PivotalOpenSourceHub
 
Consuming External Content and Enriching Content with Apache Camel
therealgaston
 
Rapid Cluster Computing with Apache Spark 2016
Zohar Elkayam
 
Data Warehouse 2.0: Master Techniques for EPM Guys (Powered by ODI)
Rodrigo Radtke de Souza
 
Koalas: How Well Does Koalas Work?
Databricks
 
Deep Dive into GPU Support in Apache Spark 3.x
Databricks
 
Cloudera Impala - San Diego Big Data Meetup August 13th 2014
cdmaxime
 
In Memory Data Pipeline And Warehouse At Scale - BerlinBuzzwords 2015
Iulia Emanuela Iancuta
 
Orca: A Modular Query Optimizer Architecture for Big Data
EMC
 
Easy, Scalable, Fault-tolerant stream processing with Structured Streaming in...
DataWorks Summit
 
Koalas: Making an Easy Transition from Pandas to Apache Spark
Databricks
 
Impala 2.0 - The Best Analytic Database for Hadoop
Cloudera, Inc.
 
Improving Python and Spark Performance and Interoperability with Apache Arrow...
Databricks
 
Spark_Part 1
Shashi Prakash
 
PostgreSQL Extension APIs are Changing the Face of Relational Databases | PGC...
Teresa Giacomini
 
Introducing Postgres Plus Advanced Server 9.4
EDB
 

Similar to Optimising Queries - Series 1 Query Optimiser Architecture (20)

PPTX
Database Fundamental Concepts- Series 1 - Performance Analysis
DAGEOP LTD
 
PDF
Breaking data
Terry Bunio
 
PDF
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
PPTX
שבוע אורקל 2016
Aaron Shilo
 
PPTX
Large Data Volume Salesforce experiences
Cidar Mendizabal
 
PPTX
Evolving the Optimal Relevancy Ranking Model at Dice.com
Simon Hughes
 
PDF
SQL Server 2017 - Adaptive Query Processing and Automatic Query Tuning
Javier Villegas
 
PPT
Choosing the Right Business Intelligence Tools for Your Data and Architectura...
Victor Holman
 
PPTX
Geek Sync | Intro to Query Store
IDERA Software
 
PDF
dd presentation.pdf
AnSHiKa187943
 
PDF
Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...
Lucidworks
 
PDF
Bb world 2012 using database statistics to make capacity planning decisions...
Geoff Mower
 
PDF
Taming the shrew Power BI
Kellyn Pot'Vin-Gorman
 
PPS
MySQL Optimization from a Developer's point of view
Sachin Khosla
 
DOCX
Database Systems Design, Implementation, and Management
OllieShoresna
 
PPT
Database performance tuning and query optimization
Dhani Ahmad
 
PPT
Datastage Introduction To Data Warehousing
Vibrant Technologies & Computers
 
PDF
071410 sun a_1515_feldman_stephen
Steve Feldman
 
PDF
Using Query Store to Understand and Control Query Performance
Grant Fritchey
 
PDF
Data DevOps: An Overview
Scott W. Ambler
 
Database Fundamental Concepts- Series 1 - Performance Analysis
DAGEOP LTD
 
Breaking data
Terry Bunio
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 
שבוע אורקל 2016
Aaron Shilo
 
Large Data Volume Salesforce experiences
Cidar Mendizabal
 
Evolving the Optimal Relevancy Ranking Model at Dice.com
Simon Hughes
 
SQL Server 2017 - Adaptive Query Processing and Automatic Query Tuning
Javier Villegas
 
Choosing the Right Business Intelligence Tools for Your Data and Architectura...
Victor Holman
 
Geek Sync | Intro to Query Store
IDERA Software
 
dd presentation.pdf
AnSHiKa187943
 
Evolving The Optimal Relevancy Scoring Model at Dice.com: Presented by Simon ...
Lucidworks
 
Bb world 2012 using database statistics to make capacity planning decisions...
Geoff Mower
 
Taming the shrew Power BI
Kellyn Pot'Vin-Gorman
 
MySQL Optimization from a Developer's point of view
Sachin Khosla
 
Database Systems Design, Implementation, and Management
OllieShoresna
 
Database performance tuning and query optimization
Dhani Ahmad
 
Datastage Introduction To Data Warehousing
Vibrant Technologies & Computers
 
071410 sun a_1515_feldman_stephen
Steve Feldman
 
Using Query Store to Understand and Control Query Performance
Grant Fritchey
 
Data DevOps: An Overview
Scott W. Ambler
 
Ad

More from DAGEOP LTD (17)

PPTX
HIGH PERFORMANCE DATABASES
DAGEOP LTD
 
PPTX
DBA – THINGS TO KNOW
DAGEOP LTD
 
PPTX
SQL Server Editions and Features
DAGEOP LTD
 
PPTX
DATA & POWER VISUALIZATION
DAGEOP LTD
 
PPTX
Microsoft Products
DAGEOP LTD
 
PPTX
Data, Education and Social awareness
DAGEOP LTD
 
PPTX
Data Modeling - Series 4 X-Events
DAGEOP LTD
 
PPTX
Data Modeling - Series 1 Storing summarised data
DAGEOP LTD
 
PPTX
Optimising Queries - Series 4 Designing Effective Indexes
DAGEOP LTD
 
PPTX
Optimising Queries - Series 3 Distinguishing among query types
DAGEOP LTD
 
PPTX
Managing Memory & Locks - Series 2 Transactions & Lock management
DAGEOP LTD
 
PPTX
Managing Memory & Locks - Series 1 Memory Management
DAGEOP LTD
 
PPTX
All about Storage - Series 3 - All about indexes
DAGEOP LTD
 
PPTX
All about Storage - Series 2 Defining Data
DAGEOP LTD
 
PPTX
Database Fundamental Concepts - Series 2 Monitoring plan
DAGEOP LTD
 
PDF
Advanced SSRS Reporting Techniques
DAGEOP LTD
 
PPTX
Perfect Performance Platter - SQL Server 2014
DAGEOP LTD
 
HIGH PERFORMANCE DATABASES
DAGEOP LTD
 
DBA – THINGS TO KNOW
DAGEOP LTD
 
SQL Server Editions and Features
DAGEOP LTD
 
DATA & POWER VISUALIZATION
DAGEOP LTD
 
Microsoft Products
DAGEOP LTD
 
Data, Education and Social awareness
DAGEOP LTD
 
Data Modeling - Series 4 X-Events
DAGEOP LTD
 
Data Modeling - Series 1 Storing summarised data
DAGEOP LTD
 
Optimising Queries - Series 4 Designing Effective Indexes
DAGEOP LTD
 
Optimising Queries - Series 3 Distinguishing among query types
DAGEOP LTD
 
Managing Memory & Locks - Series 2 Transactions & Lock management
DAGEOP LTD
 
Managing Memory & Locks - Series 1 Memory Management
DAGEOP LTD
 
All about Storage - Series 3 - All about indexes
DAGEOP LTD
 
All about Storage - Series 2 Defining Data
DAGEOP LTD
 
Database Fundamental Concepts - Series 2 Monitoring plan
DAGEOP LTD
 
Advanced SSRS Reporting Techniques
DAGEOP LTD
 
Perfect Performance Platter - SQL Server 2014
DAGEOP LTD
 
Ad

Recently uploaded (20)

PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
PPTX
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
PPTX
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PDF
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PPTX
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
PDF
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
PDF
Simplifying Document Processing with Docling for AI Applications.pdf
Tamanna
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PDF
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
PPTX
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PDF
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
PDF
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
Simplifying Document Processing with Docling for AI Applications.pdf
Tamanna
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 

Optimising Queries - Series 1 Query Optimiser Architecture

  • 1. www.dageop.com Optimising Queries ® OQ-01 Query Optimiser Architecture DR. SUBRAMANI PARAMASIVAM (MANI)
  • 2. About me Dr. SubraMANI Paramasivam PhD., MCT, MCSE, MCITP, MCP, MCTS, MCSA CEO, Principal Consultant & Trainer @ DAGEOP (UK) Email: [email protected] Blog: http://dataap.org/blog Follow Us https://www.facebook.com/pages/YOUR-SQL-MAN-LTD/ http://www.youtube.com/user/YourSQLMAN https://twitter.com/dageop https://uk.linkedin.com/in/dageop Proud Sponsor • SQLBits • SQL Saturdays • MCT Summit • SQL Server Geeks Summit • Data Awareness Programme • Dageop’s Data Day ® www.DataAP.org SPEAKER
  • 3. Contents • OQ-01 Query Optimiser Architecture • Phases • Strategies • Data access plans • Auto-parameterisation • Avoiding recompilation of queries www.dageop.com Optimizing Queries
  • 4. OQ-01 Query Optimiser Architecture www.dageop.com Optimizing Queries
  • 6. Optimising Queries • Optimising Query is very important task to maintain the server resources. • Most of the time server is hit by performance issues. • Clean the SQL Server Cache. • Well written T-SQL Queries will help to optimise. www.dageop.com Optimizing Queries
  • 8. Introduction to Query Optimiser Architecture 2 Major Components in SQL Server •SQL Server Database Engine • Reading data between disk and memory •Relational Engine (Query Processor) • Accepts queries, analyse & executes the plan. www.dageop.com Optimizing Queries
  • 9. Introduction to Query Optimiser Architecture • Analyse Execution Plans (Cannot consider every plan) • Estimate the cost of each plan • Select the plan with low cost www.dageop.com Optimizing Queries Plan1 Plan2 Plan3 Plan4 Plan5 Plan6 Plan7 Plan1 – 80% Plan2 – 60% Plan3 – 10% Plan5 – 90% Plan6 – 70% Plan3 – 10% Analyse Estimate Select COST X 2 Plan & itself
  • 10. BETTER UNDERSTANDING of Query Optimiser is a must for both DBA & DEVELOPERS. Main Challenges are Cardinality & Cost Estimations www.dageop.com Optimizing Queries
  • 11. Query Optimiser • 1st Part - Searching or enumerating candidate plans • 2nd Part - Estimates the cost of each physical operator in that plan (I/O, CPU, and memory) • This cost estimation depends mostly on the algorithm used by the physical operator, as well as the estimated number of records that will need to be processed (Cardinality Estimation). PRIMARY WAY to interact with Query Optimizer is through EXECUTION PLANS www.dageop.com Optimizing Queries
  • 12. Query Optimiser • Execution Plan • Trees consisting of a number of physical operators • Physical operators • Index Scan • Hash Aggregate • result operator (root element in the plan) • Nested Loops Join • Merge Join • Hash Join • Algorithms • Can be saved as .sqlplan (Can be viewed in SSMS) www.dageop.com Optimizing Queries
  • 13. Query Optimiser • Operators • Logical • Relational algebraic operation • Conceptually describes what operation needs to be performed • Physical • Implement the operation described by logical operators • Access columns, rows, index, table, views, calculations, aggregations, data integrity checks • 3 methods • Init() – Initializes a connection • GetNext() – 0 to many calls to get data • Close() – Some cleanup and close the connection. • Query Optimizer chooses efficient physical operator based on logical. www.dageop.com Optimizing Queries
  • 14. Query Optimiser www.dageop.com Optimizing Queries Logical Physical Logical & Physical Bitmap Create Assert Aggregate Branch Repartition Bitmap Clustered Index Scan Cache Clustered Index Delete Clustered Index Scan Distinct Clustered Index Insert Clustered Index Update Distinct Sort Clustered Index Merge Collapse Distribute Streams Hash Match Compute Scalar Eager Spool Merge Join Concatenation Flow Distinct Nested Loops Cursor Full Outer Join Nonclustered Index Delete Inserted Scan Gather Streams Index Insert Log Row Scan Inner Join Index Spool Merge Interval Insert Nonclustered Index Update Index Scan Lazy Spool Online Index Insert Index Seek Left Anti Semi Join Parallelism Parameter Table Scan Left Outer Join RID Lookup Remote Delete Left Semi Join Stream Aggregate Remote Index Scan Partial Aggregate Table Delete Remote Index Seek Repartition Streams Table Insert Remote Insert Right Anti Semi Join Table Merge Remote Query Right Outer Join Table Spool Remote Scan Right Semi Join Table Update Remote Update Row Count Spool Segment Segment Repartition Sequence Union Sequence Project Update Sort Split Switch Table Scan Table-valued Function Top Window Spool
  • 15. Query Optimiser • Invalid Plans • Removal of an index • Removal of a constraint, • Significant changes made to the contents of the database • SQL Server under memory pressure • Changing configuration options - max degree of parallelism • Discard the plan cache and new optimization generated www.dageop.com Optimizing Queries
  • 16. Query Optimiser • Highly complex pieces of software • Even 30 years of research, still has technical challenges. HINTS • override the operations of the Query Optimizer • caution and only as a last option • Influence Query Optimizer to use certain plans www.dageop.com Optimizing Queries
  • 17. Query Optimiser - Interaction www.dageop.com Optimizing Queries Query Optimizer Execution Plans Trees, Algorithm, Physical operator ACTUAL ESTIMATED Graphics, Text, XML format
  • 20. Phases • Once we execute the SQL Statement, it will follow certain procedures from SQL Statement to Query Result www.dageop.com Optimizing Queries Parsing Query Compilation Query Optimization Query Execution
  • 21. Phases • If the Query is already in the plan cache then it will not generate any new execution plan for that query. • Parsing: • The Query’s syntax will be validated and query is transformed in a tree. Checks the objects for its existence which are used in the query. • After the query validated, the final tree is formed. • Query Compilation: • Query Tree will be compiled here. www.dageop.com Optimizing Queries
  • 22. Phases • Query Optimization • The query optimizer takes as input the compiled query tree generated in the previous step and investigates several access strategies before it decides how to process the given query. • It will analyse the query to find most efficient execution plan. • Query Execution • After the execution plan is generated, it is permanently stored and executed • Note: For Some statements, parsing and optimization can be avoided if the database engines know that there is only one viable plan. This is called trivial plan optimization. www.dageop.com Optimizing Queries
  • 24. Strategies • As a DBA, we need to choose right strategy for each SQL statement like scripts should be well written before executing the SQL statement. • Statistics will be used to understand the performance of each query. • Enable Set Statistics IO before query run. It will display the following information • How many scans were performed • How many logical reads (reads in cache) were performed • How many physical reads (reads on disk) were performed • How many pages were placed in the cache in anticipation of future reads (read-ahead reads) www.dageop.com Optimizing Queries
  • 25. Strategies • We can understand the query performance from statistics IO, If the query is good then the logical reads of the query result should be lower and few physical reads and scans. • Enable Set Statistics Time, it will display the execution time of the query. It purely depends on the total activity of the server. • Enable show execution plan, to see how the query performed. • These are things will helpful for choosing the right strategy. www.dageop.com Optimizing Queries
  • 26. www.dageop.com Optimizing Queries Strategies Choose the right strategies based on below information from SQL Server
  • 29. Data Access Plans • If we need to optimize the data access plans then we need to start from creating files for database, Index on the tables and T SQL Statements. • Steps: • Organize the file groups and files • Apply partitioning in big tables • Create the appropriate indexes/covering indexes • Defragment the indexes • Identify inefficient TSQL • Diagnose performance problems www.dageop.com Optimizing Queries
  • 30. Data Access Plans • Organize the file groups and files • Initially two files will be created while created a database (.mdf & .ldf). • .mdf file : Primary data file for each database. All system objects will be stored in this file, including the user defined objects if .ndf file is not there. • .ndf file: These are secondary data files, these are optional. These files will have user created objects. • .ldf file: These are the transaction log files. This could be one or more files for single database. • File Group: • Database files are logically grouped for better performance and improved administration on large databases. When a new SQL Server database is created, the primary file group is created and the primary data file is included in the primary file group. Also, the primary group is marked as the default group. www.dageop.com Optimizing Queries
  • 31. Data Access Plans • To obtain the performance of the data access, Primary file group must be separate and it should be only for system objects. • Need to create one more file called secondary data file for user defined objects. • Separating the system objects will improve the performance enhance the ability to access tables in cases of serious data failures. • For frequently accessed tables containing indexes, put the tables and the indexes in separate file groups. This would enable reading the index and table data faster www.dageop.com Optimizing Queries
  • 32. Data Access Plans • Apply partitioning in big tables • Table partitioning is nothing but splitting the large table into multiple small tables so that queries have to scan less amount for data retrieving. • Consider partitioning big fat tables into different file groups where each file inside the file group is spread into separate physical disks (so that the table spans across different files in different physical disks). This would enable the database engine to read/write data operations faster. • Partitioning is very much needed for history tables. • 2 types • Physical • Logical www.dageop.com Optimizing Queries
  • 33. Data Access Plans • Create the appropriate indexes/covering indexes • Create Non-Clustered index on frequently used columns • Create index on column which is used for joining the tables • Index for foreign key columns • Create covering index for particular columns which are using frequently. • Defragment the indexes • Once index has created , it should maintain properly to avoid defragmentation. • Maintaining the index will lead to performance gain. www.dageop.com Optimizing Queries
  • 34. Data Access Plans • Identify inefficient TSQL • Don’t use * in the select statements. Mention column names while retrieving the data. It will improve the performance of the query • Avoid Deadlocks between two objects. • Diagnose the performance issue • SQL Server has many tools to Monitor and diagnose the issues. • Accessing the data will be more easier. www.dageop.com Optimizing Queries
  • 37. Auto-Parameterisation • SQL Server Query Optimizer might decide to parameterize some of the queries. • In this case the specific parameter will not make any impact on the execution plan. It will return the same execution plan. • In SQL Server 2005 forced parameterization has been introduced and is disabled by default and can be enabled in database level. • To differentiate from forced parameterization, auto-parameterization is also referred as simple parameterization. www.dageop.com Optimizing Queries
  • 39. Avoiding Recompilation of Queries www.dageop.com Optimizing Queries
  • 40. Avoiding Recompilation of Queries • In SQL Server 2005 recompiles the stored procedures, only the statement that causes recompilation is compiled, rather than the entire procedure. • Recompilation will occur in following ways, • On Schema change of objects • On Change of the SET options • On statistics change of tables. www.dageop.com Optimizing Queries
  • 41. Avoiding Recompilation of Queries • On Schema Change of Objects • Adding and dropping column, constraints, index, indexed view and trigger. • On change of the SET options • When executing the stored procedure, the compiled plan is created and it will store the environment setting of a connection (SET OPTION) . • Recompilation will occur, if the stored procedure run on different environment and with different SET option then it will not use the existing plan which it is created first time. • On statistics change of tables • SQL server maintains a modification counter for each table and index. • If the counter values exceed the defined threshold, the previously create compiled plans is considered stale plan and new plan will be created. www.dageop.com Optimizing Queries
  • 42. Avoiding Recompilation of Queries • Temporary table modification counter threshold is 6. Stored procedure will be recompiled when stored procedure create a temp table insert 6 or more rows into this table. • For Permanent table the counter threshold is 500. • We can increase the temp table counter threshold to 500 as same as permanent table. • Use table variable instead of Temporary table. www.dageop.com Optimizing Queries
  • 44. Review Query Optimiser Architecture Phases Strategies Data access plans Auto-parameterisation Avoiding recompilation of queries www.dageop.com Optimizing Queries