Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Jaime Crespo
Tutorial delivered at Percona MySQL Conference Live London 2013.
It doesn't matter what new SSD technologies appear, or what are the latest breakthroughs in flushing algorithms: the number one cause for MySQL applications being slow is poor execution plan of SQL queries. While the latest GA version provided a huge amount of transparent optimizations -specially for JOINS and subqueries- it is still the developer's responsibility to take advantage of all new MySQL 5.6 features.
In this tutorial we will propose the attendants a sample PHP application with bad response time. Through practical examples, we will suggest step-by-step strategies to improve its performance, including:
* Checking MySQL & InnoDB configuration
* Internal (performance_schema) and external tools for profiling (pt-query-digest)
* New EXPLAIN tools
* Simple and multiple column indexing
* Covering index technique
* Index condition pushdown
* Batch key access
* Subquery optimization
Introduction into MySQL Query Tuning for Dev[Op]sSveta Smirnova
Percona Live Online 2021 talk: https://www.percona.com/resources/videos/introduction-mysql-query-tuning-for-devops
In this talk I will show how to get started with MySQL Query Tuning. I will make a short introduction into physical table structure and demonstrate how it may influence query execution time.
Then we will discuss basic query tuning instruments and techniques, mainly EXPLAIN command with its latest variations. You will learn how to understand its output and how to rewrite queries or change table structure to achieve better performance.
Percona Live 2016 (https://www.percona.com/live/data-performance-conference-2016/sessions/why-use-explain-formatjson). Although EXPLAIN FORMAT=JSON was first presented a long time ago, there still aren't many resources that explain how and why to use it. The most advertised feature is visual EXPLAIN in MySQL Workbench, but this format can do more than create nice pictures. It prints additional information that can't be found in good old tabular EXPLAIN, and can help to solve many tricky performance issues. In this session, I will not only describe which additional information we can get with the new syntax, but also provide examples showing how to use it to diagnose production issues.
Introduction to MySQL Query Tuning for Dev[Op]sSveta Smirnova
To get data, we query the database. MySQL does its best to return requested bytes as fast as possible. However, it needs human help to identify what is important and should be accessed in the first place.
Queries, written smartly, can significantly outperform automatically generated ones. Indexes and Optimizer statistics, not limited to the Histograms only, help to increase the speed of the query a lot.
In this session, I will demonstrate by examples of how MySQL query performance can be improved. I will focus on techniques, accessible by Developers and DevOps rather on those which are usually used by Database Administrators. In the end, I will present troubleshooting tools which will help you to identify why your queries do not perform. Then you could use the knowledge from the beginning of the session to improve them.
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Jaime Crespo
Tutorial delivered at Percona MySQL Conference Live London 2013.
It doesn't matter what new SSD technologies appear, or what are the latest breakthroughs in flushing algorithms: the number one cause for MySQL applications being slow is poor execution plan of SQL queries. While the latest GA version provided a huge amount of transparent optimizations -specially for JOINS and subqueries- it is still the developer's responsibility to take advantage of all new MySQL 5.6 features.
In this tutorial we will propose the attendants a sample PHP application with bad response time. Through practical examples, we will suggest step-by-step strategies to improve its performance, including:
* Checking MySQL & InnoDB configuration
* Internal (performance_schema) and external tools for profiling (pt-query-digest)
* New EXPLAIN tools
* Simple and multiple column indexing
* Covering index technique
* Index condition pushdown
* Batch key access
* Subquery optimization
Introduction into MySQL Query Tuning for Dev[Op]sSveta Smirnova
Percona Live Online 2021 talk: https://www.percona.com/resources/videos/introduction-mysql-query-tuning-for-devops
In this talk I will show how to get started with MySQL Query Tuning. I will make a short introduction into physical table structure and demonstrate how it may influence query execution time.
Then we will discuss basic query tuning instruments and techniques, mainly EXPLAIN command with its latest variations. You will learn how to understand its output and how to rewrite queries or change table structure to achieve better performance.
Percona Live 2016 (https://www.percona.com/live/data-performance-conference-2016/sessions/why-use-explain-formatjson). Although EXPLAIN FORMAT=JSON was first presented a long time ago, there still aren't many resources that explain how and why to use it. The most advertised feature is visual EXPLAIN in MySQL Workbench, but this format can do more than create nice pictures. It prints additional information that can't be found in good old tabular EXPLAIN, and can help to solve many tricky performance issues. In this session, I will not only describe which additional information we can get with the new syntax, but also provide examples showing how to use it to diagnose production issues.
Introduction to MySQL Query Tuning for Dev[Op]sSveta Smirnova
To get data, we query the database. MySQL does its best to return requested bytes as fast as possible. However, it needs human help to identify what is important and should be accessed in the first place.
Queries, written smartly, can significantly outperform automatically generated ones. Indexes and Optimizer statistics, not limited to the Histograms only, help to increase the speed of the query a lot.
In this session, I will demonstrate by examples of how MySQL query performance can be improved. I will focus on techniques, accessible by Developers and DevOps rather on those which are usually used by Database Administrators. In the end, I will present troubleshooting tools which will help you to identify why your queries do not perform. Then you could use the knowledge from the beginning of the session to improve them.
MySQL performance can be improved by tuning queries, server options, and hardware. Traditionally it was an area of responsibility of three different roles: Development, DBA and System Administrators. Now DevOps handle these all. But there is a gap. Knowledge, gained by MySQL DBAs after years or focus on the single product is hard to gain when you focus on more than one. This is why I am doing this session. I will show minimal, but the most effective, set of options which will improve MySQL performance. For illustrations, I will use real user stories, gained by my Support experience, and Kubernetes operators, now available from all main MySQL eco-system vendors: Oracle, MariaDB, and Percona.
Presented at Open Source Summit Europe 2020: https://sched.co/eCGf
Just about anyone can write a basic SQL query for a table. Not everyone can write a good query though - that takes practice and knowing how to understand what the optimizer is doing with the query. Learn the basics of query optimization so you keep your application engaging the user rather then showing the progress bar as they wait on the database.
Talk at "Istanbul Tech Talks" in Istanbul, April, 17, 2018. http://www.istanbultechtalks.com/
In this talk I will show how to get started with MySQL Query Tuning. I will make short introduction into physical table structure and demonstrate how it may influence query execution time. Then we will discuss basic query tuning instruments and techniques, mainly EXPLAIN command with its latest variations. You will learn how to understand its output and how to rewrite query or change table structure to achieve better performance.
New features in Performance Schema 5.7 in actionSveta Smirnova
Tutorial for PerconaLive Amsterdam - September, 2015
Virtual machine can be downloaded from https://drive.google.com/file/d/0B67lYkmv0ZcsNE5wTFJUNC1sUlk/view?usp=sharing
Modern solutions for modern database load: improvements in the latest MariaDB...Sveta Smirnova
Presented at MariaDB Server Fest 2020: https://mariadb.org/fest2020/improvements/
MariaDB is famous for working well in high-performance environments. But our view of what to call high-performance changes over time. Every year we get faster data transfer speed; more devices connected to the Internet; more users and, as a result, more data.
Challenges, which developers have to solve, are getting harder. This session shows what engineers do to keep the product up to date, focusing on MariaDB improvements that make it different from its predecessor, MySQL.
Tutorial which I presented at Percona Live 2018 conference (https://www.percona.com/live/18/sessions/mysql-performance-schema-in-action) together with Alexander Rubin.
====
Performance Schema in MySQL is maturing from version to version. It includes extended lock instrumentation, memory usage statistics, new tables for server variables, first time ever instrumentation for user variables, prepared statements and stored routines.
Version 8.0 adds additional variables, replication, error messages, data locks instrumentation. A lot! Amazing! And complicated!
In this tutorial, we will try all these instruments out. We will provide a test environment and a few typical problems that would be difficult to solve before MySQL 5.7. Just few examples:
- "Where is memory going?"
- "Why are these queries hanging?"
- "How huge is the overhead of my stored procedures?"
- "Why are queries waiting for metadata locks?"
You will not only learn how to collect and use this information but will gain practical experience with it. You will also learn many details on how to setup Performance Schema.
Optimizer Trace Walkthrough talk from MariaDB Fest 2020.
Covers trace contents, reasons why this feature was implemented this way, comparison with Optimizer Trace in MySQL 8
Billion Goods in Few Categories: how Histograms Save a Life?Sveta Smirnova
We store data with the intention to use it: search, retrieve, group, sort... To perform these actions effectively MySQL storage engines index data and communicate statistics with the Optimizer when it compiles a query execution plan. This approach works perfectly well unless your data distribution is not even.
Last year I worked on several tickets where data follow the same pattern: millions of popular products fit into a couple of categories and rest used the rest. We had a hard time to find a solution for retrieving goods fast. Workarounds for version 5.7 were offered. However new MySQL 8.0 feature: histograms, - would work better, cleaner and faster. This is how the idea of the talk was born.
I will discuss
- how index statistics physically stored
- which data exchanged with the Optimizer
- why it is not enough to make correct index choice
In the end, I will explain which issues resolve histograms and why using index statistics is insufficient for fast retrieving of not evenly distributed data.
https://www.percona.com/live/e18/sessions/billion-goods-in-few-categories-how-histograms-save-a-life
MySQL indexing is one of the areas where you can achieve the most performance gains. In this presentation we have a short look how we can improve MySQL performance with indexing...
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)Valeriy Kravchuk
The recently released MariaDB 10.5 GA includes many new, useful features, but I’d like to concentrate on those helping DBAs and support engineers to find out what’s going on when a problem occurs.
Specifically I present and discuss the Performance Schema updates to match MySQL 5.7 instrumentation, new tables in the INFORMATION_SCHEMA to monitor the internals of a generic thread pool and improvements of ANALYZE for statements.
MySQL performance can be improved by tuning queries, server options, and hardware. Traditionally it was an area of responsibility of three different roles: Development, DBA and System Administrators. Now DevOps handle these all. But there is a gap. Knowledge, gained by MySQL DBAs after years or focus on the single product is hard to gain when you focus on more than one. This is why I am doing this session. I will show minimal, but the most effective, set of options which will improve MySQL performance. For illustrations, I will use real user stories, gained by my Support experience, and Kubernetes operators, now available from all main MySQL eco-system vendors: Oracle, MariaDB, and Percona.
Presented at Open Source Summit Europe 2020: https://sched.co/eCGf
Just about anyone can write a basic SQL query for a table. Not everyone can write a good query though - that takes practice and knowing how to understand what the optimizer is doing with the query. Learn the basics of query optimization so you keep your application engaging the user rather then showing the progress bar as they wait on the database.
Talk at "Istanbul Tech Talks" in Istanbul, April, 17, 2018. http://www.istanbultechtalks.com/
In this talk I will show how to get started with MySQL Query Tuning. I will make short introduction into physical table structure and demonstrate how it may influence query execution time. Then we will discuss basic query tuning instruments and techniques, mainly EXPLAIN command with its latest variations. You will learn how to understand its output and how to rewrite query or change table structure to achieve better performance.
New features in Performance Schema 5.7 in actionSveta Smirnova
Tutorial for PerconaLive Amsterdam - September, 2015
Virtual machine can be downloaded from https://drive.google.com/file/d/0B67lYkmv0ZcsNE5wTFJUNC1sUlk/view?usp=sharing
Modern solutions for modern database load: improvements in the latest MariaDB...Sveta Smirnova
Presented at MariaDB Server Fest 2020: https://mariadb.org/fest2020/improvements/
MariaDB is famous for working well in high-performance environments. But our view of what to call high-performance changes over time. Every year we get faster data transfer speed; more devices connected to the Internet; more users and, as a result, more data.
Challenges, which developers have to solve, are getting harder. This session shows what engineers do to keep the product up to date, focusing on MariaDB improvements that make it different from its predecessor, MySQL.
Tutorial which I presented at Percona Live 2018 conference (https://www.percona.com/live/18/sessions/mysql-performance-schema-in-action) together with Alexander Rubin.
====
Performance Schema in MySQL is maturing from version to version. It includes extended lock instrumentation, memory usage statistics, new tables for server variables, first time ever instrumentation for user variables, prepared statements and stored routines.
Version 8.0 adds additional variables, replication, error messages, data locks instrumentation. A lot! Amazing! And complicated!
In this tutorial, we will try all these instruments out. We will provide a test environment and a few typical problems that would be difficult to solve before MySQL 5.7. Just few examples:
- "Where is memory going?"
- "Why are these queries hanging?"
- "How huge is the overhead of my stored procedures?"
- "Why are queries waiting for metadata locks?"
You will not only learn how to collect and use this information but will gain practical experience with it. You will also learn many details on how to setup Performance Schema.
Optimizer Trace Walkthrough talk from MariaDB Fest 2020.
Covers trace contents, reasons why this feature was implemented this way, comparison with Optimizer Trace in MySQL 8
Billion Goods in Few Categories: how Histograms Save a Life?Sveta Smirnova
We store data with the intention to use it: search, retrieve, group, sort... To perform these actions effectively MySQL storage engines index data and communicate statistics with the Optimizer when it compiles a query execution plan. This approach works perfectly well unless your data distribution is not even.
Last year I worked on several tickets where data follow the same pattern: millions of popular products fit into a couple of categories and rest used the rest. We had a hard time to find a solution for retrieving goods fast. Workarounds for version 5.7 were offered. However new MySQL 8.0 feature: histograms, - would work better, cleaner and faster. This is how the idea of the talk was born.
I will discuss
- how index statistics physically stored
- which data exchanged with the Optimizer
- why it is not enough to make correct index choice
In the end, I will explain which issues resolve histograms and why using index statistics is insufficient for fast retrieving of not evenly distributed data.
https://www.percona.com/live/e18/sessions/billion-goods-in-few-categories-how-histograms-save-a-life
MySQL indexing is one of the areas where you can achieve the most performance gains. In this presentation we have a short look how we can improve MySQL performance with indexing...
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)Valeriy Kravchuk
The recently released MariaDB 10.5 GA includes many new, useful features, but I’d like to concentrate on those helping DBAs and support engineers to find out what’s going on when a problem occurs.
Specifically I present and discuss the Performance Schema updates to match MySQL 5.7 instrumentation, new tables in the INFORMATION_SCHEMA to monitor the internals of a generic thread pool and improvements of ANALYZE for statements.
Performance Schema for MySQL TroubleshootingSveta Smirnova
Percona Live (https://www.percona.com/live/data-performance-conference-2016/sessions/performance-schema-mysql-troubleshooting)
The performance schema in MySQL version 5.6, released in February, 2013, is a very powerful tool that can help DBAs discover why even the trickiest performance issues occur. Version 5.7 introduces even more instruments and tables. And while all these give you great power, you can get stuck choosing which instrument to use.
In this session, I will start with a description of a typical problem, then guide you how to use the performance schema to find out what causes the issue, the reason for unwanted behavior and how the received information can help you solve a particular problem.
Traditionally, performance schema sessions teach what is in contained in tables. I will, in contrast, start from a performance issue, then demonstrate which instruments and tables can help solve it. We will discuss how to setup the performance schema so that it has minimal impact on your server.
MySQL 8.0 has come up with an exciting addition of window function which can make the developer life more ease with complex SQL's. This presentation will be a walk through over window functions in MySQL 8.0.
http://www.mydbops.com
Modern query optimisation features in MySQL 8.Mydbops
MySQL 8 (a huge leap forward), indexing capabilities, execution plan enhancements, optimizer improvements, and many other current query tweak features are covered in the slides.
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019Gabriela Ferrara
Sick and tired of "X technology is only good for starting out; after you do, move to Y"? Good news - you don’t need to move away, you just need to get in further! In this talk, you'll learn about improvements in the newest version of the most used database in the world. What are Window Functions? How do you use CTEs? How can the new default encoding help me and what should I look for when upgrading versions?
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
Student information management system project report ii.pdfKamal Acharya
Our project explains about the student management. This project mainly explains the various actions related to student details. This project shows some ease in adding, editing and deleting the student details. It also provides a less time consuming process for viewing, adding, editing and deleting the marks of the students.
We have compiled the most important slides from each speaker's presentation. This year’s compilation, available for free, captures the key insights and contributions shared during the DfMAy 2024 conference.
Overview of the fundamental roles in Hydropower generation and the components involved in wider Electrical Engineering.
This paper presents the design and construction of hydroelectric dams from the hydrologist’s survey of the valley before construction, all aspects and involved disciplines, fluid dynamics, structural engineering, generation and mains frequency regulation to the very transmission of power through the network in the United Kingdom.
Author: Robbie Edward Sayers
Collaborators and co editors: Charlie Sims and Connor Healey.
(C) 2024 Robbie E. Sayers
About
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Technical Specifications
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
Key Features
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface
• Compatible with MAFI CCR system
• Copatiable with IDM8000 CCR
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
Application
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
6th International Conference on Machine Learning & Applications (CMLA 2024)ClaraZara1
6th International Conference on Machine Learning & Applications (CMLA 2024) will provide an excellent international forum for sharing knowledge and results in theory, methodology and applications of on Machine Learning & Applications.
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
Using recycled concrete aggregates (RCA) for pavements is crucial to achieving sustainability. Implementing RCA for new pavement can minimize carbon footprint, conserve natural resources, reduce harmful emissions, and lower life cycle costs. Compared to natural aggregate (NA), RCA pavement has fewer comprehensive studies and sustainability assessments.
Hierarchical Digital Twin of a Naval Power SystemKerry Sado
A hierarchical digital twin of a Naval DC power system has been developed and experimentally verified. Similar to other state-of-the-art digital twins, this technology creates a digital replica of the physical system executed in real-time or faster, which can modify hardware controls. However, its advantage stems from distributing computational efforts by utilizing a hierarchical structure composed of lower-level digital twin blocks and a higher-level system digital twin. Each digital twin block is associated with a physical subsystem of the hardware and communicates with a singular system digital twin, which creates a system-level response. By extracting information from each level of the hierarchy, power system controls of the hardware were reconfigured autonomously. This hierarchical digital twin development offers several advantages over other digital twins, particularly in the field of naval power systems. The hierarchical structure allows for greater computational efficiency and scalability while the ability to autonomously reconfigure hardware controls offers increased flexibility and responsiveness. The hierarchical decomposition and models utilized were well aligned with the physical twin, as indicated by the maximum deviations between the developed digital twin hierarchy and the hardware.
1. www.fromdual.com
1 / 29
Need for Speed:
MySQL Indexing
Percona Live 2013,
November 11 – 12, London
Oli Sennhauser
Senior MySQL Consultant at FromDual GmbH
oli.sennhauser@fromdual.com
2. www.fromdual.com
2 / 29
About FromDual GmbH
● FromDual provides neutral and
independent:
● Consulting for MySQL, Percona Server, MariaDB
● Support for all MySQL and Galera Cluster
● Remote-DBA Services
● MySQL Training
● Oracle Silver Partner (OPN)
www.fromdual.com
4. www.fromdual.com
4 / 29
MySQL and Indexing
● MySQL documentation says:
The best way to improve the performance of SELECT operations is
to create indexes on one or more of the columns that are tested in
the query.
● Great! But:
Unnecessary indexes waste space and waste time to determine
which indexes to use. You must find the right balance to achieve
fast queries using the optimal set of indexes.
● ... hmm so we have to think a bit... :-(
5. www.fromdual.com
5 / 29
What is an Index?
● Adams, Douglas:
The Hitchhiker's
Guide to the
Galaxy?
● Sennhauser, Oli,
Uster?
7. www.fromdual.com
7 / 29
MySQL uses indexes:
● To enforce uniqueness (PRIMARY KEY, UNIQUE
KEY)
● To fast access and filter rows (WHERE)
● To perform joins fast (JOIN)
● To find MIN() and MAX() values
● For sorting and grouping (ORDER BY, GROUP BY)
● To avoid joins by using covering indexes
● To enforce FOREIGN KEY Constraints (FOREIGN
KEY)
8. www.fromdual.com
8 / 29
WHERE clause 1
SELECT *
FROM customers
WHERE name = 'No ClSuHeO Wo fC RMEyASTQEL TLALBCL'E; customersG
CREATE TABLE `customers` (
`customer_id` smallint(5) unsigned
, `name` varchar(64) DEFAULT NULL
, PRIMARY KEY (`customer_id`)
EXPLAIN )
SELECT *
FROM customers
WHERE name = 'No Clue of MySQL LLC';
+-----------+------+---------------+------+-------+-------------+
| table | type | possible_keys | key | rows | Extra |
+-----------+------+---------------+------+-------+-------------+
| customers | ALL | NULL | NULL | 31978 | Using where |
+-----------+------+---------------+------+-------+-------------+
9. www.fromdual.com
9 / 29
How to create and Index?
ALTER TABLE …
● ADD PRIMARY KEY (id);
● ADD UNIQUE KEY (uuid);
● ADD FOREIGN KEY (customer_id)
REFERENCES customers (customer_id);
● ADD INDEX (last_name, first_name);
● ADD INDEX pre_ind (hash(8));
● ADD FULLTEXT INDEX (last_name,
first_name);
10. www.fromdual.com
10 / 29
WHERE clause 2
ALTER TABLE customers
ADD INDEX (name);
CREATE TABLE `customers` (
`customer_id` smallint(5) unsigned
Gain: 20 ms → 5 ms
, `name` varchar(64) DEFAULT NULL
, PRIMARY KEY (`customer_id`)
, KEY `name` (`name`)
)
+-----------+------+---------------+------+---------+-------+------+
| table | type | possible_keys | key | key_len | ref | rows |
+-----------+------+---------------+------+---------+-------+------+
| customers | ref | name | name | 67 | const | 1 |
+-----------+------+---------------+------+---------+-------+------+
11. www.fromdual.com
11 / 29
JOIN clause
EXPLAIN SELECT *
FROM customers AS c
JOIN orders AS o ON c.customer_id = o.customer_id
WHERE c.name = 'No Clue of MySQL LLC';
+-------+------+---------------+------+---------+-------+---------+
| table | type | possible_keys | key | key_len | ref | rows |
+-------+------+---------------+------+---------+-------+---------+
| c | ref | PRIMARY,Gain: name 450 | name ms | → 67 6 ms
| const | 1 |
| o | ALL | NULL | NULL | NULL | NULL | 1045105 |
+-------+------+---------------+------+---------+-------+---------+
ALTER TABLE orders
ADD INDEX (customer_id);
+-------+------+---------------+-------------+---------+---------------+------+
| table | type | possible_keys | key | key_len | ref | rows |
+-------+------+---------------+-------------+---------+---------------+------+
| c | ref | PRIMARY,name | name | 67 | const | 1 |
| o | ref | customer_id | customer_id | 3 | c.customer_id | 8 |
+-------+------+---------------+-------------+---------+---------------+------+
12. www.fromdual.com
12 / 29
For sorting/grouping tables
ORDER BY, GROUP BY
EXPLAIN SELECT *
FROM contacts AS c
WHERE last_name = 'Sennhauser'
ORDER BY last_name, first_name;
+-------+------+-----------+------+----------------------------------------------------+
| table | type | key | +-------+------+-----------+------+----------------------------------------------------+
Gain: rows | Extra 20 ms → 7 ms
|
| c | ref | last_name | 1561 | Using index condition; Using where; Using filesort |
+-------+------+-----------+------+----------------------------------------------------+
ALTER TABLE contacts
ADD INDEX (last_name, first_name);
+----------+------+-------------+------+--------------------------+
| table | type | key | rows | Extra |
+----------+------+-------------+------+--------------------------+
| contacts | ref | last_name_2 | 1561 | Using where; Using index |
+----------+------+-------------+------+--------------------------+
13. www.fromdual.com
13 / 29
Covering Indexes
EXPLAIN
SELECT customer_id, amount
FROM orders AS o
WHERE customer_id = 59349;
+-------+------+-------------+------+-------+
| table | type | key | rows | Extra |
+-------+------+-------------+------+-------+
| o | ref | customer_id So | what?
15 | NULL |
+-------+------+-------------+------+-------+
ALTER TABLE orders
ADD INDEX (customer_id, amount);
+-------+------+---------------+------+-------------+
| table | type | key | rows | Extra |
+-------+------+---------------+------+-------------+
| o | ref | customer_id_2 | 15 | Using index |
+-------+------+---------------+------+-------------+
14. www.fromdual.com
14 / 29
Benefit of Covering Indexes
● Why are Covering Indexes beneficial
15. www.fromdual.com
15 / 29
How-to find missing indexes?
● ER Diagram? :-(
● Most of them rely to you business logic...
● How-to FIND?
● Slow Query Log
● MySQL Variables:
● Since v5.1
on-line!
+-------------------------------+----------+
| Variable_name | Value |
+-------------------------------+----------+
| log_queries_not_using_indexes | ON |
| long_query_time | 0.250000 |
| min_examined_row_limit | 100 |
| slow_query_log | ON |
| slow_query_log_file | slow.log |
+-------------------------------+----------+
16. www.fromdual.com
16 / 29
Indexes are not only good
● Indexes use space (Disk, hot data in RAM!)
● Indexes use time to maintain (CPU, RAM, I/O)
● Optimizer needs time to determine which indexes to
use.
● Sometimes optimizer is completely confused and
does wrong decisions if too many (similar) indexes
are there.
→ You must find the right
balance to achieve fast
queries using the optimal
set of indexes.
17. www.fromdual.com
17 / 29
Smaller indexes faster queries
● Better fit into memory (less I/O)
● Higher data density (rows/block)
● Less CPU cycles (to crawl through)
● Prefixed indexes:
ADD INDEX pre_ind (hash(8));
18. www.fromdual.com
18 / 29
Avoid indexes
● Avoid redundant (and thus unnecessary ) indexes
● How does it happen?
● Developer 1: Creates a Foreign Key constraint → done
● Developer 2: Ouu! Query is slow → Oli told me to
create an index! → done
● Developer 3: Ouu! Query is slow → Developer 2 is
stupid! → Create and index → done
● Frameworks vs. Developer
● Upgrade process vs. Developer
● Avoid indexes which are not used / needed
19. www.fromdual.com
19 / 29
How to find such indexes?
SHOW CREATE TABLE ...G
mysqldump nodata
> structure_dump.sql
● Since MySQL 5.6: PERFORMANCE_SCHEMA
● Percona Server / MariaDB: Userstats
● http://fromdual.com/mysql-performance-schema-hints
SELECT object_schema, object_name, index_name
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NOT NULL
AND count_star = 0
ORDER BY object_schema, object_name;
20. www.fromdual.com
20 / 29
Avoid partial redundant indexes
● INDEX (city, last_name, first_name)
● INDEX (city, last_name)
● INDEX (city)
● INDEX (last_name, city)
???
● INDEX (first_name, last_name)
!!!
21. www.fromdual.com
SELECT status, COUNT(*)
21 / 29
Bad selectivity
● Remove indexes with bad selectivity (~= low cardinality)
● Candidates are:
● status
● gender
● active
● How to find if field has bad
selectivity?
Indexes (and Joins) are expensive!!!
● Break even between 15% and 66%
FROM orders
GROUP BY status;
+--------+--------+
| status | cnt |
+--------+--------+
| 0 | 393216 |
| 1 | 262144 |
| 2 | 12 |
| 3 | 36 |
| 4 | 24 |
| 5 | 4 |
| 6 | 8 |
+--------+--------+
● Lets see if the MySQL optimizer knows about it... :-)
22. www.fromdual.com
SELECT status, COUNT(*)
+--------+--------+
| status | cnt |
+--------+--------+
| 0 | 393216 |
| 1 | 262144 |
| 2 | 12 |
| 3 | 36 |
| 4 | 24 |
| 5 | 4 |
| 6 | 8 |
+--------+--------+
22 / 29
Optimizer is wrong!
SELECT * FROM orders WHERE status = 2;
+--------+------+---------------+--------+------+
| table | type | possible_keys | key | rows |
+--------+------+---------------+--------+------+
| orders | ref | status | status | 12 |
+--------+------+---------------+--------+------+
SELECT * FROM orders WHERE status = 0;
1.43 s
+--------+------+---------------+--------+--------+
| table | type | possible_keys | key | rows |
+--------+------+---------------+--------+--------+
| orders | ref | status | status | 327469 |
+--------+------+---------------+--------+--------+
FROM orders
GROUP BY status;
SELECT * FROM orders IGNORE INDEX (STATUS) WHERE status = 0;
0.44 s
+--------+------+---------------+------+--------+
| table | type | possible_5.6.12 keys (after | key analyze | rows table)
|
+--------+------+---------------+------+--------+
| orders | ALL | NULL | NULL | 654939 |
+--------+------+---------------+------+--------+
23. www.fromdual.com
23 / 29
InnoDB PK and SK
● InnoDB has
● Primary Keys and
● Secondary Keys
24. www.fromdual.com
24 / 29
Clustered Index
● InnoDB: Data = Leaf of Primary Key
● We call this an Index Clustered Table (IOT)
→ Data are sorted like PK (key is sorted)!
→ PK influences Locality of data (physical
location)
● AUTO_INCREMENT ~= sorting by time!
● Good for many things
● where hot data = recent data
● Bad for time series
● Where hot data = per item data
25. www.fromdual.com
25 / 29
Example: InnoDB
A_I ts v_id xpos ypos ...
1 17:30 #42 x, y, ...
2 17:30 #43 x, y, ...
3 17:30 #44 x, y, ...
...
2001 17:32 #42 x, y, ...
2002 17:32 #43 x, y, ...
2003 17:32 #44 x, y, ...
#42 every 2'
over the last 3 days
~ 2000 rows
~ 200 kbyte
Q1: Δ in rows?
A1: 1 row ~ 100 byte
Q2: Δ in bytes?
Q3: Default InnoDB block size?
default: 16 kbyte
Q4: Avg. # of rows of car #42 in 1 InnoDB block?
A2: 3 d and 720 pt/d → ~2000 pt ~ 2000 rec ~ 2000 blk
Q5: How long will this take and why (32 Mbyte)?
~ 2000 IOPS ~ 10s random read!!!
S: All in RAM or strong I/O system or …?
~ 1
2000 trucks
26. www.fromdual.com
26 / 29
InnoDB PK safes the day!
ts v_id xpos ypos ...
17:30 #42 x, y, ...
17:32 #42 x, y, ...
17:34 #42 x, y, ...
... #42 every 2'
17:30 #43 x, y, ...
17:32 #43 x, y, ...
17:34 #43 x, y, ...
over the last 3 days
Q1: Avg. # of rows of car #42 in 1 InnoDB block?
A1: 3 d and 720 pt/d → ~2000 pt ~ 2000 rec ~ 20 blk
Q2: How long will this take and why (320 kbyte)?
~ 1-2 IOPS ~ 10-20 ms sequential read!
S: Wow f=50 faster! Any drawbacks?
~ 120
2000 trucks
17:30 #44 x, y, ... ...
27. www.fromdual.com
27 / 29
Index hints
● MySQL optimizer is sometimes wrong!
● We have to help (= hint) him...
● Index hints are:
● USE INDEX (ind1, ind2)
● Only consider these indexes
● FORCE INDEX (ind3)
● Use this index without considering anything else
● IGNORE INDEX (ind1, ind3)
● Do NOT consider these indexes but everything else
● Hints should be used only as a last resort
28. www.fromdual.com
28 / 29
MySQL Variables
● MySQL variables influencing index use
● MyISAM: key_buffer_size
● InnoDB: innodb_buffer_pool_size /
innodb_buffer_pool_instances
● InnoDB Change Buffer
● innodb_change_buffer_max_size
● innodb_change_buffering
● Adaptive Hash Index (AHI)
● MySQL 5.6.3 / 5.5.14 index length 767 → 3072 bytes
● innodb_large_prefix
29. www.fromdual.com
29 / 29
Q & A
Questions ?
Discussion?
We have time for some face-to-face talks...
● FromDual provides neutral and independent:
● Consulting
● Remote-DBA
● Support for MySQL, Galera, Percona Server and MariaDB
● Training
www.fromdual.com/presentations