SlideShare uma empresa Scribd logo
1 de 86
Baixar para ler offline
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow1
Firebird 3.0 statistics and plans
Dmitry Kuzmenko, IBSurgeon
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow2
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow3
• 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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow4
• Replication, Recovery and
Optimization for Firebird
and InterBase since 2002
• Platinum Sponsor of
Firebird Foundation
• Based in Moscow, Russia
www.ib-aid.com
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow5
Agenda
• New elements for table statistics
– Including blob information
• New elements for index statistics
• Plan elements
• Explained plans
• Optimizer enhancements
– Firebird 3 and 4
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow6
NEW STATISTICS ELEMENTS
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow7
How to get statistics
• Gstat –r
• Gstat –r –t tablename1 –t
tablename2…
• Services API
• HQbird Database Analyst
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow8
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow9
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow10
Primary, Secondary – new storage concept
• Primary
– Primary record versions - insert
– Backversions
• Secondary
– Backversions, record fragments -
update/delete
– Small blobs (level 0)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow11
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow12
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow13
Packing
• Average unpacked length: 96.00,
compression ratio: 1.46
• Average record length: 65.58
• 96 / 65.58 = 1.46
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow14
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.
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow15
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow16
BLOB Levels
• Level 1 –
pointers to the
blob pages BLOB
BLOB page
with data
BLOB page
with data
BLOB record
• Level 0 – at data page, as record data
• Level 2 – pointers to
pointers (BLOB pages
with pointers)
BLOB Record
BLOB
pointer page
BLOB
pointer page
BLOB data
page
BLOB data
page
BLOB data
page
BLOB data
page
Data page
BLOB record
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow17
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow18
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow19
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow20
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow21
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow22
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow23
Reading speed, ms
0,000
0,100
0,200
0,300
0,400
0,500
0,600
0,700
0,800
A B C D
FB 2.5
FB 3.0
Performance with B and C may decrease up to 22%
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow24
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow25
Page reads
0
2000
4000
6000
8000
10000
12000
14000
16000
A B C D
FB 2.5
FB 3.0
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow26
957
8658
14286
1555
960
8904
15352
1560
960 1072 1066 1069
0
7832
14286
491
A B C D
FB 2.5 FB 3.0 PP SP
Number of pages
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow27
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow28
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow29
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow30
Clustering Factor
Index Key 1
Index Key 2
Index Key 3
Index Key 5
Index Key 4
Data Page 12
Data Page 25
Data Page 28
Data Page 57
Data Page 44
Data Page 12
Data Page 13
Data Page 14
Bad Clustering Factor = 5
guid primary key
Good Clustering Factor = 3
int/bigint primary key
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow31
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow32
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow33
Clustering factor
• Clustering factor closer to the primary
data pages number – good
• Ratio – less is better
• How clustering factor affects
performance?
See below
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow34
PLAN ELEMENTS
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow35
Plan elements
• tablename NATURAL
• tablename INDEX indexname
• Tablename ORDER indexname
• JOIN
• HASH JOIN
• SORT
• SORT MERGE
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow36
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow37
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))
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow38
r15
r20
r28
r43
r44
r55
r68
r75
Index -> Table
A
D
A
C
D
A r55
B r28
C r44
C r68
D r15
E r43
Root Page Pointer Page Keys (Leaf page) Data Pages
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow39
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow40
Index bitmap merge
• select * from employee
where emp_no > 5 and last_name > 'b'
PLAN (EMPLOYEE INDEX (RDB$PRIMARY7, NAMEX))
rdb$primary7 namexemployee
emp_no > 5
last_name > b
AND, OR
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow41
• 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))
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow42
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow43
r15
r20
r28
r43
r44
r55
r68
r75
Index -> Table
A
D
A
C
D
A r55
B r28
C r44
C r68
D r15
E r43
Root Page Pointer Page Keys (Leaf page) Data Pages
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow44
Clustering Factor
Index Key 1
Index Key 2
Index Key 3
Index Key 5
Index Key 4
Data Page 12
Data Page 25
Data Page 28
Data Page 57
Data Page 44
Data Page 12
Data Page 13
Data Page 14
Bad Clustering Factor
guid primary key
Good Clustering Factor
int/bigint primary key
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow45
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow46
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow47
• 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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow48
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow49
PLAN SORT
• select * from employee
• order by first_name
PLAN SORT ((EMPLOYEE NATURAL))
Database
Memory +
temporary file
Sorting data
Returning
data to
client
Moving rows
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow50
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow51
ORDER vs SORT
• 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
PLAN SORT ((A NATURAL))
Execute time = 2m 5s 485ms
Buffers = 2 048
Reads = 118 757
Fetches 28 813 410
PLAN (TABLE ORDER A)
Execute time = 45m 55s 469ms
Buffers = 2 048
Reads = 3 733 434
Fetches = 42 869 143
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow52
• 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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow53
EXPLAIN PLAN
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow54
Old and new plan output
• ISQL
• set planonly;
• Old plan example:
PLAN SORT (RDB$RELATIONS INDEX (RDB$INDEX_0))
• set explain;
• …
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow55
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow56
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow57
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow58
Index Scan
• Lower bound
• Upper bound
• Full scan
• Unique scan
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow59
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow60
• Second column sorted by groups, depending
on first column values
• where A > 1 and B > 5 will not use 2nd column
• where A = 1 and B … will use first and second column
• where A = 1 and B = 5 and C …
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow61
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow62
Procedure plan
• Now – natural, instead of all plans for all
queries
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow63
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow64
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)
))
Final Row Set
cost = 5000
cardinality = 2500
Sort
cost = 5000
cardinality = 2500
Full Scan
cost = 1000
cardinality = 1000
Filter
cost = 1000
cardinality = 500
Index Scan
cost = 7
cardinality = 5
Loop Join
cost = 4500
cardinality = 2500
Table T1: base cardinality = 1000
Table T2: base cardinality = 5000
Index FK: selectivity = 0.001
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow65
EXPLAINED PLAN EXAMPLES
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow66
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow67
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow68
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow69
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow70
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow71
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow72
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow73
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow74
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))
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow75
Plan change at runtime
Select Expression
-> Filter
-> Condition
-> Table "EMPLOYEE" Full Scan
-> Table "EMPLOYEE" Access By ID
-> Bitmap
-> Index "RDB$PRIMARY7" Unique Scan
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow76
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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow77
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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow78
• 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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow79
ANOTHER FIREBIRD 3 AND 4
OPTIMIZER FEATURES
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow80
• 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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow81
• FULL JOIN improvements
– reimplemented as «semi-join union all anti-
join»
– 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)
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow82
• 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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow83
• 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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow84
• 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
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow85
Planned for v 4
• HASH/MERGE for outer joins
• Execute EXISTS/IN as semi-join
• LATERAL joins
• More optimizer statistics and its
background update
© IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow86
Thank you!
• Contacts:
www.ib-aid.com
support@ib-aid.com

Mais conteúdo relacionado

Semelhante a Firebird 3.0 statistics and plans

MongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB
 
Big Data Ecosystem at LinkedIn. Keynote talk at Big Data Innovators Gathering...
Big Data Ecosystem at LinkedIn. Keynote talk at Big Data Innovators Gathering...Big Data Ecosystem at LinkedIn. Keynote talk at Big Data Innovators Gathering...
Big Data Ecosystem at LinkedIn. Keynote talk at Big Data Innovators Gathering...Mitul Tiwari
 
MongoDB Distilled
MongoDB DistilledMongoDB Distilled
MongoDB Distilledb0ris_1
 
Object Compaction in Cloud for High Yield
Object Compaction in Cloud for High YieldObject Compaction in Cloud for High Yield
Object Compaction in Cloud for High YieldScyllaDB
 
[2D3]TurboGraph- Ultrafast graph analystics engine for billion-scale graphs i...
[2D3]TurboGraph- Ultrafast graph analystics engine for billion-scale graphs i...[2D3]TurboGraph- Ultrafast graph analystics engine for billion-scale graphs i...
[2D3]TurboGraph- Ultrafast graph analystics engine for billion-scale graphs i...NAVER D2
 
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...confluent
 
A Day in the Life of a Druid Implementor and Druid's Roadmap
A Day in the Life of a Druid Implementor and Druid's RoadmapA Day in the Life of a Druid Implementor and Druid's Roadmap
A Day in the Life of a Druid Implementor and Druid's RoadmapItai Yaffe
 
MongoDB for Time Series Data: Sharding
MongoDB for Time Series Data: ShardingMongoDB for Time Series Data: Sharding
MongoDB for Time Series Data: ShardingMongoDB
 
The state of Hive and Spark in the Cloud (July 2017)
The state of Hive and Spark in the Cloud (July 2017)The state of Hive and Spark in the Cloud (July 2017)
The state of Hive and Spark in the Cloud (July 2017)Nicolas Poggi
 
1404 app dev series - session 8 - monitoring & performance tuning
1404   app dev series - session 8 - monitoring & performance tuning1404   app dev series - session 8 - monitoring & performance tuning
1404 app dev series - session 8 - monitoring & performance tuningMongoDB
 
Understanding and Measuring I/O Performance
Understanding and Measuring I/O PerformanceUnderstanding and Measuring I/O Performance
Understanding and Measuring I/O PerformanceGlenn K. Lockwood
 
The state of Spark in the cloud
The state of Spark in the cloudThe state of Spark in the cloud
The state of Spark in the cloudNicolas Poggi
 
Conceptos básicos. Seminario web 1: Introducción a NoSQL
Conceptos básicos. Seminario web 1: Introducción a NoSQLConceptos básicos. Seminario web 1: Introducción a NoSQL
Conceptos básicos. Seminario web 1: Introducción a NoSQLMongoDB
 
Spark to DocumentDB connector
Spark to DocumentDB connectorSpark to DocumentDB connector
Spark to DocumentDB connectorDenny Lee
 
Frontera: open source, large scale web crawling framework
Frontera: open source, large scale web crawling frameworkFrontera: open source, large scale web crawling framework
Frontera: open source, large scale web crawling frameworkScrapinghub
 
Paradigmas de procesamiento en Big Data: estado actual, tendencias y oportu...
Paradigmas de procesamiento en  Big Data: estado actual,  tendencias y oportu...Paradigmas de procesamiento en  Big Data: estado actual,  tendencias y oportu...
Paradigmas de procesamiento en Big Data: estado actual, tendencias y oportu...Facultad de Informática UCM
 
11. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:211. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:2Fabio Fumarola
 
Accelerating hbase with nvme and bucket cache
Accelerating hbase with nvme and bucket cacheAccelerating hbase with nvme and bucket cache
Accelerating hbase with nvme and bucket cacheDavid Grier
 

Semelhante a Firebird 3.0 statistics and plans (20)

MongoDB Aggregation Performance
MongoDB Aggregation PerformanceMongoDB Aggregation Performance
MongoDB Aggregation Performance
 
Big Data Ecosystem at LinkedIn. Keynote talk at Big Data Innovators Gathering...
Big Data Ecosystem at LinkedIn. Keynote talk at Big Data Innovators Gathering...Big Data Ecosystem at LinkedIn. Keynote talk at Big Data Innovators Gathering...
Big Data Ecosystem at LinkedIn. Keynote talk at Big Data Innovators Gathering...
 
MongoDB Distilled
MongoDB DistilledMongoDB Distilled
MongoDB Distilled
 
Object Compaction in Cloud for High Yield
Object Compaction in Cloud for High YieldObject Compaction in Cloud for High Yield
Object Compaction in Cloud for High Yield
 
[2D3]TurboGraph- Ultrafast graph analystics engine for billion-scale graphs i...
[2D3]TurboGraph- Ultrafast graph analystics engine for billion-scale graphs i...[2D3]TurboGraph- Ultrafast graph analystics engine for billion-scale graphs i...
[2D3]TurboGraph- Ultrafast graph analystics engine for billion-scale graphs i...
 
14 query processing-sorting
14 query processing-sorting14 query processing-sorting
14 query processing-sorting
 
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
 
A Day in the Life of a Druid Implementor and Druid's Roadmap
A Day in the Life of a Druid Implementor and Druid's RoadmapA Day in the Life of a Druid Implementor and Druid's Roadmap
A Day in the Life of a Druid Implementor and Druid's Roadmap
 
Say Hello to MyRocks
Say Hello to MyRocksSay Hello to MyRocks
Say Hello to MyRocks
 
MongoDB for Time Series Data: Sharding
MongoDB for Time Series Data: ShardingMongoDB for Time Series Data: Sharding
MongoDB for Time Series Data: Sharding
 
The state of Hive and Spark in the Cloud (July 2017)
The state of Hive and Spark in the Cloud (July 2017)The state of Hive and Spark in the Cloud (July 2017)
The state of Hive and Spark in the Cloud (July 2017)
 
1404 app dev series - session 8 - monitoring & performance tuning
1404   app dev series - session 8 - monitoring & performance tuning1404   app dev series - session 8 - monitoring & performance tuning
1404 app dev series - session 8 - monitoring & performance tuning
 
Understanding and Measuring I/O Performance
Understanding and Measuring I/O PerformanceUnderstanding and Measuring I/O Performance
Understanding and Measuring I/O Performance
 
The state of Spark in the cloud
The state of Spark in the cloudThe state of Spark in the cloud
The state of Spark in the cloud
 
Conceptos básicos. Seminario web 1: Introducción a NoSQL
Conceptos básicos. Seminario web 1: Introducción a NoSQLConceptos básicos. Seminario web 1: Introducción a NoSQL
Conceptos básicos. Seminario web 1: Introducción a NoSQL
 
Spark to DocumentDB connector
Spark to DocumentDB connectorSpark to DocumentDB connector
Spark to DocumentDB connector
 
Frontera: open source, large scale web crawling framework
Frontera: open source, large scale web crawling frameworkFrontera: open source, large scale web crawling framework
Frontera: open source, large scale web crawling framework
 
Paradigmas de procesamiento en Big Data: estado actual, tendencias y oportu...
Paradigmas de procesamiento en  Big Data: estado actual,  tendencias y oportu...Paradigmas de procesamiento en  Big Data: estado actual,  tendencias y oportu...
Paradigmas de procesamiento en Big Data: estado actual, tendencias y oportu...
 
11. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:211. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:2
 
Accelerating hbase with nvme and bucket cache
Accelerating hbase with nvme and bucket cacheAccelerating hbase with nvme and bucket cache
Accelerating hbase with nvme and bucket cache
 

Mais de Marius Adrian Popa

Python with Firebird: FDB driver 101
Python with Firebird: FDB driver 101Python with Firebird: FDB driver 101
Python with Firebird: FDB driver 101Marius Adrian Popa
 
The State of the Romanian Game Developers Industry Rgda 2019 report
The State of the Romanian Game Developers Industry Rgda 2019 reportThe State of the Romanian Game Developers Industry Rgda 2019 report
The State of the Romanian Game Developers Industry Rgda 2019 reportMarius Adrian Popa
 
Understanding the firebird optimizer
Understanding the firebird optimizerUnderstanding the firebird optimizer
Understanding the firebird optimizerMarius Adrian Popa
 
Tuning Linux Windows and Firebird for Heavy Workload
Tuning Linux Windows and Firebird for Heavy WorkloadTuning Linux Windows and Firebird for Heavy Workload
Tuning Linux Windows and Firebird for Heavy WorkloadMarius Adrian Popa
 
Firebird Performance 2.5/3.0.x/4.0
 Firebird Performance 2.5/3.0.x/4.0 Firebird Performance 2.5/3.0.x/4.0
Firebird Performance 2.5/3.0.x/4.0Marius Adrian Popa
 

Mais de Marius Adrian Popa (11)

Python with Firebird: FDB driver 101
Python with Firebird: FDB driver 101Python with Firebird: FDB driver 101
Python with Firebird: FDB driver 101
 
The State of the Romanian Game Developers Industry Rgda 2019 report
The State of the Romanian Game Developers Industry Rgda 2019 reportThe State of the Romanian Game Developers Industry Rgda 2019 report
The State of the Romanian Game Developers Industry Rgda 2019 report
 
Understanding the firebird optimizer
Understanding the firebird optimizerUnderstanding the firebird optimizer
Understanding the firebird optimizer
 
Firebird Advanced Trace API
Firebird Advanced Trace API Firebird Advanced Trace API
Firebird Advanced Trace API
 
Tuning Linux Windows and Firebird for Heavy Workload
Tuning Linux Windows and Firebird for Heavy WorkloadTuning Linux Windows and Firebird for Heavy Workload
Tuning Linux Windows and Firebird for Heavy Workload
 
Firebird Performance 2.5/3.0.x/4.0
 Firebird Performance 2.5/3.0.x/4.0 Firebird Performance 2.5/3.0.x/4.0
Firebird Performance 2.5/3.0.x/4.0
 
Django firebird project
Django firebird projectDjango firebird project
Django firebird project
 
Proiectul django firebird
Proiectul django firebirdProiectul django firebird
Proiectul django firebird
 
Firebird Myths in Romanian
Firebird Myths in RomanianFirebird Myths in Romanian
Firebird Myths in Romanian
 
2 Firebird Technical Ro
2 Firebird Technical Ro2 Firebird Technical Ro
2 Firebird Technical Ro
 
0 What Is Mind The Bird Ro
0 What Is Mind The Bird Ro0 What Is Mind The Bird Ro
0 What Is Mind The Bird Ro
 

Último

Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...amitlee9823
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...only4webmaster01
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...amitlee9823
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 

Último (20)

Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 

Firebird 3.0 statistics and plans

  • 1. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow1 Firebird 3.0 statistics and plans Dmitry Kuzmenko, IBSurgeon
  • 2. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow2 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow3 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow4 • Replication, Recovery and Optimization for Firebird and InterBase since 2002 • Platinum Sponsor of Firebird Foundation • Based in Moscow, Russia www.ib-aid.com
  • 5. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow5 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow6 NEW STATISTICS ELEMENTS
  • 7. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow7 How to get statistics • Gstat –r • Gstat –r –t tablename1 –t tablename2… • Services API • HQbird Database Analyst
  • 8. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow8 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow9 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow10 Primary, Secondary – new storage concept • Primary – Primary record versions - insert – Backversions • Secondary – Backversions, record fragments - update/delete – Small blobs (level 0)
  • 11. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow11 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow12 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow13 Packing • Average unpacked length: 96.00, compression ratio: 1.46 • Average record length: 65.58 • 96 / 65.58 = 1.46
  • 14. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow14 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow15 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow16 BLOB Levels • Level 1 – pointers to the blob pages BLOB BLOB page with data BLOB page with data BLOB record • Level 0 – at data page, as record data • Level 2 – pointers to pointers (BLOB pages with pointers) BLOB Record BLOB pointer page BLOB pointer page BLOB data page BLOB data page BLOB data page BLOB data page Data page BLOB record
  • 17. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow17 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow18
  • 19. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow19 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow20 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow21 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow22 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow23 Reading speed, ms 0,000 0,100 0,200 0,300 0,400 0,500 0,600 0,700 0,800 A B C D FB 2.5 FB 3.0 Performance with B and C may decrease up to 22%
  • 24. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow24 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow25 Page reads 0 2000 4000 6000 8000 10000 12000 14000 16000 A B C D FB 2.5 FB 3.0
  • 26. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow26 957 8658 14286 1555 960 8904 15352 1560 960 1072 1066 1069 0 7832 14286 491 A B C D FB 2.5 FB 3.0 PP SP Number of pages
  • 27. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow27 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow28 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow29 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow30 Clustering Factor Index Key 1 Index Key 2 Index Key 3 Index Key 5 Index Key 4 Data Page 12 Data Page 25 Data Page 28 Data Page 57 Data Page 44 Data Page 12 Data Page 13 Data Page 14 Bad Clustering Factor = 5 guid primary key Good Clustering Factor = 3 int/bigint primary key
  • 31. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow31 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow32 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow33 Clustering factor • Clustering factor closer to the primary data pages number – good • Ratio – less is better • How clustering factor affects performance? See below
  • 34. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow34 PLAN ELEMENTS
  • 35. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow35 Plan elements • tablename NATURAL • tablename INDEX indexname • Tablename ORDER indexname • JOIN • HASH JOIN • SORT • SORT MERGE
  • 36. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow36 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow37 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow38 r15 r20 r28 r43 r44 r55 r68 r75 Index -> Table A D A C D A r55 B r28 C r44 C r68 D r15 E r43 Root Page Pointer Page Keys (Leaf page) Data Pages
  • 39. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow39 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow40 Index bitmap merge • select * from employee where emp_no > 5 and last_name > 'b' PLAN (EMPLOYEE INDEX (RDB$PRIMARY7, NAMEX)) rdb$primary7 namexemployee emp_no > 5 last_name > b AND, OR
  • 41. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow41 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow42 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow43 r15 r20 r28 r43 r44 r55 r68 r75 Index -> Table A D A C D A r55 B r28 C r44 C r68 D r15 E r43 Root Page Pointer Page Keys (Leaf page) Data Pages
  • 44. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow44 Clustering Factor Index Key 1 Index Key 2 Index Key 3 Index Key 5 Index Key 4 Data Page 12 Data Page 25 Data Page 28 Data Page 57 Data Page 44 Data Page 12 Data Page 13 Data Page 14 Bad Clustering Factor guid primary key Good Clustering Factor int/bigint primary key
  • 45. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow45 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow46 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow47 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow48 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow49 PLAN SORT • select * from employee • order by first_name PLAN SORT ((EMPLOYEE NATURAL)) Database Memory + temporary file Sorting data Returning data to client Moving rows
  • 50. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow50 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow51 ORDER vs SORT • 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 PLAN SORT ((A NATURAL)) Execute time = 2m 5s 485ms Buffers = 2 048 Reads = 118 757 Fetches 28 813 410 PLAN (TABLE ORDER A) Execute time = 45m 55s 469ms Buffers = 2 048 Reads = 3 733 434 Fetches = 42 869 143
  • 52. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow52 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow53 EXPLAIN PLAN
  • 54. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow54 Old and new plan output • ISQL • set planonly; • Old plan example: PLAN SORT (RDB$RELATIONS INDEX (RDB$INDEX_0)) • set explain; • …
  • 55. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow55 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow56 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow57 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow58 Index Scan • Lower bound • Upper bound • Full scan • Unique scan
  • 59. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow59 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow60 • Second column sorted by groups, depending on first column values • where A > 1 and B > 5 will not use 2nd column • where A = 1 and B … will use first and second column • where A = 1 and B = 5 and C …
  • 61. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow61 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow62 Procedure plan • Now – natural, instead of all plans for all queries
  • 63. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow63 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow64 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) )) Final Row Set cost = 5000 cardinality = 2500 Sort cost = 5000 cardinality = 2500 Full Scan cost = 1000 cardinality = 1000 Filter cost = 1000 cardinality = 500 Index Scan cost = 7 cardinality = 5 Loop Join cost = 4500 cardinality = 2500 Table T1: base cardinality = 1000 Table T2: base cardinality = 5000 Index FK: selectivity = 0.001
  • 65. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow65 EXPLAINED PLAN EXAMPLES
  • 66. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow66 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow67 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow68 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow69 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow70 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow71 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow72 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow73 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow74 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow75 Plan change at runtime Select Expression -> Filter -> Condition -> Table "EMPLOYEE" Full Scan -> Table "EMPLOYEE" Access By ID -> Bitmap -> Index "RDB$PRIMARY7" Unique Scan
  • 76. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow76 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow77 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow78 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow79 ANOTHER FIREBIRD 3 AND 4 OPTIMIZER FEATURES
  • 80. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow80 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow81 • FULL JOIN improvements – reimplemented as «semi-join union all anti- join» – 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow82 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow83 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow84 • 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow85 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. © IBSurgeonFirebird Tour 2017 – Prague, Bad Sassendorf, Moscow86 Thank you! • Contacts: www.ib-aid.com support@ib-aid.com