Download presentation
Presentation is loading. Please wait.
Published byRudolf Webster Modified over 8 years ago
1
Inside Query Processor Sort Dmitry Pilugin SQL Server Developer, MVP pilugin@inbox.ru (RU) http://www.somewheresomehow.ru/ (EN) http://www.queryprocessor.com/
2
Внутри Оптимизатора Запросов ДОКЛАДСОБЫТИЕМЕСТОДАТА Внутри Оптимизатора ЗапросовSQL Saturday #178Москва17.11.2012 Внутри Оптимизатора Запросов: КардинальностьSQL Saturday #261Москва30.11.2013 Внутри Оптимизатора Запросов: СтоимостьSQL Saturday #259Харьков07.12.2013 Внутри Оптимизатора Запросов: CE SQL Server 2014SQL Saturday #270Краснодар26.04.2014 Внутри Оптимизатора Запросов: Соединения (часть 1,2)24 HOP RussiaOnline18.03.2015 Внутри Оптимизатора Запросов: Поиск и ПросмотрSQL Saturday #337Киев23.05.2015 Внутри Оптимизатора Запросов: СортировкаSQL Saturday #398Краснодар06.06.2015 Внутри Оптимизатора Запросов: АгрегацияVirtual ChapterOnline08.2015 Внутри Оптимизатора Запросов: Row Goal--- Внутри Оптимизатора Запросов: Параллелизм--- Внутри Оптимизатора Запросов: Bitmap фильтры--- Внутри Оптимизатора Запросов: Spool--- Внутри Оптимизатора Запросов: DML (часть 1,2)--- Внутри Оптимизатора Запросов: Distributed Queries---
3
Today’s session scope Questions What kind of sorts there are in SQL Server? When does SQL Server use sort? Language Processing and Execution Query Optimizer Query Executor Storage Engine Access Methods, Buffer Pool, Locking, Transactions, … Storage Engine Access Methods, Buffer Pool, Locking, Transactions, … SQL OS Threads, Memory Management, … SQL OS Threads, Memory Management, … Utilities (DBCC, Backup/Restore, BCP,…) Metadata, Type System, Expression Services Components Row Store Column Store In-Memory Engines
4
Query Processing Logical Operators LogOp_OrderBy LogOp_Get LogOp_OrderBy LogOp_Get Cardinality Estimation Framework, Rules Simplify, Derive cardinality, Reorder Physical Operators Plan Operators PhyOp_Sort PhyOp_Range PhyOp_Sort PhyOp_Range XteSort XteRange XteSort XteRange Memo, Cost Utils, Rules Search Explore, Cost, Pick the cheapest Query Tree Extract, Create Plan Executable Operators QScanSortNew QScanRangeNew QScanSortNew QScanRangeNew Exec Context Execute Plan Query Text select * from t1 order by a select * from t1 order by a Parser, Algebrizer Check syntax, Parse, Bind Access Methods, Sort Methods Access Methods, Sort Methods e. g. quick sort, merge sort Rowset Sorted, Sorting Algorithms Get and Sort Data LPEQOQEAM, SM QPSE …
5
Demo 1 Observing Internals
6
Query Processing Logical Operators LogOp_OrderBy LogOp_Get LogOp_OrderBy LogOp_Get Cardinality Estimation Framework, Rules Simplify, Derive cardinality, Reorder Physical Operators Plan Operators PhyOp_Sort PhyOp_Range PhyOp_Sort PhyOp_Range XteSort XteRange XteSort XteRange Memo, Cost Utils, Rules Search Explore, Cost, Pick the cheapest Query Tree Extract, Create Plan Executable Operators QScanSortNew QScanRangeNew QScanSortNew QScanRangeNew Exec Context Execute Plan Query Text select * from t1 order by a select * from t1 order by a Parser, Algebrizer Check syntax, Parse, Bind Access Methods, Sort Methods Access Methods, Sort Methods e. g. quick sort, merge sort Rowset Sorted, Sorting Algorithms Get and Sort Data LPEQOQEAM, SM QPSE …
7
Parsing, Simplification order by over(order by) group by, distinct group by, distinct join DML DDL LogOp_ OrderBy LogOp_ SequenceProject LogOp_ GbAgg LogOp_ Join Various DML operators Various DML operators Various DDL trees Various DDL trees Logical Operators Physical Operators Plan Operators Executable Operators Query Text Access Methods, Sort Methods Access Methods, Sort Methods Demands sort May demand sort Order by Demands sort if ORDER BY argument is not const Group by, Distinct, Join May demand sort in physical implementation DML, DDL May demand sort, depends on the updated/created data structures May demand sort
8
Optimization Logical Operators Physical Operators Plan Operators Executable Operators Query Text Access Methods, Sort Methods Access Methods, Sort Methods No pre-sorted input Cost effective LogOp_ OrderBy LogOp_ SequenceProject LogOp_ GbAgg LogOp_ Join Various DML operators Various DML operators Various DDL trees Various DDL trees PhyOp_ Sort PhyOp_ StreamGbAgg PhyOp_ MergeJoin DML Request Sort DDL Plan Cost DML Request Sort (Cost) DDL Plan No pre-sorted input PhyOp_ Apply NL Explicit Batch Sort (Cost)
9
Post Optimization Rewrite Logical Operators Physical Operators Plan Operators Executable Operators Query Text Access Methods, Sort Methods Access Methods, Sort Methods Physical operators to plan operators Execution engine format transformation with extra optimization tricks PhyOp_ Sort PhyOp_ Top PhyOp_ Apply DDL Plan NL Implicit Batch Sort PhyOp_ Sort CXteTopSort CXteBatchSort CXteIndexSort PhyOp_ Sort CXteSort
10
Demo 2 Post Optimization Rewrite
11
Executable Compilation Logical Operators Physical Operators Plan Operators Executable Operators Query Text Access Methods, Sort Methods Access Methods, Sort Methods Query plan compilation to executable plan CXteTopSort CXteBatchSort CXteIndexSort CXteSort CQScan TopSortNew CQScan BatchSortNew CQScan IndexSortNew CQScan InMemSortNew CQScan SortNew CQScan PartitionSortNew CQScan XSortNew Sorting input - list of constants (list of 500 elements max) Clustered partitioned Columnstore index insert (IO Cost = 0, TF 2417 – disables) Parallel plan Generic iterator First N rows Implicit in-mem NL sort Index creation
12
Demo 3 Sorting Iterators
13
Idea of Internal Merge Sort 1 1 3 3 6 6 8 8 2 2 4 4 5 5 7 7 Sorted 1 Sorted 2 Compare 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 1 3 3 6 6 8 8 2 2 4 4 5 5 7 7 Single Element is a Sorted Array of Length 1 Pass 0 Pass 1 Pass 2
14
Idea of Two-Way External Merge Sort 1,3 6,5 4,2 7,8 6,9 2,1 2,6 … … Very simple basic idea illustration, fan-in 2, a lot of optimizations possible (e.g. replacement selection run generation) Divide and conquer strategy using memory as a buffer and disk to save runs Sort data more than available memory input pages 1,3 pass 1 5,6 2,4 7,8 1,2 6,9 2,6 … … 2-page runs 1,3 5,6 2,4 7,8 6,9 1,2 2,6 … … 1-page runs pass 0 1,2 pass 2 3,4 5,6 7,8 1,2 6,9 2,6 … … 4-page runs 1,1 2,2 2,3 4,5 6,6 6,7 8,9 … … pass 3 8-page runs
15
SQL Server General Sort Algorithm Variation of external merge sort Takes care of variable record length Optimizes IO performance Balances IO/Memory usage Under estimated row count and long variable strings – may produce spill Data might be spilled even if estates are good (EstimatedRowSize * Rows) / PageSize Rows / LmRecordsPerPage +25% (or 100%) PageSize constant equals 8192 lm_records_per_page constant equals 204 in 2014 Sort Memory Grant in Pages (approximately)
16
Demo 4 Spills
17
Spill Spill if: input_phase_max_buffers – 8*input_phase_bobs < sort_data_size_in_pages Input Buffers Input bobs (bob – big output buffer) 1 buffer = 8 pages Other buffers Used Memory Used Memory
18
Avoiding Sort Index creation Column order matters in case of composite index Remove ordering demand if applicable Rewrite query to use operators that doesn’t demand sorted input Rewrite query to use order preserving operators TFs 2340 – Disable Nested Loops Implicit Batch Sort on the Post Optimization Rewrite Phase 2335 – Generate more conservative query plan for the servers with big amount of memory, reducing big memory consumption operators like Sort (or Hash) 9115 – Disable NL Implicit/Explicit Batch Sort Optimization as well as Prefetch Optimization (undocumented – test only) 8795 – Disable DML Request Sort (undocumented – test only)
19
Thank you! pilugin@inbox.ru http://twitter.com/@SomewereSomehow (RU) http://www.somewheresomehow.ru/ (EN) http://www.queryprocessor.com/ Mmm… Questions…
20
Спонсоры Золотой спонсор: Спонсоры розыгрыша призов :
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.