SlideShare una empresa de Scribd logo
1 de 34
Covering indexes

Stéphane Combaudon - SQLI
Indexing basics

 Data structure intended to speed up SELECTs

 Similar to an index in a book

 Overhead for every write
       ● Usually negligeable / speed up for SELECT




 Possibility to have one index for several columns
Index types
SHOW INDEX info
BTree indexes




   All leaves at the same distance from the root
   Efficient insertions, deletions
   Values are sorted
   B+Trees
           ● Efficient range scans


           ● Values stored in the leaves
BTree indexes

 Ok for most kinds of lookups:
        ● Exact full value (= xxx)


        ● Range of values (BETWEEN xx AND yy)


        ● Column prefix (LIKE 'xx%')


        ● Leftmost prefix




 Ok for sorting too

 But
        ● Not useful for 'LIKE %xxx' or LIKE '%xx%'
        ● You can't skip columns
Hash indexes

 Hash table with hash and pointer to row




Drawbacks
       ● Useful only for exact lookups (=, IN)


       ● Not supported by InnoDB or MyISAM




 Benefits
        ● Very fast


        ● Compact
R-Tree and T-Tree indexes

 R-Tree Indexes
        ● Same principle as B-Tree indexes


        ● Used for spatial indexes


        ● Requires the use of GIS functions


        ● MyISAM only




 T-Tree indexes
        ● Same principle as B-Tree indexes


        ● Specialized for in-memory storage engines


        ● Used in NDB Cluster
Index and data layouts
Data and indexes for MyISAM
 Data, primary key and secondary key (simplified)




 No structural difference between PK and secondary
  key
Data and indexes for InnoDB
 Data, primary key and secondary key (simplified)




 Two lookups needed to get row from secondary key
Accessing data
Different methods to access data

 Disk : cheap but slow
         ● ~ 100 random I/O ops/s


         ● ~ 500,000 sequential I/O ops/s




 RAM : quick but expensive
       ● ~ 250,000 random accesses/s


       ● ~ 5,000,000 sequential accesses/s




 Remember :
      ● Disks are extremely slow for random accesses


      ● Not much difference for sequential accesses
Covering indexes
Index-covered queries

 When performance problems occur:
       ● Add indexes


       ● Rewrite your queries


       ● Or both




 Do you need to fetch data (often on disk) ?

 If the index contains the data, you don't

 If you don't, your query is covered by an index (=index-
  only query)
Index-covered queries

 Query with traditional index:
       ● Get right rows with index


       ● Get data from rows


       ● Send data back to client




 Index-covered query:
        ● Get right rows with index


        ● Get data from rows


        ● Send data back to client
Covering index and EXPLAIN

mysql> EXPLAIN SELECT ID FROM world.CityG
*************************** 1. row ***************************
              id: 1
   select_type: SIMPLE
          table: City
           type: index
possible_keys: NULL
            key: PRIMARY
       key_len: 4
            ref: NULL
          rows: 4079
         Extra: Using index
Advantages of a covering index

 No access to the rows anymore !

 Indexes smaller and easier to cache than data

 Indexes sorted by values: random access can become
  sequential access

 Additional trick with InnoDB (more later)

 => Covering indexes are very beneficial for I/O bound
  workloads
When you can't use a covering idx

 SELECT *



 Indexes that don't store the values:
        ● Indexes different from BTree indexes


        ● BTree indexes with MEMORY tables


        ● Indexes on a column's prefix
Case studies
A case study

CREATE TABLE `customer` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(20) NOT NULL DEFAULT '',
    `age` tinyint(4) DEFAULT NULL,
    `subscription` date NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=MyISAM



 Name of people who subscribed on 2009-01-01 ?
 We want this list to be sorted by name
The naïve way
mysql> EXPLAIN SELECT name FROM customer
   WHERE subscription='2009-01-01' ORDER BY name
*************************** 1. row ***************************
               id: 1
    select_type: SIMPLE
           table: customer
           type: ALL
possible_keys: NULL
             key: NULL
       key_len: NULL
              ref: NULL
           rows: 5000000
          Extra: Using where; Using filesort
First try ...
mysql> ALTER TABLE customer ADD INDEX idx_name
       (name)

mysql> EXPLAIN SELECT name FROM customer
   WHERE subscription='2009-01-01' ORDER BY name
*************************** 1. row ***************************
                ...
           type: ALL
possible_keys: NULL
            key: NULL
       key_len: NULL
              ref: NULL
           rows: 5000000
           Extra: Using where; Using filesort
Better ...
mysql> ALTER TABLE customer ADD INDEX idx_sub
       (subscription)

mysql> EXPLAIN SELECT name FROM customer
   WHERE subscription='2009-01-01' ORDER BY name
*************************** 1. row ***************************
           ...
       type: ref
        key: idx_sub
      rows: 4370
      Extra: Using where; Using filesort
The ideal way

mysql> ALTER TABLE customer ADD INDEX
       idx_sub_name (subscription,name)



mysql> EXPLAIN SELECT name FROM customer
   WHERE subscription='2009-01-01' ORDER BY name
*************************** 1. row ***************************
           ...
       type: ref
        key: idx_sub_name
      rows: 4363
      Extra: Using where; Using index
Benchmarks

 Avg number of sec to run the query
       ● Without index: 3.743


       ● Index on subscription: 0.435


       ● Covering index: 0.012




 Covering index
        ● 35x faster than index on subscription


        ● 300x faster than full table scan
Even better for MyISAM

 We can keep the covering index in memory


  mysql> SET GLOBAL
  customer_cache.key_buffer_size = 130000000;
  mysql> CACHE INDEX customer IN customer_cache;
  mysql> LOAD INDEX INTO CACHE customer;

 Avg number of sec to run the query: 0.007

 This step is specific to MyISAM !
Even better for InnoDB

 InnoDB secondary keys hold primary key values


mysql> EXPLAIN SELECT name,id FROM customer
  WHERE subscription='2009-01-01' ORDER BY name

*************************** 1. row ***************************
 possible_keys: idx_sub_name
             key: idx_sub_name
           Extra: Using where; Using index
Another (harder) case study

 Same table : customer

 List people who subscribed on 2009-01-01 AND
  whose name ends up with xx ?

 SELECT * FROM customer WHERE
  subscription='2009-01-01' AND name LIKE '%xx'

 Let's add an index on (subscription,name) ...
Another (harder) case study

mysql> EXPLAIN SELECT * FROM customer WHERE
   subscription='2009-01-01' AND name LIKE '%xx'
*************************** 1. row ***************************
                ...
            key: idx_sub_name
       key_len: 3
             ref: const
           rows: 500272
          Extra: Using where

 The index is not covering anymore
Query rewriting - Indexing

 Rewriting the query
        SELECT * FROM customer
        INNER JOIN (
             SELECT id FROM customer
             WHERE subscription='2009-01-01'
             AND name LIKE '%xx'
         ) AS t USING(id)

 Adding an index
       ALTER TABLE customer ADD INDEX
       idx_sub_name_id (subscription,name,id)
Running EXPLAIN

*************************** 1. row ***************************
 select_type: PRIMARY
          table: <derived2>
*************************** 2. row ***************************
 select_type: PRIMARY
          table: customer
*************************** 3. row ***************************
 select_type: DERIVED
         table: customer
           key: idx_sub_name_id
         Extra: Using where; Using index
Efficiency of the optimization

 Beware of the subquery

 10 subs./3 names with %xx
        ● Original query: 0.000 s


        ● Rewritten query: 0.000 s




 300,000 subs./500 names with %xx
        ● Original query: 1.284 s


        ● Rewritten query: 0.553 s




 Many intermediate situations

 Always benchmark !
InnoDB ?

 The index on (subscription,name) is already covering
  for the subquery

 Your work is easier: just rewrite the query if need be

 But you still need to benchmark

Más contenido relacionado

La actualidad más candente

Cis 336 final exam 2
Cis 336 final exam 2Cis 336 final exam 2
Cis 336 final exam 2lifesgood12
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query TuningAlexander Rubin
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle SqlAhmed Yaseen
 
SQL (Basic to Intermediate Customized 8 Hours)
SQL (Basic to Intermediate Customized 8 Hours)SQL (Basic to Intermediate Customized 8 Hours)
SQL (Basic to Intermediate Customized 8 Hours)Edu4Sure
 
New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3Sergey Petrunya
 
Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Jennifer Berk
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index CookbookMYXPLAIN
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL QueriesAchievers Tech
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functionsNitesh Singh
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performancejkeriaki
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tablesSyed Zaid Irshad
 
Learning sql from w3schools
Learning sql from w3schoolsLearning sql from w3schools
Learning sql from w3schoolsfarhan516
 

La actualidad más candente (20)

Cis 336 final exam 2
Cis 336 final exam 2Cis 336 final exam 2
Cis 336 final exam 2
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
SQL (Basic to Intermediate Customized 8 Hours)
SQL (Basic to Intermediate Customized 8 Hours)SQL (Basic to Intermediate Customized 8 Hours)
SQL (Basic to Intermediate Customized 8 Hours)
 
New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3
 
Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index Cookbook
 
Chapter8 my sql revision tour
Chapter8 my sql revision tourChapter8 my sql revision tour
Chapter8 my sql revision tour
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
 
Mssql
MssqlMssql
Mssql
 
Sql operator
Sql operatorSql operator
Sql operator
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
Sql fundamentals
Sql fundamentalsSql fundamentals
Sql fundamentals
 
Files,blocks and functions in R
Files,blocks and functions in RFiles,blocks and functions in R
Files,blocks and functions in R
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
 
Learning sql from w3schools
Learning sql from w3schoolsLearning sql from w3schools
Learning sql from w3schools
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 

Destacado

What's new in MySQL 5.5?
What's new in MySQL 5.5?What's new in MySQL 5.5?
What's new in MySQL 5.5?Lenz Grimmer
 
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...Dave Stokes
 
InnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevInnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevFuenteovejuna
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Jaime Crespo
 
MariaDB Galera Cluster - Simple, Transparent, Highly Available
MariaDB Galera Cluster - Simple, Transparent, Highly AvailableMariaDB Galera Cluster - Simple, Transparent, Highly Available
MariaDB Galera Cluster - Simple, Transparent, Highly AvailableMariaDB Corporation
 
etcd based PostgreSQL HA Cluster
etcd based PostgreSQL HA Clusteretcd based PostgreSQL HA Cluster
etcd based PostgreSQL HA Clusterwinsletts
 
HBase schema design Big Data TechCon Boston
HBase schema design Big Data TechCon BostonHBase schema design Big Data TechCon Boston
HBase schema design Big Data TechCon Bostonamansk
 
7. Relational Database Design in DBMS
7. Relational Database Design in DBMS7. Relational Database Design in DBMS
7. Relational Database Design in DBMSkoolkampus
 
Security Best Practices for your Postgres Deployment
Security Best Practices for your Postgres DeploymentSecurity Best Practices for your Postgres Deployment
Security Best Practices for your Postgres DeploymentPGConf APAC
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Jargalsaikhan Alyeksandr
 
HBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
HBaseCon 2012 | HBase Schema Design - Ian Varley, SalesforceHBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
HBaseCon 2012 | HBase Schema Design - Ian Varley, SalesforceCloudera, Inc.
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignMongoDB
 

Destacado (17)

What's new in MySQL 5.5?
What's new in MySQL 5.5?What's new in MySQL 5.5?
What's new in MySQL 5.5?
 
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...
 
InnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevInnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter Zaitsev
 
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
 
MariaDB Galera Cluster - Simple, Transparent, Highly Available
MariaDB Galera Cluster - Simple, Transparent, Highly AvailableMariaDB Galera Cluster - Simple, Transparent, Highly Available
MariaDB Galera Cluster - Simple, Transparent, Highly Available
 
etcd based PostgreSQL HA Cluster
etcd based PostgreSQL HA Clusteretcd based PostgreSQL HA Cluster
etcd based PostgreSQL HA Cluster
 
Scalable Real-time analytics using Druid
Scalable Real-time analytics using DruidScalable Real-time analytics using Druid
Scalable Real-time analytics using Druid
 
HBase schema design Big Data TechCon Boston
HBase schema design Big Data TechCon BostonHBase schema design Big Data TechCon Boston
HBase schema design Big Data TechCon Boston
 
Secure PostgreSQL deployment
Secure PostgreSQL deploymentSecure PostgreSQL deployment
Secure PostgreSQL deployment
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, Really
 
7. Relational Database Design in DBMS
7. Relational Database Design in DBMS7. Relational Database Design in DBMS
7. Relational Database Design in DBMS
 
Security Best Practices for your Postgres Deployment
Security Best Practices for your Postgres DeploymentSecurity Best Practices for your Postgres Deployment
Security Best Practices for your Postgres Deployment
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)
 
HBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
HBaseCon 2012 | HBase Schema Design - Ian Varley, SalesforceHBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
HBaseCon 2012 | HBase Schema Design - Ian Varley, Salesforce
 
Intro to HBase
Intro to HBaseIntro to HBase
Intro to HBase
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 

Similar a Covering indexes

Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with ExplainMYXPLAIN
 
Troubleshooting MySQL Performance
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL PerformanceSveta Smirnova
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)Hemant Kumar Singh
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101Sveta Smirnova
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query TuningSveta Smirnova
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON? Sveta Smirnova
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQLEvan Weaver
 
PPC2009_yahoo_mysql_pagination
PPC2009_yahoo_mysql_paginationPPC2009_yahoo_mysql_pagination
PPC2009_yahoo_mysql_paginationmysqlops
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQLSurat Singh Bhati
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index TuningManikanda kumar
 
Advanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdfAdvanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdfFederico Razzoli
 
My sql查询优化实践
My sql查询优化实践My sql查询优化实践
My sql查询优化实践ghostsun
 
Clase 12 manejo indices modificada
Clase 12 manejo indices   modificadaClase 12 manejo indices   modificada
Clase 12 manejo indices modificadaTitiushko Jazz
 
Clase 12 manejo indices modificada
Clase 12 manejo indices   modificadaClase 12 manejo indices   modificada
Clase 12 manejo indices modificadaTitiushko Jazz
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performanceguest9912e5
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the ServerdevObjective
 

Similar a Covering indexes (20)

Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with Explain
 
Troubleshooting MySQL Performance
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL Performance
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON?
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
 
PPC2009_yahoo_mysql_pagination
PPC2009_yahoo_mysql_paginationPPC2009_yahoo_mysql_pagination
PPC2009_yahoo_mysql_pagination
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
Advanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdfAdvanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdf
 
My sql查询优化实践
My sql查询优化实践My sql查询优化实践
My sql查询优化实践
 
Clase 12 manejo indices modificada
Clase 12 manejo indices   modificadaClase 12 manejo indices   modificada
Clase 12 manejo indices modificada
 
Clase 12 manejo indices modificada
Clase 12 manejo indices   modificadaClase 12 manejo indices   modificada
Clase 12 manejo indices modificada
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 
MySQL Indexes
MySQL IndexesMySQL Indexes
MySQL Indexes
 
MySQL performance tuning
MySQL performance tuningMySQL performance tuning
MySQL performance tuning
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
 

Más de MYXPLAIN

Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksMYXPLAIN
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL IndexingMYXPLAIN
 
Advanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisMYXPLAIN
 
Are You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL IndexesAre You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL IndexesMYXPLAIN
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, ReallyMYXPLAIN
 
MySQL 5.6 Performance
MySQL 5.6 PerformanceMySQL 5.6 Performance
MySQL 5.6 PerformanceMYXPLAIN
 
MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MYXPLAIN
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query OptimizationMYXPLAIN
 
Tools and Techniques for Index Design
Tools and Techniques for Index DesignTools and Techniques for Index Design
Tools and Techniques for Index DesignMYXPLAIN
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6MYXPLAIN
 
The Power of MySQL Explain
The Power of MySQL ExplainThe Power of MySQL Explain
The Power of MySQL ExplainMYXPLAIN
 
Improving Performance with Better Indexes
Improving Performance with Better IndexesImproving Performance with Better Indexes
Improving Performance with Better IndexesMYXPLAIN
 
Explaining the MySQL Explain
Explaining the MySQL ExplainExplaining the MySQL Explain
Explaining the MySQL ExplainMYXPLAIN
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewMYXPLAIN
 
Advanced query optimization
Advanced query optimizationAdvanced query optimization
Advanced query optimizationMYXPLAIN
 

Más de MYXPLAIN (15)

Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL Indexing
 
Advanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and Analysis
 
Are You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL IndexesAre You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL Indexes
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, Really
 
MySQL 5.6 Performance
MySQL 5.6 PerformanceMySQL 5.6 Performance
MySQL 5.6 Performance
 
MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query Optimization
 
Tools and Techniques for Index Design
Tools and Techniques for Index DesignTools and Techniques for Index Design
Tools and Techniques for Index Design
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
 
The Power of MySQL Explain
The Power of MySQL ExplainThe Power of MySQL Explain
The Power of MySQL Explain
 
Improving Performance with Better Indexes
Improving Performance with Better IndexesImproving Performance with Better Indexes
Improving Performance with Better Indexes
 
Explaining the MySQL Explain
Explaining the MySQL ExplainExplaining the MySQL Explain
Explaining the MySQL Explain
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
Advanced query optimization
Advanced query optimizationAdvanced query optimization
Advanced query optimization
 

Último

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

Último (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

Covering indexes

  • 2. Indexing basics  Data structure intended to speed up SELECTs  Similar to an index in a book  Overhead for every write ● Usually negligeable / speed up for SELECT  Possibility to have one index for several columns
  • 5. BTree indexes  All leaves at the same distance from the root  Efficient insertions, deletions  Values are sorted  B+Trees ● Efficient range scans ● Values stored in the leaves
  • 6. BTree indexes  Ok for most kinds of lookups: ● Exact full value (= xxx) ● Range of values (BETWEEN xx AND yy) ● Column prefix (LIKE 'xx%') ● Leftmost prefix  Ok for sorting too  But ● Not useful for 'LIKE %xxx' or LIKE '%xx%' ● You can't skip columns
  • 7. Hash indexes  Hash table with hash and pointer to row Drawbacks ● Useful only for exact lookups (=, IN) ● Not supported by InnoDB or MyISAM  Benefits ● Very fast ● Compact
  • 8. R-Tree and T-Tree indexes  R-Tree Indexes ● Same principle as B-Tree indexes ● Used for spatial indexes ● Requires the use of GIS functions ● MyISAM only  T-Tree indexes ● Same principle as B-Tree indexes ● Specialized for in-memory storage engines ● Used in NDB Cluster
  • 9. Index and data layouts
  • 10. Data and indexes for MyISAM  Data, primary key and secondary key (simplified)  No structural difference between PK and secondary key
  • 11. Data and indexes for InnoDB  Data, primary key and secondary key (simplified)  Two lookups needed to get row from secondary key
  • 13. Different methods to access data  Disk : cheap but slow ● ~ 100 random I/O ops/s ● ~ 500,000 sequential I/O ops/s  RAM : quick but expensive ● ~ 250,000 random accesses/s ● ~ 5,000,000 sequential accesses/s  Remember : ● Disks are extremely slow for random accesses ● Not much difference for sequential accesses
  • 15. Index-covered queries  When performance problems occur: ● Add indexes ● Rewrite your queries ● Or both  Do you need to fetch data (often on disk) ?  If the index contains the data, you don't  If you don't, your query is covered by an index (=index- only query)
  • 16. Index-covered queries  Query with traditional index: ● Get right rows with index ● Get data from rows ● Send data back to client  Index-covered query: ● Get right rows with index ● Get data from rows ● Send data back to client
  • 17. Covering index and EXPLAIN mysql> EXPLAIN SELECT ID FROM world.CityG *************************** 1. row *************************** id: 1 select_type: SIMPLE table: City type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 4079 Extra: Using index
  • 18. Advantages of a covering index  No access to the rows anymore !  Indexes smaller and easier to cache than data  Indexes sorted by values: random access can become sequential access  Additional trick with InnoDB (more later)  => Covering indexes are very beneficial for I/O bound workloads
  • 19. When you can't use a covering idx  SELECT *  Indexes that don't store the values: ● Indexes different from BTree indexes ● BTree indexes with MEMORY tables ● Indexes on a column's prefix
  • 21. A case study CREATE TABLE `customer` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', `age` tinyint(4) DEFAULT NULL, `subscription` date NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM  Name of people who subscribed on 2009-01-01 ?  We want this list to be sorted by name
  • 22. The naïve way mysql> EXPLAIN SELECT name FROM customer WHERE subscription='2009-01-01' ORDER BY name *************************** 1. row *************************** id: 1 select_type: SIMPLE table: customer type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 5000000 Extra: Using where; Using filesort
  • 23. First try ... mysql> ALTER TABLE customer ADD INDEX idx_name (name) mysql> EXPLAIN SELECT name FROM customer WHERE subscription='2009-01-01' ORDER BY name *************************** 1. row *************************** ... type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 5000000 Extra: Using where; Using filesort
  • 24. Better ... mysql> ALTER TABLE customer ADD INDEX idx_sub (subscription) mysql> EXPLAIN SELECT name FROM customer WHERE subscription='2009-01-01' ORDER BY name *************************** 1. row *************************** ... type: ref key: idx_sub rows: 4370 Extra: Using where; Using filesort
  • 25. The ideal way mysql> ALTER TABLE customer ADD INDEX idx_sub_name (subscription,name) mysql> EXPLAIN SELECT name FROM customer WHERE subscription='2009-01-01' ORDER BY name *************************** 1. row *************************** ... type: ref key: idx_sub_name rows: 4363 Extra: Using where; Using index
  • 26. Benchmarks  Avg number of sec to run the query ● Without index: 3.743 ● Index on subscription: 0.435 ● Covering index: 0.012  Covering index ● 35x faster than index on subscription ● 300x faster than full table scan
  • 27. Even better for MyISAM  We can keep the covering index in memory mysql> SET GLOBAL customer_cache.key_buffer_size = 130000000; mysql> CACHE INDEX customer IN customer_cache; mysql> LOAD INDEX INTO CACHE customer;  Avg number of sec to run the query: 0.007  This step is specific to MyISAM !
  • 28. Even better for InnoDB  InnoDB secondary keys hold primary key values mysql> EXPLAIN SELECT name,id FROM customer WHERE subscription='2009-01-01' ORDER BY name *************************** 1. row *************************** possible_keys: idx_sub_name key: idx_sub_name Extra: Using where; Using index
  • 29. Another (harder) case study  Same table : customer  List people who subscribed on 2009-01-01 AND whose name ends up with xx ?  SELECT * FROM customer WHERE subscription='2009-01-01' AND name LIKE '%xx'  Let's add an index on (subscription,name) ...
  • 30. Another (harder) case study mysql> EXPLAIN SELECT * FROM customer WHERE subscription='2009-01-01' AND name LIKE '%xx' *************************** 1. row *************************** ... key: idx_sub_name key_len: 3 ref: const rows: 500272 Extra: Using where  The index is not covering anymore
  • 31. Query rewriting - Indexing  Rewriting the query SELECT * FROM customer INNER JOIN ( SELECT id FROM customer WHERE subscription='2009-01-01' AND name LIKE '%xx' ) AS t USING(id)  Adding an index ALTER TABLE customer ADD INDEX idx_sub_name_id (subscription,name,id)
  • 32. Running EXPLAIN *************************** 1. row *************************** select_type: PRIMARY table: <derived2> *************************** 2. row *************************** select_type: PRIMARY table: customer *************************** 3. row *************************** select_type: DERIVED table: customer key: idx_sub_name_id Extra: Using where; Using index
  • 33. Efficiency of the optimization  Beware of the subquery  10 subs./3 names with %xx ● Original query: 0.000 s ● Rewritten query: 0.000 s  300,000 subs./500 names with %xx ● Original query: 1.284 s ● Rewritten query: 0.553 s  Many intermediate situations  Always benchmark !
  • 34. InnoDB ?  The index on (subscription,name) is already covering for the subquery  Your work is easier: just rewrite the query if need be  But you still need to benchmark