MySQL-Indexing Best Practices (WEBINAR)
MySQL-Indexing Best Practices (WEBINAR)
Best Practices
• Understanding Indexing
• Setting up best indexes for your applications
• Working around common MySQL limitations
Indexing in the Nutshell
• BTREE Indexes
– Majority of indexes you deal in MySQL is this type
• RTREE Indexes
– MyISAM only, for GIS
• HASH Indexes
– MEMORY, NDB
• BITMAP Indexes
– Not Supported by MySQL
• FULLTEXT Indexes
– MyISAM, Innodb planned in MySQL 5.6
Family of BTREE like Indexes
Branch/Root Node
Less than 3
• Data Lookups
• Sorting
• Avoiding reading “data”
• Special Optimizations
Using Indexes for Data Lookups
• “Covering Index”
– Applies to index use for specific query, not type of
index.
• Reading Index ONLY and not accessing the “data”
• SELECT STATUS FROM ORDERS WHERE
CUSTOMER_ID=123
– KEY(CUSTOMER_ID,STATUS)
• Index is typically smaller than data
• Access is a lot more sequential
– Access through data pointers is often quite “random”
Min/Max Optimizations
• KEY (A,B)
• SELECT * FROM TBL WHERE A BETWEEN 2
AND 4 AND B=5
– Will only use first key part of the index
• SELECT * FROM TBL WHERE A IN (2,3,4) AND
B=5
– Will use both key parts
Trick #2: Adding Fake Filter
• KEY (GENDER,CITY)
• SELECT * FROM PEOPLE WHERE CITY=“NEW
YORK”
– Will not be able to use the index at all
• SELECT * FROM PEOPLE WHERE GENDER IN
(“M”,”F”) AND CITY=“NEW YORK”
– Will be able to use the index
• The trick works best with low selectivity columns.
– Gender, Status, Boolean Types etc
Trick #3: Unionizing Filesort
• KEY(A,B)
• SELECT * FROM TBL WHERE A IN (1,2) ORDER BY
B LIMIT 5;
– Will not be able to use index for SORTING
• (SELECT * FROM TBL WHERE A=1 ORDER BY B
LIMIT 5) UNION ALL (SELECT * FROM TBL WHERE
A=2 ORDER BY B LIMIT 5) ORDER BY B LIMIT 5;
– Will use the index for Sorting. “filesort” will be needed
only to sort over 10 rows.
Join us at Webinars
• [email protected]
• http://www.percona.com
• @percona at Twitter
• http://www.facebook.com/Percona