Documente Academic
Documente Profesional
Documente Cultură
Copyright
Copyright2015,
2014,Oracle
Oracleand/or
and/oritsitsaffiliates.
affiliates.All
Allrights
rightsreserved.
reserved.|
MySQL Optimizer
MySQL Server
Optimizer
Parser
SELECT a, b
FROM t1, t2, t3
WHERE t1.a = t2.b
AND t2.b = t3.c
AND t2.d > 20
AND t2.d < 30;
JOIN
Cost based
optimizations
Table/index info
(data dictionary)
Cost Model
Heuristics
Statistics
(storage engines)
Copyright 2014, Oracle and/or its affiliates. All rights reserved. |
Table
scan
JOIN
Range
scan
Ref
access
t2
t3
t1
Generated Columns
Generated Columns
Functional Index
CREATE TABLE order_lines
(orderno integer,
lineno integer,
price decimal(10,2),
qty integer,
sum_price decimal(10,2) GENERATED ALWAYS AS (qty * price) VIRTUAL);
ALTER TABLE order_lines ADD INDEX idx (sum_price);
VIRTUAL
VIRTUAL
10
JSON support
JSON Support
Seemless integration of relational and schema-less data
Leverage existing database infrastructure for new applications
Provide a native JSON datatype
You can always store JSON data in TEXT or VARCHAR column, but
native JSON datatype has many benefits:
Validation on insert
Efficient access
Copyright 2014, Oracle and/or its affiliates. All rights reserved. |
Modify
JSON_REMOVE()
JSON_ARRAY_APPEND()
JSON_SET()
JSON_INSERT()
JSON_ARRAY_INSERT()
JSON_REPLACE()
Create
JSON_MERGE()
JSON_ARRAY()
JSON_OBJECT()
Get data
JSON_EXTRACT()
JSON_SEARCH()
Helper
JSON_QUOTE()
JSON_UNQUOTE()
14
16
17
{
"type":"Feature",
"geometry":{
"type":"Polygon",
"coordinates":[
[
[-122.42200352825247,37.80848009696725,0],
[-122.42207601332528,37.808835019815085,0],
[-122.42110217434865,37.808803534992904,0],
[-122.42106256906727,37.80860105681814,0],
[-122.42200352825247,37.80848009696725,0]
]
]
},
"properties":{
"TO_ST":"0",
"BLKLOT":"0001001",
"STREET":"UNKNOWN",
"FROM_ST":"0",
"LOT_NUM":"001",
"ST_TYPE":null,
"ODD_EVEN":"E",
"BLOCK_NUM":"0001",
"MAPBLKLOT":"0001001"
}
}
Copyright 2014, Oracle and/or its affiliates. All rights reserved. |
18
# as TEXT type
SELECT DISTINCT
feature->"$.type" as json_extract
FROM features;
+------------------+
| json_extract |
+------------------+
| "Feature" |
+------------------+
1 row in set (12.85 sec)
19
Create Index
From table scan on 206K documents to index scan on 206K materialized values
20
SELECT feature->"$.properties.STREET"
AS property_street FROM features
WHERE id = 121254;
+--------------------------------+
| extract_path
|
+--------------------------------+
| "$.properties.STREET" |
+--------------------------------+
1 row in set (0.00 sec)
+-----------------------+
| property_street |
+-----------------------+
| "MARKET"
|
+-----------------------+
1 row in set (0.00 sec)
21
22
Cost Model
Tunable/configurable
Prefix_rows_t1
Number of records read
from t1
Access
Method
t1
t2
Total cost = cost (access method t1) + Prefix_rows_t1 * cost (access method t2)
Prefix_rows_t1 is records read by t1
Overestimation if where conditions apply!->Suboptimial join order
Copyright 2014, Oracle and/or its affiliates. All rights reserved. |
Condition filter
Number of
records read
from t1
Prefix_rows_t1
Records passing the table
conditions on t1
Access
Method
Condition filter
t2
Explain for 5.6: Total Cost = cost(scan office) + 100 * cost(ref_access emp)
Table
Type
Ref
Rows
Filtered
Extra
office
ALL
PRIMARY
NULL
NULL
100
100.00
NULL
employee
ref
office
office
office.id
99
100.00
Using where
Type
Possible keys
Key
Ref
Rows
Filtered
Extra
employee
ALL
NULL
NULL
NULL
9991
1.23
NULL
office
eq_ref
PRIMARY
PRIMARY
employee.office 1
100.00
Using where
JOIN ORDER
HAS CHANGED!
HINT
Improved HINTs
Introduced new hint syntax /*+ ...*/
Flexibility over optimizer switch, effect individual statement only
Hints within one statement take preceduence over optimizer switch
Hints apply at different scope levels: global, query block, table, index
Table
Type
Possible_keys Key
Key_len Ref
Rows
Extra
simple
t3
index
null
simple
t2
ref
test.t3.a 1
Using index
EXPLAIN SELECT * FROM t2 WHERE t2.a IN (SELECT /*+ NO_SEMIJOIN() */ a FROM t3);
Semi-join disabled with hint, subquery is executed for each row of outer table
id Select_type
Table
Type
Possible_keys Key
Key_len
Ref
Rows
Extra
primary
t2
index
null
null
dependent
subquery
t3
Index_
subquery
func
Using index
Table
Type
Possible_ke
ys
Key
Key_len Ref
Rows
Extra
simple
t2
index
null
simple
<subquery2>
eq_ref
<auto_key>
<auto_key>
test.t2.a 1
null
null
Using index
23 rows
materialized
t3
index
in set, 1 warning
(0.01 sec)
Replacement is:
SELECT *
FROM t1 JOIN t2 ON t1.keycol =
t2.keycol
WHERE col1 = ? AND col2 = ?
SELECT a, b, c
FROM t1 STRAIGHT_JOIN t2 FORCE
INDEX (col1)
ON t1.keycol = t2.keycol
WHERE col1 = ? AND col2 = ?
For query
SELECT * FROM t1 JOIN t2 ON t1.keycol = t2.keycol WHERE col1 = 42 AND col2 = 2
Replace parameter markers in Replacement with actual literals:
SELECT a, b, c
FROM t1 STRAIGHT_JOIN t2 FORCE INDEX (col1)
ON t1.keycol = t2.keycol WHERE col1 = 42 AND col2 = 2
Copyright 2014, Oracle and/or its affiliates. All rights reserved. |
Pattern_
database
Replacement
Enabl
ed
Message
NULL
Parse error in
pattern:near
at line 1
User Requested
Performance Improvements
5.7: IN queries with row value expressions executed using range scans.
5.7: Explain output: Index/table scans change to range scans
MySQL 5.7:
select_type: SIMPLE
select_type: SIMPLE
table: t1
table: t1
type: index
type: range
key: x
key: x
key_len: 10
key_len: 10
ref: NULL
ref: NULL
rows: 10 000
rows: 2
MySQL 5.6:
Derived table (subquery in FROM
clause) always materialized in
temporary table
MySQL 5.7:
Merged into outer query or
materialized
Derived table optimized as part of
outer query:
Faster queries
EXPLAIN