SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
New index features in MySQL 8
Erik Frøseth
Software Developer
MySQL Optimizer Team
February 1st, 2019
2Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
3Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Program Agenda
Functional indexes
Index skip scan
Invisible indexes
1
2
3
4
5
6
7
4Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Program Agenda
Functional indexes
Index skip scan
Invisible indexes
1
2
3
4
5
6
7
5Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Terminology
# This creates a single index/key with two index/key parts
CREATE INDEX my_index ON my_table (column_a, column_b);
6Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Functional index – what is it?
● A functional index is an index where at least one of the index
parts is a function instead of a column.
1) CREATE INDEX my_index ON my_table ((ABS(column_a)));
2) CREATE INDEX my_index ON my_table
((ABS(column_a)), column_b);
3) CREATE TABLE my_table (
column_a VARCHAR(255),
INDEX my_index ((UPPER(column_a)))
);
7Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Functional index – how do you use it?
● A functional index is created, used and removed exactly like any other index.
mysql> CREATE TABLE my_table (
-> column_a VARCHAR(255),
-> INDEX my_index ((UPPER(column_a)))
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> EXPLAIN SELECT * FROM my_table WHERE UPPER(column_a) = "FOOBAR";
+-------------+----------+------+----------+---------+-------+------+----------+
| select_type | table | type | key | key_len | ref | rows | filtered |
+-------------+----------+------+----------+---------+-------+------+----------+
| SIMPLE | my_table | ref | my_index | 1023 | const | 1 | 100.00 |
+-------------+----------+------+----------+---------+-------+------+----------+
1 row in set, 1 warning (0.00 sec)
8Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Functional index – how do you use it?
mysql> CREATE TABLE my_table (
-> column_a VARCHAR(255),
-> column_b INT,
-> INDEX my_index ((UPPER(column_a)), column_b)
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> EXPLAIN SELECT * FROM my_table WHERE UPPER(column_a) = "FOOBAR" AND column_b < 20;
+-------------+----------+-------+---------------+----------+---------+------+----------+
| select_type | table | type | possible_keys | key | key_len | rows | filtered |
+-------------+----------+-------+---------------+----------+---------+------+----------+
| SIMPLE | my_table | range | my_index | my_index | 1028 | 1 | 100.00 |
+-------------+----------+-------+---------------+----------+---------+------+----------+
1 row in set, 1 warning (0.00 sec)
● You can mix functional and non-functional index parts
9Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Functional index – observability
mysql> SHOW INDEX FROM t1;
+-------+------------+----------+--------------+-------------+...+-------------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name |...| Expression |
+-------+------------+----------+--------------+-------------+...+-------------------+
| t1 | 1 | idx | 1 | NULL |...| (`col1` + `col1`) |
+-------+------------+----------+--------------+-------------+...+-------------------+
1 row in set (0.01 sec)
mysql> SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_NAME = "t1";
+...+------------+--------------+-------------+...+-------------------+
|...| INDEX_NAME | SEQ_IN_INDEX | COLUMN_NAME |...| EXPRESSION |
+...+------------+--------------+-------------+...+-------------------+
|...| idx | 1 | NULL |...| (`col1` + `col1`) |
+...+------------+--------------+-------------+...+-------------------+
1 row in set (0.00 sec)
10Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Functional index – observability
mysql> SHOW CREATE TABLE t1;
+-------+-----------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`col1` int(11) DEFAULT NULL,
KEY `idx` (((`col1` + `col1`)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+----------------------------------------------------------+
1 row in set (0.00 sec)
11Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Functional index – use cases
The main use case is indexing complex type such as
geometries and (especially) JSON.
CREATE INDEX
json_extract_idx
ON my_table
((CAST(data->>'$.person.id' AS CHAR(100))));
{
“person”:
{
“first_name”: “Erik”,
“last_name”: “Frøseth”,
“id”: 8
}
}
12Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Functional index – limitations and other notes
● The primary key cannot be a functional index
● You cannot index a function that returns a BLOB or TEXT
● You can not index non-deterministic functions (RAND(),
NOW(), UUID() etc...)
● Each functional index part “uses” one column
● It’s more costly to maintain a functional index than a regular
index. Do not overuse it!
13Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Program Agenda
Functional indexes
Index skip scan
Invisible indexes
1
2
3
4
5
6
7
14Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Index skip scan
● Based on a contribution from Facebook
● An index can only be used if the first/leading column(s) is
referenced in the WHERE-condition
CREATE INDEX my_index ON my_table (column1, column2);
# Works!
SELECT * FROM my_table WHERE column1 = 33;
SELECT * FROM my_table WHERE column1 = 33 AND column2 > 3;
# Does not work...
SELECT * FROM my_table WHERE column2 > 3;
15Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Index skip scan
● With index skip scan, the first index part can be omitted in the
WHERE condition
CREATE INDEX my_index ON my_table (column1, column2);
# Works!
SELECT * FROM my_table WHERE column1 = 33;
SELECT * FROM my_table WHERE column1 = 33 AND column2 > 3;
# This works too!
SELECT * FROM my_table WHERE column2 > 3;
16Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Index skip scan
column1 column2
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
2 5
SELECT * FROM my_table WHERE column2 > 3;
column1 = 1 AND
column2 > 3
column1 = 2 AND
column2 > 3
17Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Index skip scan
mysql> EXPLAIN SELECT * FROM my_table WHERE column2 > 3;
+----------+-------+------+------+----------+----------------------------------------+
| table | type | key | rows | filtered | Extra |
+----------+-------+------+------+----------+----------------------------------------+
| my_table | range | idx1 | 85 | 100.00 | Using where; Using index for skip scan |
+----------+-------+------+------+----------+----------------------------------------+
1 row in set, 1 warning (0.00 sec)
18Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Index skip scan
●
Usage criterias:
– The query must only reference columns that exists in the in the index.
– The estimated cost must be the lowest of any alternative (many
distinct values in the first key part will most likely results in a high
cost).
19Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Index skip scan
Few distinct values
"skip_scan_range": {
"potential_skip_scan_indexes": [{
"index": "idx2",
"tree_travel_cost": 0.45,
"num_groups": 12,
"rows": 138,
"cost": 31.817
}
]
}
Many distinct values
"skip_scan_range": {
"potential_skip_scan_indexes": [{
"index": "idx2",
"tree_travel_cost": 0.5,
"num_groups": 421,
"rows": 832,
"cost": 546.45
}
]
}
20Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Index skip scan
●
Can be controlled by optimizer switch and query hints
1) SET optimizer_switch='skip_scan=off';
2) SELECT /*+ SKIP_SCAN(t3) */ col1 FROM t3 WHERE col2 < 10;
3) SELECT /*+ SKIP_SCAN(t3 idx1) */ col1 FROM t3 WHERE col2 < 10;
4) SELECT /*+ NO_SKIP_SCAN(t3) */ col1 FROM t3 WHERE col2 < 10;
21Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Program Agenda
Functional indexes
Index skip scan
Invisible indexes
1
2
3
4
5
6
7
22Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Invisible indexes – What is it?
● Lets you turn off any index
● Global setting
● Invisible to the optimizer, but it’s still maintained by the
storage engine
● Can be turned on/off completely by setting the optimizer
switch «use_invisible_indexes»
● Primary keys cannot be made invisible
23Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Invisible indexes – How is it used?
1) ALTER TABLE t1 ALTER INDEX idx INVISIBLE;
2) CREATE INDEX idx ON my_table (a_column) INVISIBLE;
3) CREATE TABLE t1 (
col1 INT,
INDEX idx (col1) INVISIBLE
);
24Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Invisible indexes - Example
mysql> EXPLAIN SELECT COUNT(*) FROM lineitem WHERE l_suppkey = 7706;
...+------+---------------+-------------+---------+-------+------+----------+-------------+
...| type | possible_keys | key | key_len | ref | rows | filtered | Extra |
...+------+---------------+-------------+---------+-------+------+----------+-------------+
...| ref | i_l_suppkey | i_l_suppkey | 5 | const | 604 | 100.00 | Using index |
...+------+---------------+-------------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
mysql> ALTER TABLE lineitem ALTER INDEX i_l_suppkey INVISIBLE;
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> EXPLAIN SELECT COUNT(*) FROM lineitem WHERE l_suppkey = 7706;
...+------+---------------+------+---------+------+---------+----------+-------------+
...| type | possible_keys | key | key_len | ref | rows | filtered | Extra |
...+------+---------------+------+---------+------+---------+----------+-------------+
...| ALL | NULL | NULL | NULL | NULL | 5941264 | 0.01 | Using where |
...+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
25Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Invisible indexes - Observability
● See the visibility using INFORMATION_SCHEMA or SHOW INDEXES
mysql> SHOW INDEXES FROM my_table;
+----------+------------+---------------------+...+---------+
| Table | Non_unique | Key_name |...| Visible |
+----------+------------+---------------------+...+---------+
| my_table | 1 | just_to_be_safe_idx |...| NO |
+----------+------------+---------------------+...+---------+
mysql> SELECT INDEX_NAME, IS_VISIBLE FROM INFORMATION_SCHEMA.STATISTICS
> WHERE TABLE_NAME = "my_table";
+---------------------+------------+
| INDEX_NAME | IS_VISIBLE |
+---------------------+------------+
| just_to_be_safe_idx | NO |
+---------------------+------------+
1 row in set (0.01 sec)
26Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Invisible indexes - Observability
mysql> SHOW CREATE TABLE t1;
+-------+----------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`col1` int(11) DEFAULT NULL,
KEY `just_to_be_safe_index` (((`col1` * 2))) /*!80000 INVISIBLE */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+----------------------------------------------------------+
27Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Invisible indexes – what you should remember
●
See if you get any errors due to index hints referring to the
index
●
Slow query log
●
Performance schema
●
Application performance
●
Check sys.schema_unused_indexes
28Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
29Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
The preceding is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
New index features in MySQL 8

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
MySQL 5.7 NEW FEATURES, BETTER PERFORMANCE, AND THINGS THAT WILL BREAK -- Mid...
 
Mysql8 advance tuning with resource group
Mysql8 advance tuning with resource groupMysql8 advance tuning with resource group
Mysql8 advance tuning with resource group
 
Optimizing MySQL
Optimizing MySQLOptimizing MySQL
Optimizing MySQL
 
MySQL For Linux Sysadmins
MySQL For Linux SysadminsMySQL For Linux Sysadmins
MySQL For Linux Sysadmins
 
MySQL partitioning
MySQL partitioning MySQL partitioning
MySQL partitioning
 
DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2
 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server Defaults
 
MySQL Query Optimization
MySQL Query OptimizationMySQL Query Optimization
MySQL Query Optimization
 
Solving Performance Problems Using MySQL Enterprise Monitor
Solving Performance Problems Using MySQL Enterprise MonitorSolving Performance Problems Using MySQL Enterprise Monitor
Solving Performance Problems Using MySQL Enterprise Monitor
 
DBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowDBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should Know
 
MySQL Monitoring Mechanisms
MySQL Monitoring MechanismsMySQL Monitoring Mechanisms
MySQL Monitoring Mechanisms
 
Mysql server query path
Mysql server query pathMysql server query path
Mysql server query path
 
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source SummitMySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
 
MySQL NoSQL APIs
MySQL NoSQL APIsMySQL NoSQL APIs
MySQL NoSQL APIs
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
MySQL Performance Best Practices
MySQL Performance Best PracticesMySQL Performance Best Practices
MySQL Performance Best Practices
 
MySQL JSON Functions
MySQL JSON FunctionsMySQL JSON Functions
MySQL JSON Functions
 
Performance schema and sys schema
Performance schema and sys schemaPerformance schema and sys schema
Performance schema and sys schema
 
Mini Session - Using GDB for Profiling
Mini Session - Using GDB for ProfilingMini Session - Using GDB for Profiling
Mini Session - Using GDB for Profiling
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 

Similar a New index features in MySQL 8

Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Guatemala User Group
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
mCloud
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfz
Joshua Thijssen
 
MySQL Performance Schema in 20 Minutes
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 Minutes
Sveta Smirnova
 

Similar a New index features in MySQL 8 (20)

Five more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQLFive more things about Oracle SQL and PLSQL
Five more things about Oracle SQL and PLSQL
 
2019 indit blackhat_honeypot your database server
2019 indit blackhat_honeypot your database server2019 indit blackhat_honeypot your database server
2019 indit blackhat_honeypot your database server
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfz
 
MySQL 8.0 Released Update
MySQL 8.0 Released UpdateMySQL 8.0 Released Update
MySQL 8.0 Released Update
 
DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2DBA Commands and Concepts That Every Developer Should Know - Part 2
DBA Commands and Concepts That Every Developer Should Know - Part 2
 
Top 10 tips for Oracle performance
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performance
 
5 Cool Things About SQL
5 Cool Things About SQL5 Cool Things About SQL
5 Cool Things About SQL
 
Windowing Functions - Little Rock Tech Fest 2019
Windowing Functions - Little Rock Tech Fest 2019Windowing Functions - Little Rock Tech Fest 2019
Windowing Functions - Little Rock Tech Fest 2019
 
Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019
 
Php forum2015 tomas_final
Php forum2015 tomas_finalPhp forum2015 tomas_final
Php forum2015 tomas_final
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
 
MySQL Performance Schema in 20 Minutes
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 Minutes
 
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Wellington APAC Groundbreakers tour - Upgrading to the 12c OptimizerWellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
 
My sql 5.6&MySQL Cluster 7.3
My sql 5.6&MySQL Cluster 7.3My sql 5.6&MySQL Cluster 7.3
My sql 5.6&MySQL Cluster 7.3
 
Everything you always wanted to know about datetime types but didn’t have tim...
Everything you always wanted to know about datetime types but didn’t have tim...Everything you always wanted to know about datetime types but didn’t have tim...
Everything you always wanted to know about datetime types but didn’t have tim...
 
MySQL 8.0: Secure your replication deployment
MySQL 8.0: Secure your replication deploymentMySQL 8.0: Secure your replication deployment
MySQL 8.0: Secure your replication deployment
 

Último

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Último (20)

Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 

New index features in MySQL 8

  • 1. Copyright © 2019 Oracle and/or its affiliates. All rights reserved. New index features in MySQL 8 Erik Frøseth Software Developer MySQL Optimizer Team February 1st, 2019
  • 2. 2Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
  • 3. 3Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Program Agenda Functional indexes Index skip scan Invisible indexes 1 2 3 4 5 6 7
  • 4. 4Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Program Agenda Functional indexes Index skip scan Invisible indexes 1 2 3 4 5 6 7
  • 5. 5Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Terminology # This creates a single index/key with two index/key parts CREATE INDEX my_index ON my_table (column_a, column_b);
  • 6. 6Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Functional index – what is it? ● A functional index is an index where at least one of the index parts is a function instead of a column. 1) CREATE INDEX my_index ON my_table ((ABS(column_a))); 2) CREATE INDEX my_index ON my_table ((ABS(column_a)), column_b); 3) CREATE TABLE my_table ( column_a VARCHAR(255), INDEX my_index ((UPPER(column_a))) );
  • 7. 7Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Functional index – how do you use it? ● A functional index is created, used and removed exactly like any other index. mysql> CREATE TABLE my_table ( -> column_a VARCHAR(255), -> INDEX my_index ((UPPER(column_a))) -> ); Query OK, 0 rows affected (0.04 sec) mysql> EXPLAIN SELECT * FROM my_table WHERE UPPER(column_a) = "FOOBAR"; +-------------+----------+------+----------+---------+-------+------+----------+ | select_type | table | type | key | key_len | ref | rows | filtered | +-------------+----------+------+----------+---------+-------+------+----------+ | SIMPLE | my_table | ref | my_index | 1023 | const | 1 | 100.00 | +-------------+----------+------+----------+---------+-------+------+----------+ 1 row in set, 1 warning (0.00 sec)
  • 8. 8Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Functional index – how do you use it? mysql> CREATE TABLE my_table ( -> column_a VARCHAR(255), -> column_b INT, -> INDEX my_index ((UPPER(column_a)), column_b) -> ); Query OK, 0 rows affected (0.04 sec) mysql> EXPLAIN SELECT * FROM my_table WHERE UPPER(column_a) = "FOOBAR" AND column_b < 20; +-------------+----------+-------+---------------+----------+---------+------+----------+ | select_type | table | type | possible_keys | key | key_len | rows | filtered | +-------------+----------+-------+---------------+----------+---------+------+----------+ | SIMPLE | my_table | range | my_index | my_index | 1028 | 1 | 100.00 | +-------------+----------+-------+---------------+----------+---------+------+----------+ 1 row in set, 1 warning (0.00 sec) ● You can mix functional and non-functional index parts
  • 9. 9Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Functional index – observability mysql> SHOW INDEX FROM t1; +-------+------------+----------+--------------+-------------+...+-------------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name |...| Expression | +-------+------------+----------+--------------+-------------+...+-------------------+ | t1 | 1 | idx | 1 | NULL |...| (`col1` + `col1`) | +-------+------------+----------+--------------+-------------+...+-------------------+ 1 row in set (0.01 sec) mysql> SELECT * FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_NAME = "t1"; +...+------------+--------------+-------------+...+-------------------+ |...| INDEX_NAME | SEQ_IN_INDEX | COLUMN_NAME |...| EXPRESSION | +...+------------+--------------+-------------+...+-------------------+ |...| idx | 1 | NULL |...| (`col1` + `col1`) | +...+------------+--------------+-------------+...+-------------------+ 1 row in set (0.00 sec)
  • 10. 10Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Functional index – observability mysql> SHOW CREATE TABLE t1; +-------+-----------------------------------------------------------+ | Table | Create Table | +-------+-----------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `col1` int(11) DEFAULT NULL, KEY `idx` (((`col1` + `col1`))) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-------+----------------------------------------------------------+ 1 row in set (0.00 sec)
  • 11. 11Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Functional index – use cases The main use case is indexing complex type such as geometries and (especially) JSON. CREATE INDEX json_extract_idx ON my_table ((CAST(data->>'$.person.id' AS CHAR(100)))); { “person”: { “first_name”: “Erik”, “last_name”: “Frøseth”, “id”: 8 } }
  • 12. 12Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Functional index – limitations and other notes ● The primary key cannot be a functional index ● You cannot index a function that returns a BLOB or TEXT ● You can not index non-deterministic functions (RAND(), NOW(), UUID() etc...) ● Each functional index part “uses” one column ● It’s more costly to maintain a functional index than a regular index. Do not overuse it!
  • 13. 13Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Program Agenda Functional indexes Index skip scan Invisible indexes 1 2 3 4 5 6 7
  • 14. 14Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Index skip scan ● Based on a contribution from Facebook ● An index can only be used if the first/leading column(s) is referenced in the WHERE-condition CREATE INDEX my_index ON my_table (column1, column2); # Works! SELECT * FROM my_table WHERE column1 = 33; SELECT * FROM my_table WHERE column1 = 33 AND column2 > 3; # Does not work... SELECT * FROM my_table WHERE column2 > 3;
  • 15. 15Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Index skip scan ● With index skip scan, the first index part can be omitted in the WHERE condition CREATE INDEX my_index ON my_table (column1, column2); # Works! SELECT * FROM my_table WHERE column1 = 33; SELECT * FROM my_table WHERE column1 = 33 AND column2 > 3; # This works too! SELECT * FROM my_table WHERE column2 > 3;
  • 16. 16Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Index skip scan column1 column2 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 2 5 SELECT * FROM my_table WHERE column2 > 3; column1 = 1 AND column2 > 3 column1 = 2 AND column2 > 3
  • 17. 17Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Index skip scan mysql> EXPLAIN SELECT * FROM my_table WHERE column2 > 3; +----------+-------+------+------+----------+----------------------------------------+ | table | type | key | rows | filtered | Extra | +----------+-------+------+------+----------+----------------------------------------+ | my_table | range | idx1 | 85 | 100.00 | Using where; Using index for skip scan | +----------+-------+------+------+----------+----------------------------------------+ 1 row in set, 1 warning (0.00 sec)
  • 18. 18Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Index skip scan ● Usage criterias: – The query must only reference columns that exists in the in the index. – The estimated cost must be the lowest of any alternative (many distinct values in the first key part will most likely results in a high cost).
  • 19. 19Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Index skip scan Few distinct values "skip_scan_range": { "potential_skip_scan_indexes": [{ "index": "idx2", "tree_travel_cost": 0.45, "num_groups": 12, "rows": 138, "cost": 31.817 } ] } Many distinct values "skip_scan_range": { "potential_skip_scan_indexes": [{ "index": "idx2", "tree_travel_cost": 0.5, "num_groups": 421, "rows": 832, "cost": 546.45 } ] }
  • 20. 20Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Index skip scan ● Can be controlled by optimizer switch and query hints 1) SET optimizer_switch='skip_scan=off'; 2) SELECT /*+ SKIP_SCAN(t3) */ col1 FROM t3 WHERE col2 < 10; 3) SELECT /*+ SKIP_SCAN(t3 idx1) */ col1 FROM t3 WHERE col2 < 10; 4) SELECT /*+ NO_SKIP_SCAN(t3) */ col1 FROM t3 WHERE col2 < 10;
  • 21. 21Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Program Agenda Functional indexes Index skip scan Invisible indexes 1 2 3 4 5 6 7
  • 22. 22Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Invisible indexes – What is it? ● Lets you turn off any index ● Global setting ● Invisible to the optimizer, but it’s still maintained by the storage engine ● Can be turned on/off completely by setting the optimizer switch «use_invisible_indexes» ● Primary keys cannot be made invisible
  • 23. 23Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Invisible indexes – How is it used? 1) ALTER TABLE t1 ALTER INDEX idx INVISIBLE; 2) CREATE INDEX idx ON my_table (a_column) INVISIBLE; 3) CREATE TABLE t1 ( col1 INT, INDEX idx (col1) INVISIBLE );
  • 24. 24Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Invisible indexes - Example mysql> EXPLAIN SELECT COUNT(*) FROM lineitem WHERE l_suppkey = 7706; ...+------+---------------+-------------+---------+-------+------+----------+-------------+ ...| type | possible_keys | key | key_len | ref | rows | filtered | Extra | ...+------+---------------+-------------+---------+-------+------+----------+-------------+ ...| ref | i_l_suppkey | i_l_suppkey | 5 | const | 604 | 100.00 | Using index | ...+------+---------------+-------------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) mysql> ALTER TABLE lineitem ALTER INDEX i_l_suppkey INVISIBLE; Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT COUNT(*) FROM lineitem WHERE l_suppkey = 7706; ...+------+---------------+------+---------+------+---------+----------+-------------+ ...| type | possible_keys | key | key_len | ref | rows | filtered | Extra | ...+------+---------------+------+---------+------+---------+----------+-------------+ ...| ALL | NULL | NULL | NULL | NULL | 5941264 | 0.01 | Using where | ...+------+---------------+------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
  • 25. 25Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Invisible indexes - Observability ● See the visibility using INFORMATION_SCHEMA or SHOW INDEXES mysql> SHOW INDEXES FROM my_table; +----------+------------+---------------------+...+---------+ | Table | Non_unique | Key_name |...| Visible | +----------+------------+---------------------+...+---------+ | my_table | 1 | just_to_be_safe_idx |...| NO | +----------+------------+---------------------+...+---------+ mysql> SELECT INDEX_NAME, IS_VISIBLE FROM INFORMATION_SCHEMA.STATISTICS > WHERE TABLE_NAME = "my_table"; +---------------------+------------+ | INDEX_NAME | IS_VISIBLE | +---------------------+------------+ | just_to_be_safe_idx | NO | +---------------------+------------+ 1 row in set (0.01 sec)
  • 26. 26Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Invisible indexes - Observability mysql> SHOW CREATE TABLE t1; +-------+----------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `col1` int(11) DEFAULT NULL, KEY `just_to_be_safe_index` (((`col1` * 2))) /*!80000 INVISIBLE */ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-------+----------------------------------------------------------+
  • 27. 27Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Invisible indexes – what you should remember ● See if you get any errors due to index hints referring to the index ● Slow query log ● Performance schema ● Application performance ● Check sys.schema_unused_indexes
  • 28. 28Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
  • 29. 29Copyright © 2019 Oracle and/or its affiliates. All rights reserved. Safe Harbor Statement The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.