Are You Getting the Best Out of Your MySQL Indexes?

Slides:



Advertisements
Similar presentations
Index Dennis Shasha and Philippe Bonnet, 2013.
Advertisements

CS 245Notes 71 CS 245: Database System Principles Notes 7: Query Optimization Hector Garcia-Molina.
Hash Indexes: Chap. 11 CS634 Lecture 6, Feb
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
David Konopnicki Choosing Access Path ä The basic methods. ä The access paths and when they are available. ä How the optimizer chooses among the.
Access Lecture 1 Database Overview and Creating Tables Create an Employee Table.
Sarah Sproehnle Cloudera, Inc
Indexing - revisited CS 186, Fall 2012 R & G Chapter 8.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Chapter 3 Single-Table Queries
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
SQL – Structured Query Language CIS 324 – Chapter 5.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Nikolay Kostov Telerik Corporation
Proactively Optimizing Queries with EXPLAIN and mk-query-digest Presented by: Sheeri K. Cabral Database Operations Manager MySQL Sunday at Oracle OpenWorld.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Chapter 5 Index and Clustering
CPSC 404, Laks V.S. Lakshmanan1 Overview of Query Evaluation Chapter 12 Ramakrishnan & Gehrke (Sections )
Relational Databases and MySQL. Relational Databases Relational databases model data by storing rows and columns in tables. The power of the relational.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
Query Optimization Cases. D. ChristozovINF 280 DB Systems Query Optimization: Cases 2 Executable Block 1 Algorithm using Indices (if available) Temporary.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Bend SQL to Your Will With EXPLAIN Ligaya Turmelle MySQL Support Engineer
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Fundamentals of DBMS Notes-1.
Database System Architecture and Implementation
Module 11: File Structure
CPS216: Data-intensive Computing Systems
INLS 623– Database Systems II– File Structures, Indexing, and Hashing
Indexing Structures for Files and Physical Database Design
Indexing Goals: Store large files Support multiple search keys
Optimizing MySQL Joins and Subqueries
SQL Implementation & Administration
Indices.
Indexing ? Why ? Need to locate the actual records on disk without having to read the entire table into memory.
Choosing Access Path The basic methods.
COMP 430 Intro. to Database Systems
Alejandro Álvarez on behalf of the FTS team
PostgreSQL Conference East 2009 The Art of Indexes Josh Williams
Translation of ER-diagram into Relational Schema
Examples of Physical Query Plan Alternatives
Physical Database Design and Referential Integrity
Physical Join Operators
Lecture 12 Lecture 12: Indexing.
Physical Database Design
Lecture 19: Data Storage and Indexes
SQL DATA CONSTRAINTS.
Chapters 15 and 16b: Query Optimization
Lesson Plan Instructional Objective Learning Objective
Database Systems (資料庫系統)
Database Design and Programming
CSE 544: Lecture 11 Storing Data, Indexes
2018, Spring Pusan National University Ki-Joune Li
Implementation of Relational Operations
Section 4 - Sorting/Functions
Introduction to Database Systems CSE 444 Lectures 19: Data Storage and Indexes May 16, 2008.
SQL Basics BCHB697.
A – Pre Join Indexes.
All about Indexes Gail Shaw.
Index Structures Chapter 13 of GUW September 16, 2019
Presentation transcript:

Are You Getting the Best Out of Your MySQL Indexes? Sheeri Cabral Senior DB Admin/Architect, Mozilla @sheeri www.sheeri.com Slides http://bit.ly/mysqlindex2

What is an index?

KEY vs. INDEX KEY = KEY CONSTRAINT PRIMARY, UNIQUE, FOREIGN Everything else is an “implementation detail” The plural of INDEX is....?

http://www.sheldoncomics.com/strips/sd120626.png

More Lingo Simple (last_name) Composite (last_name,first_name)

Data Structures B-TREE for InnoDB, MyISAM Can be HASH for MEMORY

(Image from Wikipedia) B-tree (Image from Wikipedia)

B-trees Are Excellent For... A range search - foo BETWEEN 5 AND 10 One equality match - foo=11 A few equality matches - foo IN (5,10,11) - How is it done? (Image from Wikipedia)

(Image from Wikipedia) Composite Indexes Index on (last_name,first_name) Used find words beginning with “g” (last_name) Not used to find words ending with “g” (first_name) OK to have (last_name,first_name) and (first_name) Like a dictionary index (Image from Wikipedia)

(Image from Wikipedia) MySQL Uses Indexes For... Matching a WHERE clause Eliminating rows Resolving MIN() or MAX() values Eliminating sorting Helping with grouping Everything – Covering index (Image from Wikipedia)

MySQL Ignores Indexes For... Functions DATE(ts_col)='2012_08_11' JOINs if the fields are not similar date_col='2012_08_11 00:00:00' (Image from Wikipedia)

MySQL Ignores Indexes For... Queries with multiple WHERE clauses not all using the same index joined by OR For example, imagine a test table, with an index on (last_name,first_name)

test,(last_name,first_name) Queries that use the index: SELECT * FROM test WHERE last_name='Cabral'; SELECT * FROM test WHERE last_name='Cabral' AND first_name='Sheeri'; WHERE last_name='Cabral' AND (first_name='Tony' OR first_name='Antonio');

test,(last_name,first_name) Queries that DO NOT use the index: SELECT * FROM test WHERE first_name='Sheeri'; SELECT * FROM test WHERE last_name='Cabral' OR first_name='Sheeri';

Composite Indexes Index on (last_name,first_name,middle_name) Functions as: (last_name,first_name,middle_name) (last_name,first_name) (last_name)

MySQL Ignores Indexes For... “Too much” data, just do a full table scan (7,9,12) 6 lookups vs. walking the 10-node tree (Image from Wikipedia) (Image from Wikipedia)

“Too Much” Data “Too much” is about 15-25% How is that calculated?

Metadata! Exact in MyISAM (writes are table-locking) Approximate in InnoDB “value group” Average value group size Used for approx rows for rows read, joins

Average Value Group Size Body parts: 2 eyes, 10 fingers Average value group size = 6 Not perfect optimization for either eyes or fingers Estimate is longer than reality for eyes Estimate is shorter than reality for fingers Remember the “too much data” feature/problem

(Image from Wikipedia) Composite Index Like a sorted array Can be ASC or DESC But not ASC,DESC or DESC, ASC (Image from Wikipedia)

NULL and Equality NULL = x is not true for ANY value of x NULL = NULL is not true If a referenced value in an equality is NULL MySQL immediately returns false

NULL and Equality NULL-safe operator is <=> No NULL-safe inequality operator !(foo <=> bar) Remember the “too much data” feature/problem

NULL and Value Groups Options - innodb_stats_method, myisam_stats_method nulls_equal (default) nulls_unequal nulls_ignored

PRIMARY Keys Row identifiers Cannot be NULL InnoDB orders on disk in PRIMARY KEY order

UNIQUE Keys Row identifiers Can be NULL Both PRIMARY/UNIQUE can be composite index

FOREIGN Keys Parent/child relationship e.g. payment->customer_id Cascading update/deletes

Numeric vs. Human-readable customer_id status_id 121 1 122 2 125 status_id status 1 free 2 paid 3 disabled customer_id status 121 free 122 paid 125 status free paid disabled

Prefix Indexing For strings Up to 767 bytes on InnoDB, 1000 on MyISAM Beware of charset!

FULLTEXT Indexing No prefix indexing Only for CHAR, VARCHAR, TEXT MyISAM only until MySQL 5.6

GROUP BY and Sorting By default, GROUP BY also sorts May cause 'filesort' in EXPLAIN ORDER BY NULL If you do not care about the return order

Knowing All This... Use EXPLAIN If you are not getting the index you expect Check your expectations Or file a bug: http://bugs.mysql.com

http://bit.ly/explainvideo http://bit.ly/explainslides How Do I Use EXPLAIN? http://bit.ly/explainvideo http://bit.ly/explainslides

index_merge On surface, looks good Use more than one index Better in MySQL 5.6

index_merge before 5.6 Indicates you could make a better index Index merge not used when it should have been e.g. if range scan was possible May have merged more indexes than necessary

Questions? Comments? OurSQL Podcast www.oursql.com MySQL Administrator's Bible - tinyurl.com/mysqlbible planet.mysql.com mysqlmarinate.com