Firebird 3.0 statistics and plans

Similar documents
Firebird Tour 2017: Performance. Vlad Khorsun, Firebird Project

Firebird Tour 2017: Performance. Vlad Khorsun, Firebird Project

Firebird in 2011/2012: Development Review

Understanding the Optimizer

Advanced Trace API. Vlad Khorsun, Firebird Project. Advanced Trace API. Firebird Tour 2017

Diagnosing and fixing Firebird performance problems

How Firebird transactions work

Firebird databases recovery and protection for enterprises and ISVs. Alexey Kovyazin, IBSurgeon

Firebird performance degradation: tests, myths and truth IBSurgeon, 2014

TUNING LINUX, WINDOWS AND FIREBIRD FOR HEAVY WORKLOAD

File Structures and Indexing

Firebird performance counters in details

Indexing. Jan Chomicki University at Buffalo. Jan Chomicki () Indexing 1 / 25

Datenbanksysteme II: Caching and File Structures. Ulf Leser

MySQL Indexing. Best Practices. Peter Zaitsev, CEO Percona Inc August 15, 2012

Storage hierarchy. Textbook: chapters 11, 12, and 13

Kathleen Durant PhD Northeastern University CS Indexes

Chapter 12: Indexing and Hashing. Basic Concepts

Chapter 5: Physical Database Design. Designing Physical Files

Outline. Database Management and Tuning. Outline. Join Strategies Running Example. Index Tuning. Johann Gamper. Unit 6 April 12, 2012

Chapter 12: Indexing and Hashing

PS2 out today. Lab 2 out today. Lab 1 due today - how was it?

Database Management and Tuning

Outline. Database Management and Tuning. What is an Index? Key of an Index. Index Tuning. Johann Gamper. Unit 4

University of Waterloo Midterm Examination Sample Solution

Outline. Database Management and Tuning. Index Data Structures. Outline. Index Tuning. Johann Gamper. Unit 5

Administração e Optimização de Bases de Dados 2012/2013 Index Tuning

Something to think about. Problems. Purpose. Vocabulary. Query Evaluation Techniques for large DB. Part 1. Fact:

COMP 430 Intro. to Database Systems. Indexing

Oracle DB-Tuning Essentials

Mastering the art of indexing

RAID in Practice, Overview of Indexing

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

MySQL Indexing. Best Practices for MySQL 5.6. Peter Zaitsev CEO, Percona MySQL Connect Sep 22, 2013 San Francisco,CA

Index Tuning. Index. An index is a data structure that supports efficient access to data. Matching records. Condition on attribute value

CS 405G: Introduction to Database Systems. Storage

Database Management System

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

Outline. Database Tuning. Join Strategies Running Example. Outline. Index Tuning. Nikolaus Augsten. Unit 6 WS 2014/2015

Eternal Story on Temporary Objects

Chapter 12: Indexing and Hashing

Query Processing and Query Optimization. Prof Monika Shah

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Database Systems: Fall 2015 Quiz I

Chapter 11: Storage and File Structure. Silberschatz, Korth and Sudarshan Updated by Bird and Tanin

Query Processing: The Basics. External Sorting

Chapter 12: Query Processing

Relational Database Index Design and the Optimizers

Physical Database Design: Outline

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

CMPUT 391 Database Management Systems. Query Processing: The Basics. Textbook: Chapter 10. (first edition: Chapter 13) University of Alberta 1

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Chapter 12: Query Processing. Chapter 12: Query Processing

Outline. Query Types

Covering indexes. Stéphane Combaudon - SQLI

Query Evaluation Overview, cont.

Systems Infrastructure for Data Science. Web Science Group Uni Freiburg WS 2014/15

Review. Support for data retrieval at the physical level:

Overview of Storage and Indexing

CSE 530A. B+ Trees. Washington University Fall 2013

Oracle Database 11g: SQL Tuning Workshop

Sepand Gojgini. ColumnStore Index Primer

Querying Data with Transact SQL

Practical MySQL indexing guidelines

Advanced Database Systems

Query Optimizer MySQL vs. PostgreSQL

Course Outline. SQL Server Performance & Tuning For Developers. Course Description: Pre-requisites: Course Content: Performance & Tuning.

Query Evaluation Overview, cont.

Indexing Methods. Lecture 9. Storage Requirements of Databases

Evaluation of Relational Operations

20 Essential Oracle SQL and PL/SQL Tuning Tips. John Mullins

Tips for success. Common mistakes in application development with Firebird and how to avoid them. Pavel Císař IBPhoenix

Chapter 13: Query Processing

Examples of Physical Query Plan Alternatives. Selected Material from Chapters 12, 14 and 15

7. Query Processing and Optimization

Hashing IV and Course Overview

Query Processing. Debapriyo Majumdar Indian Sta4s4cal Ins4tute Kolkata DBMS PGDBA 2016

Hash table example. B+ Tree Index by Example Recall binary trees from CSE 143! Clustered vs Unclustered. Example

State of MariaDB. Igor Babaev Notice: MySQL is a registered trademark of Sun Microsystems, Inc.

Query Processing with Indexes. Announcements (February 24) Review. CPS 216 Advanced Database Systems

DATABASE PERFORMANCE AND INDEXES. CS121: Relational Databases Fall 2017 Lecture 11

Implementing Relational Operators: Selection, Projection, Join. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

CS34800 Information Systems

Topics to Learn. Important concepts. Tree-based index. Hash-based index

Advanced Oracle SQL Tuning v3.0 by Tanel Poder

QUIZ: Is either set of attributes a superkey? A candidate key? Source:

Selection Queries. to answer a selection query (ssn=10) needs to traverse a full path.

Introduction to Database Systems CSE 414. Lecture 26: More Indexes and Operator Costs

Query optimization. Elena Baralis, Silvia Chiusano Politecnico di Torino. DBMS Architecture D B M G. Database Management Systems. Pag.

Oracle 9i Application Development and Tuning

! A relational algebra expression may have many equivalent. ! Cost is generally measured as total elapsed time for

Chapter 13: Query Processing Basic Steps in Query Processing

PostgreSQL Query Optimization. Step by step techniques. Ilya Kosmodemiansky

External Sorting. Chapter 13. Comp 521 Files and Databases Fall

Indexing. Week 14, Spring Edited by M. Naci Akkøk, , Contains slides from 8-9. April 2002 by Hector Garcia-Molina, Vera Goebel

Oracle Database Performance Tuning, Benchmarks & Replication

(Storage System) Access Methods Buffer Manager

6232B: Implementing a Microsoft SQL Server 2008 R2 Database

Database Applications (15-415)

Transcription:

1 Firebird 3.0 statistics and plans Dmitry Kuzmenko, IBSurgeon

2 Firebird 2017 Tour: Performance Optimization Firebird Tour 2017 is organized by Firebird Project, IBSurgeon and IBPhoenix, and devoted to Firebird Performance. The Platinum sponsor is Moscow Exchange Tour's locations and dates: October 3, 2017 Prague, Czech Republic October 5, 2017 Bad Sassendorf, Germany November 3, 2017 Moscow, Russia

3 Platinum Sponsor Sponsor of «Firebird 2.5 SQL Language Reference» «Firebird 3.0 SQL Language Reference» «Firebird 3.0 Developer Guide» «Firebird 3.0 Operations Guide» Sponsor of Firebird 2017 Tour seminars www.moex.com

4 Replication, Recovery and Optimization for Firebird and InterBase since 2002 Platinum Sponsor of Firebird Foundation Based in Moscow, Russia www.ib-aid.com

5 Agenda New elements for table statistics Including blob information New elements for index statistics Plan elements Explained plans Optimizer enhancements Firebird 3 and 4

6 NEW STATISTICS ELEMENTS

7 How to get statistics Gstat r Gstat r t tablename1 t tablename2 Services API HQbird Database Analyst

8 Tables JOB (129) Primary pointer page: 228, Index root page: 229 Total formats: 1, used formats: 1 Average record length: 65.58, total records: 31 Average version length: 0.00, total versions: 0, max versions: 0 Average fragment length: 0.00, total fragments: 0, max fragments: 0 Average unpacked length: 96.00, compression ratio: 1.46 Pointer pages: 1, data page slots: 3 Data pages: 3, average fill: 72% Primary pages: 1, secondary pages: 2, swept pages: 1 Empty pages: 0, full pages: 1 Blobs: 39, total length: 4840, blob pages: 0 Level 0: 39, Level 1: 0, Level 2: 0 Fill distribution: 0-19% = 0 20-39% = 0 40-59% = 1 60-79% = 1 80-99% = 1

9 Total formats: 1, used formats: 1 Number of table structure changes (except triggers and indices) Limited to 256 After limit exceeded, you need to do backup/restore Used formats how many formats used by primary records. Number of all used formats is unknown (less or equal of Formats)

10 Primary, Secondary new storage concept Primary Primary record versions - insert Backversions Secondary Backversions, record fragments - update/delete Small blobs (level 0)

11 Swept Processed by garbage collector or sweep Sweep skips swept pages Used for primary pages When no work for garbage collector Cleared when new version is created on the data page

12 Average fragment length: 0.00, total fragments: 0, max fragments: 0 Fragments - records that does not fit at a page Big records Big record+versions chain Max fragments the most number of fragments for some record

13 Packing Average unpacked length: 96.00, compression ratio: 1.46 Average record length: 65.58 96 / 65.58 = 1.46

14 Empty, full Full when there is no space to place new record (version) Empty empty, while not gathered into 8- pages extent. These pages are marked as unused only when all pages in extent are empty.

15 Blobs, blob levels Blobs: 463, total length: 248371310, blob pages: 15410 Level 0: 0, Level 1: 463, Level 2: 0 Level 0 fits to the data page. Record data is sparsed. Page of 4096 bytes can hold blob of 4052 bytes Level 1 pointers to the blob pages. Blobs bigger than page size, and up to ~4mb size can rarefact data same way as 4052 blobs. Because 4052 bytes can fit 1013 links to the blob pages Level 2 pointer to the blob pointer page, that contain pointers to the blob pages

16 Level 1 pointers to the blob pages BLOB BLOB Levels Level 0 at data page, as record data BLOB record BLOB page with data BLOB page with data BLOB pointer page Data page BLOB record BLOB data page Level 2 pointers to pointers (BLOB pages with pointers) BLOB Record BLOB pointer page BLOB data page BLOB data page BLOB data page

17 What was earlier (ODS < 12) Small blobs (level 0) sparse record data, because they fit at data page Records could be highly sparsed, causing performance loss on scans without accessing blobs To avoid this small blobs needed to be moved to separate table, linked 1:1 to the main table

18

19 Blob level 0 Now blob record, i.e. blob contents, stored at a secondary page, while record is at primary page Eliminates data page sparse Makes scan operations much faster Anything that does not touch blob data

20 Blob level 1 Blobs: 463, total length: 248371310, blob pages: 15410 Level 0: 0, Level 1: 463, Level 2: 0 Here all blobs Bigger than page size (16k) (no Level 0) Less than 64mb (no level 2) Do not interleave record data

21 Blob level 2 Blob record points to pages that contain pointers to blob pages For 16k page size blob size must be bigger than 64mb to get Level 2

22 Test Page size = 8192 Tables, 100k records, random data A no blobs at all B random blobs from 128 to 1024 bytes size Fits at data page level 0 C fixed blobs 1024 bytes size Fits at data page level 0 D fixed blobs 9000 bytes size Goes to separate blob page level 1 Reading all fields except blob Fetch all, select count

23 Reading speed, ms 0,800 0,700 0,600 0,500 0,400 0,300 0,200 0,100 0,000 A B C D FB 2.5 FB 3.0 Performance with B and C may decrease up to 22%

24 SELECT * FROM C Fetch All PLAN (C NATURAL) ------ Performance info ------ Prepare time = 16ms Execute time = 875ms Memory buffers = 256 Reads from disk to cache = 1 066 Writes from cache to disk = 0 Fetches from cache = 104 274

25 Page reads 16000 14000 12000 10000 8000 6000 4000 2000 0 A B C D FB 2.5 FB 3.0

26 Number of pages FB 2.5 FB 3.0 PP SP 15352 14286 14286 8658 8904 7832 957 960 1555 1560 960 1072 1066 1069 0 491 A B C D

27 Results Now blob record is placed at secondary page Blobs and versions may be mixed at secondary pages only Scanning data without blobs is faster and uses less I/O Level 0 in FB 3.0 by performance similar to blobs Level 1 in Firebird 2.5 Scanning - 2x times less fetches Select count - 25% less fetches

28 Indices Index MAXSALX (2) Root page: 324, depth: 1, leaf buckets: 1, nodes: 31 Average node length: 14.74, total dup: 5, max dup: 1 Average key length: 13.71, compression ratio: 1.37 Average prefix length: 7.87, average data length: 10.90 Clustering factor: 1, ratio: 0.03 Fill distribution: 0-19% = 1 20-39% = 0 40-59% = 0 60-79% = 0 80-99% = 0

29 Node, key, prefix Average node length: 14.74 Average key length: 13.71, compression ratio: 1.37 Average prefix length: 7.87, average data length: 10.90 Node prefix + key + record number Key indexed data (column value) Compression board 0 5 board boarding 5 3 ing boarded 5 2 ed Sequential numbers may be compressed up to 8 times in comparison with GUIDs

30 Clustering Factor Data Page 12 Index Key 1 Data Page 25 Index Key 2 Data Page 12 Data Page 28 Index Key 3 Data Page 13 Data Page 44 Index Key 4 Data Page 14 Data Page 57 Index Key 5 Bad Clustering Factor = 5 guid primary key Good Clustering Factor = 3 int/bigint primary key

31 Example: Clustering factor: 1066, ratio: 0.01 nodes: 100000 Primary pages: 1066 Ratio = Clustering factor / Nodes = 1066/100000 = 0.01 Ratio * keys in range = future DP reads The best Clustering factor equal to the number of primary data pages

32 Example: Clustering factor: 1066, ratio: 0.01 Clustering factor jumps to different primary data pages while walking through index 1066 with 1066 primary pages means the best Another index may have worse clustering factor For example, 2132, i.e. will be 2x primary page reads Cache size will these data pages fit, or not? (will be re-read from disk)

33 Clustering factor Clustering factor closer to the primary data pages number good Ratio less is better How clustering factor affects performance? See below

34 PLAN ELEMENTS

35 Plan elements tablename NATURAL tablename INDEX indexname Tablename ORDER indexname JOIN HASH JOIN SORT SORT MERGE

36 PLAN (TABLE NATURAL) select * from employee PLAN (EMPLOYEE NATURAL) The fastest way to read data Record 1 Record 2 Record 3 Record 3 Record 4 Record 5

37 PLAN (TABLE INDEX indexname) Search for first key applying to condition Collect all row numbers for keys, that applying to condition Sort array of row numbers Fetch records from sorted array of row numbers select * from employee where emp_no > 5 PLAN (EMPLOYEE INDEX (RDB$PRIMARY7))

38 Index -> Table Root Page Pointer Page Keys (Leaf page) Data Pages A C A r55 B r28 r15 r20 r28 A D D C r44 C r68 D r15 E r43 r43 r44 r55 r68 r75

39 How to force optimizer to use index We know that all employees have emp_no > 0. Then select * from employee where emp_no > 0 PLAN (EMPLOYEE INDEX (RDB$PRIMARY7)) But, All index pages will be scanned to get row numbers of all keys data pages will be scanned too (to read records) Result bigger page I/O Sometimes this trick allows to change PLAN (and query speed)

40 Index bitmap merge select * from employee where emp_no > 5 and last_name > 'b' PLAN (EMPLOYEE INDEX (RDB$PRIMARY7, NAMEX)) employee rdb$primary7 namex emp_no > 5 last_name > b AND, OR

41 select * from employee where emp_no > 5 and last_name > 'b' PLAN (EMPLOYEE INDEX (RDB$PRIMARY7, NAMEX)) You will not get that plan in Firebird 3 in employee.fdb Because optimizer eliminates indices on small tables Real plan: PLAN (EMPLOYEE INDEX (RDB$PRIMARY7))

42 PLAN (TABLE ORDER INDEX) Table walk by index order select * from employee order by last_name PLAN (EMPLOYEE ORDER NAMEX) Stays on first key (or key by where condition) Read record apply filter, if any Goto next key Read record

43 Index -> Table Root Page Pointer Page Keys (Leaf page) Data Pages A C A r55 B r28 r15 r20 r28 A D D C r44 C r68 D r15 E r43 r43 r44 r55 r68 r75

44 Clustering Factor Data Page 12 Index Key 1 Data Page 25 Index Key 2 Data Page 12 Data Page 28 Index Key 3 Data Page 13 Data Page 44 Index Key 4 Data Page 14 Data Page 57 Index Key 5 Bad Clustering Factor guid primary key Good Clustering Factor int/bigint primary key

45 Summary for table ORDER index Returns first row very quickly Jumping by data pages Causing pages dropping from cache, if cache size can t fit all data pages read Index Clustering factor Order of keys corresponding to records Firebird 3 between pages and rows (less is better) Example

46 Index Order Example select count(*) from table (14mln records) Execute time = 42s 500ms Buffers = 2048 Reads = 118 792 Fetches = 28 814 893 select a, count(a) from table group by a PLAN (TABLE ORDER A) Execute time = 45m 55s 469ms Reads = 3 733 434 each page was read from disk to cache 31 times Fetches = 42 869 143 Can be used to check disk performance pages * page_size / sec = 43mb/sec

47 select a from table order by a PLAN (TABLE ORDER A) Execute time = 63ms Buffers = 2 048 Reads = 48 Fetches = 12 495 if user will press Ctrl/End, it will take 3 mln reads and 45 minutes to get to the last row

48 table ORDER index notes Affects ORDER BY and GROUP BY Difference is only between number of rows returned to the client! Group by may use another access method instead SORT, so do not use GROUP BY for ordering Lot of rows causes huge page I/O Quickly return first rows, takes long time to get to the last row Only one index can be used order of fields, number of fields and order direction must correspond to index

49 PLAN SORT select * from employee order by first_name PLAN SORT ((EMPLOYEE NATURAL)) Database Moving rows Memory + temporary file Sorting data Returning data to client

50 Sort tuning firebird.conf TempBlockSize = 1048576 May increase to 2 or 3mln bytes, but not to 16mb TempCacheLimit = 67108864 SuperServer and SuperClassic. Classic = 8mb. TempDirectories = c:\temp;d:\temp Classic RAM Disk, point TempDirectories to RAM disk first, to hdd next SC, SS tune Temp* parameters

51 ORDER vs SORT PLAN (TABLE ORDER A) Execute time = 45m 55s 469ms Buffers = 2 048 Reads = 3 733 434 Fetches = 42 869 143 PLAN SORT ((A NATURAL)) Execute time = 2m 5s 485ms Buffers = 2 048 Reads = 118 757 Fetches 28 813 410 Reads equal to the table size (select count(*)) Takes 2 minutes, then ready to return the whole result without delay Temp file is deleted when last row fetched N of temp files = N of queries with plan sort Need to monitor number of temp files and their size

52 Average record length: 118.86, total records: 14 287 964 Data pages: 120 408, average fill: 99% Primary pages: 120 408, secondary pages: 0, swept pages: 0 Index BY_CZ (5) Clustering factor: 2 196 857, ratio: 0.15 Index MINS_CLIENT (0) Clustering factor: 3 651 564, ratio: 0.26 Index MINS_DATE (3) Clustering factor: 7 755 573, ratio: 0.54 Index MINS_NUMA (1) Clustering factor: 8 242 351, ratio: 0.58

53 EXPLAIN PLAN

54 ISQL set planonly; Old and new plan output Old plan example: PLAN SORT (RDB$RELATIONS INDEX (RDB$INDEX_0)) set explain;

55 Old and new plan output SELECT * FROM RDB$RELATIONS WHERE RDB$RELATION_NAME > :a ORDER BY RDB$SYSTEM_FLAG PLAN SORT (RDB$RELATIONS INDEX (RDB$INDEX_0)) Select Expression -> Sort (record length: 484, key length: 8) -> Filter -> Table "RDB$RELATIONS" Access By ID -> Bitmap -> Index "RDB$INDEX_0" Range Scan (lower bound: 1/1)

56 Old and new plan output SELECT * FROM RDB$RELATIONS WHERE RDB$RELATION_NAME > :a ORDER BY RDB$SYSTEM_FLAG PLAN SORT (RDB$RELATIONS INDEX (RDB$INDEX_0)) Select Expression -> Sort (record length: 484, key length: 8) -> Filter -> Table "RDB$RELATIONS" Access By ID -> Bitmap -> Index "RDB$INDEX_0" Range Scan (lower bound: 1/1)

57 Old and new plan output SELECT * FROM RDB$RELATIONS WHERE RDB$RELATION_NAME > :a ORDER BY RDB$SYSTEM_FLAG PLAN SORT (RDB$RELATIONS INDEX (RDB$INDEX_0)) Select Expression -> Sort (record length: 484, key length: 8) -> Filter -> Table "RDB$RELATIONS" Access By ID -> Bitmap -> Index "RDB$INDEX_0" Range Scan (lower bound: 1/1)

58 Index Scan Lower bound Upper bound Full scan Unique scan

59 Composite indices CREATE INDEX BY_AB ON MYTABLE (A, B) SELECT * FROM MYTABLE WHERE A = 1 AND B > 5 PLAN (MYTABLE INDEX (BY_AB)) A B 1 1 1 2 1 3 2 1 2 2 2 3 3 1

60 Second column sorted by groups, depending on first column values where A > 1 and B > 5 will not use 2 nd column where A = 1 and B will use first and second column where A = 1 and B = 5 and C

61 Index "RDB$INDEX_0" Range Scan (lower bound: 1/1) For composite indices > 1. First how many segments were used Second how many segments index have 1/3 only one segment is used 2/3 first 2 segments are used 3/3 all segments are used 1/3 very ineffective, 2/3 medium effective Consider using single-column indices instead

62 Procedure plan Now natural, instead of all plans for all queries

63 Cost estimation Cardinality number of records in the table. Computed by scanning pointer pages Selectivity 1/(Keys Total Dup) The less is better. Number of unique key values = keys total_dup

64 Cost estimation SELECT * FROM T1 JOIN T2 ON T1.PK = T2.FK WHERE T1.VAL < 100 ORDER BY T1.RANK PLAN SORT ( JOIN ( T1 NATURAL, T2 INDEX (FK) )) Table T1: base cardinality = 1000 Table T2: base cardinality = 5000 Index FK: selectivity = 0.001 Full Scan cost = 1000 cardinality = 1000 Final Row Set cost = 5000 cardinality = 2500 Sort cost = 5000 cardinality = 2500 Loop Join cost = 4500 cardinality = 2500 Filter cost = 1000 cardinality = 500 Index Scan cost = 7 cardinality = 5

65 EXPLAINED PLAN EXAMPLES

66 select * from rdb$relations where rdb$relation_name > :a PLAN (RDB$RELATIONS INDEX (RDB$INDEX_0)) Select Expression -> Filter -> Table "RDB$RELATIONS" Access By ID -> Bitmap -> Index "RDB$INDEX_0" Range Scan (lower bound: 1/1)

67 select * from a where name > 'b' and a.id > 5 PLAN (A INDEX (ANAME, PK_A)) Select Expression -> Filter -> Table "A" Access By ID -> Bitmap And -> Bitmap -> Index "ANAME" Range Scan (lower bound: 1/1) -> Bitmap -> Index "PK_A" Range Scan (lower bound: 1/1)

68 select * from minutes where code = '5' and zone > 5 PLAN (MINUTES INDEX (BY_CZ)) Select Expression -> Filter -> Table "MINUTES" Access By ID -> Bitmap -> Index "BY_CZ" Range Scan (lower bound: 2/2, upper bound: 1/2)

69 select * from rdb$relations where rdb$relation_name > :a order by rdb$relation_name PLAN (RDB$RELATIONS ORDER RDB$INDEX_0) Select Expression -> Filter -> Table "RDB$RELATIONS" Access By ID -> Index "RDB$INDEX_0" Range Scan (lower bound: 1/1)! No Bitmap index walk

70 select * from rdb$relations where rdb$relation_name > :a order by rdb$relation_name PLAN SORT (RDB$RELATIONS INDEX (RDB$INDEX_0)) Select Expression -> Sort (record length: 582, key length: 100) -> Filter -> Table "RDB$RELATIONS" Access By ID -> Bitmap -> Index "RDB$INDEX_0" Range Scan (lower bound: 1/1)

71 select e.last_name, p.proj_id from employee e, employee_project p where e.emp_no = p.emp_no PLAN JOIN (P NATURAL, E INDEX (RDB$PRIMARY7)) Select Expression -> Nested Loop Join (inner) -> Table "EMPLOYEE_PROJECT" as "P" Full Scan -> Filter -> Table "EMPLOYEE" as "E" Access By ID -> Bitmap -> Index "RDB$PRIMARY7" Unique Scan

72 select e.last_name, p.proj_id from employee e left join employee_project p on e.emp_no = p.emp_no where p.emp_no is null PLAN JOIN (E NATURAL, P INDEX (RDB$FOREIGN15)) Select Expression -> Filter -> Nested Loop Join (outer) -> Table "EMPLOYEE" as "E" Full Scan -> Filter -> Table "EMPLOYEE_PROJECT" as "P" Access By ID -> Bitmap -> Index "RDB$FOREIGN15" Range Scan (full match)

73 select e.* from employee e, employee_project p where e.emp_no+0 = p.emp_no+0 PLAN HASH (E NATURAL, P NATURAL) Select Expression -> Filter -> Hash Join (inner) -> Table "EMPLOYEE" as "E" Full Scan -> Record Buffer (record length: 25) -> Table "EMPLOYEE_PROJECT" as "P" Full Scan

74 select * from employee where (emp_no = :param) or (:param is null) where (emp_no = :param) or (:param = 0) Old plan PLAN (EMPLOYEE NATURAL) New plan PLAN (EMPLOYEE NATURAL, EMPLOYEE INDEX (RDB$PRIMARY7))

75 Plan change at runtime Select Expression -> Filter -> Condition -> Table "EMPLOYEE" Full Scan -> Table "EMPLOYEE" Access By ID -> Bitmap -> Index "RDB$PRIMARY7" Unique Scan

76 select * from employee where last_name = 'b' order by first_name PLAN (EMPLOYEE ORDER NAMEX) Select Expression -> Filter -> Table "EMPLOYEE" Access By ID -> Index "NAMEX" Range Scan (partial match: 1/2)

77 select * from employee where emp_no in (1, 2, 3) PLAN (EMPLOYEE INDEX (RDB$PRIMARY7, RDB$PRIMARY7, RDB$PRIMARY7)) Select Expression -> Filter -> Table "EMPLOYEE" Access By ID -> Bitmap Or -> Bitmap Or -> Bitmap -> Index "RDB$PRIMARY7" Unique Scan -> Bitmap -> Index "RDB$PRIMARY7" Unique Scan -> Bitmap -> Index "RDB$PRIMARY7" Unique Scan

78 Field in (1,2,3) Uses index 3 times one bitmap, 3 scans Field+0 in (1,2,3) Turns index usage off, completely Field+0 in (1, 2,3) and (field between 1 and 3) Turns index on back, range scan once, to avoid natural scan

79 ANOTHER FIREBIRD 3 AND 4 OPTIMIZER FEATURES

80 Stream materialization (caching) allows to avoid re-reading the same data from tables (for non-correlated streams) currently used only for hash joins, to be used for subqueries too Hash join join algorithm for non-indexed correlation usually performs better than merge join can be used instead of nested loops to avoid repeating reads of the same rows (in the future)

81 FULL JOIN improvements reimplemented as «semi-join union all antijoin» can use available indices now Conditional streams allow to choose between possible plans at runtime currently used only for(field = :PARAM OR :PARAM IS NULL)

82 Improved ORDER plan implementation avoid bad plans like «A ORDER I INDEX(I)», use simple «A ORDER I» with a range scan instead allow ORDER plan for «WHERE A = 0 ORDER BY B» if compound index {A, B} exists

83 Implicit FIRST ROWS / ALL ROWS hints whether you need to fetch the first rows faster (e.g. interactive grids) or the complete result set choose ORDER or SORT currently used internally for queries with FIRST, EXISTS, ANY prefers ORDER plan, affects join order explicit FIRST/ALL ROWS hints for other query types will appear in v4

84 Faster prepare for big tables sampling PP instead of reading them all being field tested Misc improvements improve some cases of ORDER plan usage in complex queries better plans for LEFT JOIN and UNION used together optimize SORT plan for FIRST ROWS strategy

85 Planned for v 4 HASH/MERGE for outer joins Execute EXISTS/IN as semi-join LATERAL joins More optimizer statistics and its background update

86 Thank you! Contacts: www.ib-aid.com support@ib-aid.com