際際滷

際際滷Share a Scribd company logo
Template designed by
Query Processor & Statistics
Davide Mauri
dmauri@solidq.com
www.davidemauri.it
brought to you by
Works with SQL Server from 6.5, on BI from 2003
Specialized in Data Solution Architecture, Database Design,
Performance Tuning, BI
Microsoft SQL Server MVP
President of UGISS (Italian SQL Server UG)
Mentor @ SolidQ
Regular Speaker @ SQL Server events
Consulting & Training
Davide Mauri
Query Processor
Execution Plans
Data Distribution Statistics
Conclusioni
agenda
SQL 竪 un linguaggio DICHIARATIVO
4属 Generation Programming Language (4GL)
La query descrive il risultato che si vuole (ossia il 束cosa損),
senza indicare gli step per ottenerlo (ossia il 束come損)
E quindi necessario un engine che si preoccupi del 束come損

Query Processor Overview
Query Processor Overview
Richiesta
SQL
Dati
(In Fretta e
Corretti)
Magi
c
Per ogni query inviata, il ciclo di vita 竪:
Query Processor
Parse Bind Optimize Execute
Qui viene definito il percorso di risoluzione della query,
o Execution Plan
Optimizer: Percorsi
 Come arrivare da 束A損 a 束B損?
 Valuta i possibili percorsi
 E il traffico?
 E se usassi un altro mezzo?
 Da un peso a tutti i percorsi
 Prende quello meno costoso
Query Optimization e Complessit
Select o_year,
sum(case
when nation = 'BRAZIL' then volume
else 0
end) / sum(volume)
from
(
select YEAR(O_ORDERDATE) as o_year,
L_EXTENDEDPRICE * (1 - L_DISCOUNT) as volume,
n2.N_NAME as nation
from PART, SUPPLIER, LINEITEM, ORDERS, CUSTOMER, NATION n1,
NATION n2, REGION
where
P_PARTKEY = L_PARTKEY and S_SUPPKEY = L_SUPPKEY
and L_ORDERKEY = O_ORDERKEY and O_CUSTKEY = C_CUSTKEY
and C_NATIONKEY = n1.N_NATIONKEY and n1.N_REGIONKEY = R_REGIONKEY
and R_NAME = 'AMERICA and S_NATIONKEY = n2.N_NATIONKEY
and O_ORDERDATE between '1995-01-01' and '1996-12-31'
and P_TYPE = 'ECONOMY ANODIZED STEEL'
and S_ACCTBAL <= constant-1
and L_EXTENDEDPRICE <= constant-2
) as all_nations
group by o_year order by o_year
Circa 22 Milioni di piani di
esecuzioni possibili!
Query del Benchmark TPC-H
Enumerare piani logicamente equivalenti applicando regole
di equivalenza
Per ciascun piano cosi generato equivalenti, enumerare tutti
i piani fisici possibili
Stimare il costo di ciascuno dei piani alternativi cosi ottenuti
Eseguire il piano con il pi湛 basso costo stimato complessivo
Query Optimization  Main Steps
Operatori Logici
Es:JOIN
Operatori Fisici
Es:LOOP JOIN, MERGE JOIN, HASH JOIN
Query Optimization  Operatori
Query Optimization  Regole di riscrittura
Execution Plans
Ad ogni step del piano di esecuzione viene assegnato un costo
stimato
La somma totale 竪 il costo totale del piano
Viene preso il piano con il costo minore
Per query complesse non vengono valutati tutti i piani
Tecniche di 束potatura損 vengono usate per evitare di percorrere ramificazioni ipoteticamente
non efficienti.
Come pu嘆 sapere il reale costo di ogni piano?
Non pu嘆!
Necessit di stime per capire cosa ha senso fare
Cost Based Optimization
Estimated vs Actual
Ovviamente, 竪 fondamentale
che la stima sia realistica
Il percorso 竪 stimato sulla base
della quantit di dati sui cui si
deve operare
Estimated vs Actual
Dallexecution plan 竪 possibile
avere informazioni su stima e
valori attuali
E necessario avere unidea stimata dei dati con cui si andr ad operare
Tanto pi湛 竪 verosimile la stima, tanto pi湛 i piani di esecuzioni saranno
ottimali
Punti delicati
Dati distribuiti in modo non omogeneo
Dipendenze e Correlazioni
Data Distribution Statistics
Numero di righe
Numero di righe campionate
Densit dei dati
Totale
Per prefisso
Data ed Ora di campionamento
Lunghezza media della chiave
Numero di step (fino a 200)
Data Distribution Statistics
Creazione Manuale
Statistiche create automaticamente sulle colonne indicizzate
Statistiche create automaticamente da SQL Server anche
per le colonne non indicizzate
Data Distribution Statistics
Memorizzano informazioni sulla distribuzione dei dati
allinterno della colonna
Solo per la 1属 colonna
Limitati a 200 steps
Histograms
Da SQL Server 2005
SYS.STATS, SYS.STATS_COLUMNS, STAT_DATE
DBCC SHOW_STATISTICS
Da SQL Server 2008 SP2 e SQL Server 2012 SP1
SYS.DM_DB_STATS_PROPERTIES
Visualizzazione ed Analisi
Show Statistics
HEADER
DENSITY VECTOR
HISTOGRAM
123 束Alexander損
28 Rows tra 束Adams損 e 束Alexander損 (limiti esclusi)
- 15 Valori Distinti
- 1,86 Righe per ogni valore
demo
Data Distribution
Statistics At Work
Ogni cosa che va a deteriorare la qualit delle stime 竪 da evitare
Evitare di applicare funzioni sulle colonne nei predicati di ricerca
quando possibile
Attenzione a catene di join lunghe perch辿 amplificano lerrore
statistico
Lutilizzo di una tabella temporanea di 束appoggio損 pu嘆 essere di grande aiuto
Query Analysis & Optimization
Sapere leggere ed analizzare
Piani di Esecuzione
Statistiche
E fondamentale per capire e quindi ottimizare una query
Conclusioni
Grazie a tutti per la partecipazione
Riceverete il link per il download a slide e demo via email nei
prossimi giorni
Per contattarmi
dmauri@solidq.com
Grazie

More Related Content

Viewers also liked (6)

Statistic Form 2
Statistic Form 2Statistic Form 2
Statistic Form 2
guest78a7dc
Introduction to economics
Introduction to economicsIntroduction to economics
Introduction to economics
Michael Noel
MS EXCEL PPT PRESENTATION
MS EXCEL PPT PRESENTATIONMS EXCEL PPT PRESENTATION
MS EXCEL PPT PRESENTATION
Mridul Bansal
2015 Upload Campaigns Calendar - 際際滷Share
2015 Upload Campaigns Calendar - 際際滷Share2015 Upload Campaigns Calendar - 際際滷Share
2015 Upload Campaigns Calendar - 際際滷Share
際際滷Share
What to Upload to 際際滷Share
What to Upload to 際際滷ShareWhat to Upload to 際際滷Share
What to Upload to 際際滷Share
際際滷Share
Getting Started With 際際滷Share
Getting Started With 際際滷ShareGetting Started With 際際滷Share
Getting Started With 際際滷Share
際際滷Share
Statistic Form 2
Statistic Form 2Statistic Form 2
Statistic Form 2
guest78a7dc
Introduction to economics
Introduction to economicsIntroduction to economics
Introduction to economics
Michael Noel
MS EXCEL PPT PRESENTATION
MS EXCEL PPT PRESENTATIONMS EXCEL PPT PRESENTATION
MS EXCEL PPT PRESENTATION
Mridul Bansal
2015 Upload Campaigns Calendar - 際際滷Share
2015 Upload Campaigns Calendar - 際際滷Share2015 Upload Campaigns Calendar - 際際滷Share
2015 Upload Campaigns Calendar - 際際滷Share
際際滷Share
What to Upload to 際際滷Share
What to Upload to 際際滷ShareWhat to Upload to 際際滷Share
What to Upload to 際際滷Share
際際滷Share
Getting Started With 際際滷Share
Getting Started With 際際滷ShareGetting Started With 際際滷Share
Getting Started With 際際滷Share
際際滷Share

Similar to Query Processor & Statistics: A Performance Primer (20)

A performance tuning methodology
A performance tuning methodologyA performance tuning methodology
A performance tuning methodology
Gianluca Sartori
SQL Server Modern Query Processing
SQL Server Modern Query ProcessingSQL Server Modern Query Processing
SQL Server Modern Query Processing
Gianluca Hotz
Design Exploration: Sviluppo telaio per vettura formula sae
Design Exploration: Sviluppo telaio per vettura formula saeDesign Exploration: Sviluppo telaio per vettura formula sae
Design Exploration: Sviluppo telaio per vettura formula sae
Marco Basilici
IMPROVE 4.0
IMPROVE 4.0IMPROVE 4.0
IMPROVE 4.0
nextsrl
Silver Lake Analytics, soluzione Business Intelligence di Esox Informatica
Silver Lake Analytics, soluzione Business Intelligence di Esox InformaticaSilver Lake Analytics, soluzione Business Intelligence di Esox Informatica
Silver Lake Analytics, soluzione Business Intelligence di Esox Informatica
Maurizio Anselmi
Ldb 25 strumenti gis e webgis_2014-05-15 gullotta - 4 tabelle attributi e lor...
Ldb 25 strumenti gis e webgis_2014-05-15 gullotta - 4 tabelle attributi e lor...Ldb 25 strumenti gis e webgis_2014-05-15 gullotta - 4 tabelle attributi e lor...
Ldb 25 strumenti gis e webgis_2014-05-15 gullotta - 4 tabelle attributi e lor...
laboratoridalbasso
Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1
guest5c2d35b
Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1
Alberto.F
Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1
Alberto.F
Presentazione Nuvola Vertica F
Presentazione Nuvola Vertica FPresentazione Nuvola Vertica F
Presentazione Nuvola Vertica F
Alberto.F
Presentazione Nuvola Vertica Full
Presentazione Nuvola Vertica FullPresentazione Nuvola Vertica Full
Presentazione Nuvola Vertica Full
Alberto.F
Presentazione Nuvola Vertica Full New
Presentazione Nuvola Vertica Full NewPresentazione Nuvola Vertica Full New
Presentazione Nuvola Vertica Full New
Alberto.F
Quanto mi costa SQL Pool Serverless Synapse
Quanto mi costa SQL Pool Serverless SynapseQuanto mi costa SQL Pool Serverless Synapse
Quanto mi costa SQL Pool Serverless Synapse
Marco Pozzan
noSQL La nuova frontiera dei Database [DB05-S]
noSQL La nuova frontiera dei Database [DB05-S]noSQL La nuova frontiera dei Database [DB05-S]
noSQL La nuova frontiera dei Database [DB05-S]
Andrea Maddalena
Presentazione ufficiale
Presentazione ufficialePresentazione ufficiale
Presentazione ufficiale
Francesco Rizzo
Marco Liberati - Graph Analytics in the AperiTech Community
Marco Liberati - Graph Analytics in the AperiTech CommunityMarco Liberati - Graph Analytics in the AperiTech Community
Marco Liberati - Graph Analytics in the AperiTech Community
Codemotion
Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
GraphRM
GraphRM - Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
GraphRM - Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018GraphRM - Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
GraphRM - Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
Marco Liberati
A performance tuning methodology
A performance tuning methodologyA performance tuning methodology
A performance tuning methodology
Gianluca Sartori
SQL Server Modern Query Processing
SQL Server Modern Query ProcessingSQL Server Modern Query Processing
SQL Server Modern Query Processing
Gianluca Hotz
Design Exploration: Sviluppo telaio per vettura formula sae
Design Exploration: Sviluppo telaio per vettura formula saeDesign Exploration: Sviluppo telaio per vettura formula sae
Design Exploration: Sviluppo telaio per vettura formula sae
Marco Basilici
IMPROVE 4.0
IMPROVE 4.0IMPROVE 4.0
IMPROVE 4.0
nextsrl
Silver Lake Analytics, soluzione Business Intelligence di Esox Informatica
Silver Lake Analytics, soluzione Business Intelligence di Esox InformaticaSilver Lake Analytics, soluzione Business Intelligence di Esox Informatica
Silver Lake Analytics, soluzione Business Intelligence di Esox Informatica
Maurizio Anselmi
Ldb 25 strumenti gis e webgis_2014-05-15 gullotta - 4 tabelle attributi e lor...
Ldb 25 strumenti gis e webgis_2014-05-15 gullotta - 4 tabelle attributi e lor...Ldb 25 strumenti gis e webgis_2014-05-15 gullotta - 4 tabelle attributi e lor...
Ldb 25 strumenti gis e webgis_2014-05-15 gullotta - 4 tabelle attributi e lor...
laboratoridalbasso
Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1
guest5c2d35b
Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1
Alberto.F
Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1Presentazione Nuvola Vertica Full New1
Presentazione Nuvola Vertica Full New1
Alberto.F
Presentazione Nuvola Vertica F
Presentazione Nuvola Vertica FPresentazione Nuvola Vertica F
Presentazione Nuvola Vertica F
Alberto.F
Presentazione Nuvola Vertica Full
Presentazione Nuvola Vertica FullPresentazione Nuvola Vertica Full
Presentazione Nuvola Vertica Full
Alberto.F
Presentazione Nuvola Vertica Full New
Presentazione Nuvola Vertica Full NewPresentazione Nuvola Vertica Full New
Presentazione Nuvola Vertica Full New
Alberto.F
Quanto mi costa SQL Pool Serverless Synapse
Quanto mi costa SQL Pool Serverless SynapseQuanto mi costa SQL Pool Serverless Synapse
Quanto mi costa SQL Pool Serverless Synapse
Marco Pozzan
noSQL La nuova frontiera dei Database [DB05-S]
noSQL La nuova frontiera dei Database [DB05-S]noSQL La nuova frontiera dei Database [DB05-S]
noSQL La nuova frontiera dei Database [DB05-S]
Andrea Maddalena
Presentazione ufficiale
Presentazione ufficialePresentazione ufficiale
Presentazione ufficiale
Francesco Rizzo
Marco Liberati - Graph Analytics in the AperiTech Community
Marco Liberati - Graph Analytics in the AperiTech CommunityMarco Liberati - Graph Analytics in the AperiTech Community
Marco Liberati - Graph Analytics in the AperiTech Community
Codemotion
Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
GraphRM
GraphRM - Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
GraphRM - Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018GraphRM - Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
GraphRM - Costruiamo un motore di raccomandazione con Neo4J - Workshop 25/1/2018
Marco Liberati

More from Davide Mauri (20)

Azure serverless Full-Stack kickstart
Azure serverless Full-Stack kickstartAzure serverless Full-Stack kickstart
Azure serverless Full-Stack kickstart
Davide Mauri
Agile Data Warehousing
Agile Data WarehousingAgile Data Warehousing
Agile Data Warehousing
Davide Mauri
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
Davide Mauri
When indexes are not enough
When indexes are not enoughWhen indexes are not enough
When indexes are not enough
Davide Mauri
Building a Real-Time IoT monitoring application with Azure
Building a Real-Time IoT monitoring application with AzureBuilding a Real-Time IoT monitoring application with Azure
Building a Real-Time IoT monitoring application with Azure
Davide Mauri
SSIS Monitoring Deep Dive
SSIS Monitoring Deep DiveSSIS Monitoring Deep Dive
SSIS Monitoring Deep Dive
Davide Mauri
Azure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server 2016 JSONAzure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server 2016 JSON
Davide Mauri
SQL Server & SQL Azure Temporal Tables - V2
SQL Server & SQL Azure Temporal Tables - V2SQL Server & SQL Azure Temporal Tables - V2
SQL Server & SQL Azure Temporal Tables - V2
Davide Mauri
SQL Server 2016 Temporal Tables
SQL Server 2016 Temporal TablesSQL Server 2016 Temporal Tables
SQL Server 2016 Temporal Tables
Davide Mauri
SQL Server 2016 What's New For Developers
SQL Server 2016  What's New For DevelopersSQL Server 2016  What's New For Developers
SQL Server 2016 What's New For Developers
Davide Mauri
Azure Stream Analytics
Azure Stream AnalyticsAzure Stream Analytics
Azure Stream Analytics
Davide Mauri
Azure Machine Learning
Azure Machine LearningAzure Machine Learning
Azure Machine Learning
Davide Mauri
Dashboarding with Microsoft: Datazen & Power BI
Dashboarding with Microsoft: Datazen & Power BIDashboarding with Microsoft: Datazen & Power BI
Dashboarding with Microsoft: Datazen & Power BI
Davide Mauri
Azure ML: from basic to integration with custom applications
Azure ML: from basic to integration with custom applicationsAzure ML: from basic to integration with custom applications
Azure ML: from basic to integration with custom applications
Davide Mauri
Event Hub & Azure Stream Analytics
Event Hub & Azure Stream AnalyticsEvent Hub & Azure Stream Analytics
Event Hub & Azure Stream Analytics
Davide Mauri
SQL Server 2016 JSON
SQL Server 2016 JSONSQL Server 2016 JSON
SQL Server 2016 JSON
Davide Mauri
SSIS Monitoring Deep Dive
SSIS Monitoring Deep DiveSSIS Monitoring Deep Dive
SSIS Monitoring Deep Dive
Davide Mauri
Real Time Power BI
Real Time Power BIReal Time Power BI
Real Time Power BI
Davide Mauri
AzureML - Creating and Using Machine Learning Solutions (Italian)
AzureML - Creating and Using Machine Learning Solutions (Italian)AzureML - Creating and Using Machine Learning Solutions (Italian)
AzureML - Creating and Using Machine Learning Solutions (Italian)
Davide Mauri
Datarace: IoT e Big Data (Italian)
Datarace: IoT e Big Data (Italian)Datarace: IoT e Big Data (Italian)
Datarace: IoT e Big Data (Italian)
Davide Mauri
Azure serverless Full-Stack kickstart
Azure serverless Full-Stack kickstartAzure serverless Full-Stack kickstart
Azure serverless Full-Stack kickstart
Davide Mauri
Agile Data Warehousing
Agile Data WarehousingAgile Data Warehousing
Agile Data Warehousing
Davide Mauri
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
Davide Mauri
When indexes are not enough
When indexes are not enoughWhen indexes are not enough
When indexes are not enough
Davide Mauri
Building a Real-Time IoT monitoring application with Azure
Building a Real-Time IoT monitoring application with AzureBuilding a Real-Time IoT monitoring application with Azure
Building a Real-Time IoT monitoring application with Azure
Davide Mauri
SSIS Monitoring Deep Dive
SSIS Monitoring Deep DiveSSIS Monitoring Deep Dive
SSIS Monitoring Deep Dive
Davide Mauri
Azure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server 2016 JSONAzure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server 2016 JSON
Davide Mauri
SQL Server & SQL Azure Temporal Tables - V2
SQL Server & SQL Azure Temporal Tables - V2SQL Server & SQL Azure Temporal Tables - V2
SQL Server & SQL Azure Temporal Tables - V2
Davide Mauri
SQL Server 2016 Temporal Tables
SQL Server 2016 Temporal TablesSQL Server 2016 Temporal Tables
SQL Server 2016 Temporal Tables
Davide Mauri
SQL Server 2016 What's New For Developers
SQL Server 2016  What's New For DevelopersSQL Server 2016  What's New For Developers
SQL Server 2016 What's New For Developers
Davide Mauri
Azure Stream Analytics
Azure Stream AnalyticsAzure Stream Analytics
Azure Stream Analytics
Davide Mauri
Azure Machine Learning
Azure Machine LearningAzure Machine Learning
Azure Machine Learning
Davide Mauri
Dashboarding with Microsoft: Datazen & Power BI
Dashboarding with Microsoft: Datazen & Power BIDashboarding with Microsoft: Datazen & Power BI
Dashboarding with Microsoft: Datazen & Power BI
Davide Mauri
Azure ML: from basic to integration with custom applications
Azure ML: from basic to integration with custom applicationsAzure ML: from basic to integration with custom applications
Azure ML: from basic to integration with custom applications
Davide Mauri
Event Hub & Azure Stream Analytics
Event Hub & Azure Stream AnalyticsEvent Hub & Azure Stream Analytics
Event Hub & Azure Stream Analytics
Davide Mauri
SQL Server 2016 JSON
SQL Server 2016 JSONSQL Server 2016 JSON
SQL Server 2016 JSON
Davide Mauri
SSIS Monitoring Deep Dive
SSIS Monitoring Deep DiveSSIS Monitoring Deep Dive
SSIS Monitoring Deep Dive
Davide Mauri
Real Time Power BI
Real Time Power BIReal Time Power BI
Real Time Power BI
Davide Mauri
AzureML - Creating and Using Machine Learning Solutions (Italian)
AzureML - Creating and Using Machine Learning Solutions (Italian)AzureML - Creating and Using Machine Learning Solutions (Italian)
AzureML - Creating and Using Machine Learning Solutions (Italian)
Davide Mauri
Datarace: IoT e Big Data (Italian)
Datarace: IoT e Big Data (Italian)Datarace: IoT e Big Data (Italian)
Datarace: IoT e Big Data (Italian)
Davide Mauri

Query Processor & Statistics: A Performance Primer

  • 1. Template designed by Query Processor & Statistics Davide Mauri dmauri@solidq.com www.davidemauri.it
  • 3. Works with SQL Server from 6.5, on BI from 2003 Specialized in Data Solution Architecture, Database Design, Performance Tuning, BI Microsoft SQL Server MVP President of UGISS (Italian SQL Server UG) Mentor @ SolidQ Regular Speaker @ SQL Server events Consulting & Training Davide Mauri
  • 4. Query Processor Execution Plans Data Distribution Statistics Conclusioni agenda
  • 5. SQL 竪 un linguaggio DICHIARATIVO 4属 Generation Programming Language (4GL) La query descrive il risultato che si vuole (ossia il 束cosa損), senza indicare gli step per ottenerlo (ossia il 束come損) E quindi necessario un engine che si preoccupi del 束come損 Query Processor Overview
  • 7. Per ogni query inviata, il ciclo di vita 竪: Query Processor Parse Bind Optimize Execute Qui viene definito il percorso di risoluzione della query, o Execution Plan
  • 8. Optimizer: Percorsi Come arrivare da 束A損 a 束B損? Valuta i possibili percorsi E il traffico? E se usassi un altro mezzo? Da un peso a tutti i percorsi Prende quello meno costoso
  • 9. Query Optimization e Complessit Select o_year, sum(case when nation = 'BRAZIL' then volume else 0 end) / sum(volume) from ( select YEAR(O_ORDERDATE) as o_year, L_EXTENDEDPRICE * (1 - L_DISCOUNT) as volume, n2.N_NAME as nation from PART, SUPPLIER, LINEITEM, ORDERS, CUSTOMER, NATION n1, NATION n2, REGION where P_PARTKEY = L_PARTKEY and S_SUPPKEY = L_SUPPKEY and L_ORDERKEY = O_ORDERKEY and O_CUSTKEY = C_CUSTKEY and C_NATIONKEY = n1.N_NATIONKEY and n1.N_REGIONKEY = R_REGIONKEY and R_NAME = 'AMERICA and S_NATIONKEY = n2.N_NATIONKEY and O_ORDERDATE between '1995-01-01' and '1996-12-31' and P_TYPE = 'ECONOMY ANODIZED STEEL' and S_ACCTBAL <= constant-1 and L_EXTENDEDPRICE <= constant-2 ) as all_nations group by o_year order by o_year Circa 22 Milioni di piani di esecuzioni possibili! Query del Benchmark TPC-H
  • 10. Enumerare piani logicamente equivalenti applicando regole di equivalenza Per ciascun piano cosi generato equivalenti, enumerare tutti i piani fisici possibili Stimare il costo di ciascuno dei piani alternativi cosi ottenuti Eseguire il piano con il pi湛 basso costo stimato complessivo Query Optimization Main Steps
  • 11. Operatori Logici Es:JOIN Operatori Fisici Es:LOOP JOIN, MERGE JOIN, HASH JOIN Query Optimization Operatori
  • 12. Query Optimization Regole di riscrittura
  • 14. Ad ogni step del piano di esecuzione viene assegnato un costo stimato La somma totale 竪 il costo totale del piano Viene preso il piano con il costo minore Per query complesse non vengono valutati tutti i piani Tecniche di 束potatura損 vengono usate per evitare di percorrere ramificazioni ipoteticamente non efficienti. Come pu嘆 sapere il reale costo di ogni piano? Non pu嘆! Necessit di stime per capire cosa ha senso fare Cost Based Optimization
  • 15. Estimated vs Actual Ovviamente, 竪 fondamentale che la stima sia realistica Il percorso 竪 stimato sulla base della quantit di dati sui cui si deve operare
  • 16. Estimated vs Actual Dallexecution plan 竪 possibile avere informazioni su stima e valori attuali
  • 17. E necessario avere unidea stimata dei dati con cui si andr ad operare Tanto pi湛 竪 verosimile la stima, tanto pi湛 i piani di esecuzioni saranno ottimali Punti delicati Dati distribuiti in modo non omogeneo Dipendenze e Correlazioni Data Distribution Statistics
  • 18. Numero di righe Numero di righe campionate Densit dei dati Totale Per prefisso Data ed Ora di campionamento Lunghezza media della chiave Numero di step (fino a 200) Data Distribution Statistics
  • 19. Creazione Manuale Statistiche create automaticamente sulle colonne indicizzate Statistiche create automaticamente da SQL Server anche per le colonne non indicizzate Data Distribution Statistics
  • 20. Memorizzano informazioni sulla distribuzione dei dati allinterno della colonna Solo per la 1属 colonna Limitati a 200 steps Histograms
  • 21. Da SQL Server 2005 SYS.STATS, SYS.STATS_COLUMNS, STAT_DATE DBCC SHOW_STATISTICS Da SQL Server 2008 SP2 e SQL Server 2012 SP1 SYS.DM_DB_STATS_PROPERTIES Visualizzazione ed Analisi
  • 22. Show Statistics HEADER DENSITY VECTOR HISTOGRAM 123 束Alexander損 28 Rows tra 束Adams損 e 束Alexander損 (limiti esclusi) - 15 Valori Distinti - 1,86 Righe per ogni valore
  • 24. Ogni cosa che va a deteriorare la qualit delle stime 竪 da evitare Evitare di applicare funzioni sulle colonne nei predicati di ricerca quando possibile Attenzione a catene di join lunghe perch辿 amplificano lerrore statistico Lutilizzo di una tabella temporanea di 束appoggio損 pu嘆 essere di grande aiuto Query Analysis & Optimization
  • 25. Sapere leggere ed analizzare Piani di Esecuzione Statistiche E fondamentale per capire e quindi ottimizare una query Conclusioni
  • 26. Grazie a tutti per la partecipazione Riceverete il link per il download a slide e demo via email nei prossimi giorni Per contattarmi dmauri@solidq.com Grazie

Editor's Notes

  • #9: Usare la mappa per arrivare da Milano a Roma.