SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
Syed Zaid Irshad
 

Was ist angesagt? (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
 

Andere mochten auch

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
Fuenteovejuna
 
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
amansk
 
7. Relational Database Design in DBMS
7. Relational Database Design in DBMS7. Relational Database Design in DBMS
7. Relational Database Design in DBMS
koolkampus
 

Andere mochten auch (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
 

Ähnlich wie Covering indexes

Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with Explain
MYXPLAIN
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 
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 modificada
Titiushko Jazz
 
Clase 12 manejo indices modificada
Clase 12 manejo indices   modificadaClase 12 manejo indices   modificada
Clase 12 manejo indices modificada
Titiushko Jazz
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
paulguerin
 

Ähnlich wie 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
 

Mehr von MYXPLAIN

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
MYXPLAIN
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, Really
MYXPLAIN
 
MySQL 5.6 Performance
MySQL 5.6 PerformanceMySQL 5.6 Performance
MySQL 5.6 Performance
MYXPLAIN
 
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
MYXPLAIN
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query Optimization
MYXPLAIN
 
Tools and Techniques for Index Design
Tools and Techniques for Index DesignTools and Techniques for Index Design
Tools and Techniques for Index Design
MYXPLAIN
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
MYXPLAIN
 
The Power of MySQL Explain
The Power of MySQL ExplainThe Power of MySQL Explain
The Power of MySQL Explain
MYXPLAIN
 
Improving Performance with Better Indexes
Improving Performance with Better IndexesImproving Performance with Better Indexes
Improving Performance with Better Indexes
MYXPLAIN
 
Explaining the MySQL Explain
Explaining the MySQL ExplainExplaining the MySQL Explain
Explaining the MySQL Explain
MYXPLAIN
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
MYXPLAIN
 
Advanced query optimization
Advanced query optimizationAdvanced query optimization
Advanced query optimization
MYXPLAIN
 

Mehr von 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
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

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