Are You Getting the Best of your MySQL Indexes

578 views
466 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
578
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Are You Getting the Best of your MySQL Indexes

  1. 1. Are You Getting the Best Out of Your MySQL Indexes? http://bit.ly/mysqlindex Sheeri Cabral Senior DB Admin/Architect, Mozilla @sheeri www.sheeri.com Northeast PHP 2012
  2. 2. What is an index?
  3. 3. KEY vs. INDEX KEY = KEY CONSTRAINT PRIMARY, UNIQUE, FOREIGN Everything else is an “implementation detail” The plural of INDEX is....?
  4. 4. http://www.sheldoncomics.com/strips/sd120626.png
  5. 5. More Lingo Simple (last_name) Composite (last_name,first_name)
  6. 6. Data Structures B-TREE for InnoDB, MyISAM Can be HASH for MEMORY
  7. 7. B-tree (Image from Wikipedia)
  8. 8. 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)
  9. 9. 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)
  10. 10. 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)
  11. 11. 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)
  12. 12. MySQL Ignores Indexes For... Queries with multiple WHERE clauses not all using the same index joined by AND For example, imagine a test table, with an index on (last_name,first_name)
  13. 13. 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'; SELECT * FROM test WHERE last_name='Cabral' AND (first_name='Tony' OR first_name='Antonio');
  14. 14. 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';
  15. 15. MySQL Ignores Indexes For... “Too much” data, just do a full table scan 6 lookups vs. walking the 10-node tree (Image from Wikipedia) (Image from Wikipedia)
  16. 16. “Too Much” Data “Too much” is about 15-25% How is that calculated?
  17. 17. 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
  18. 18. 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
  19. 19. Composite Index Like a sorted array Can be ASC or DESC But not ASC,DESC or DESC, ASC (Image from Wikipedia)
  20. 20. 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-safe operator is <=> Remember the “too much data” feature/problem
  21. 21. NULL and Value Groups innodb_stats_method, myisam_stats_method nulls_equal (default) nulls_unequal nulls_ignored
  22. 22. PRIMARY Keys Row identifiers Cannot be NULL InnoDB orders on disk in PRIMARY KEY order
  23. 23. UNIQUE Keys Row identifiers Can be NULL Both PRIMARY/UNIQUE can be composite index
  24. 24. FOREIGN Keys Parent/child relationship customer->id vs. payment->customer_id Cascading update/deletes
  25. 25. Numeric vs. Human-readable customer_id status_id status_id status 121 1 1 free 122 2 2 paid 125 1 3 disabled customer_id status status 121 free free 122 paid paid 125 free disabled
  26. 26. Prefix Indexing For strings Up to 767 bytes on InnoDB, 1000 on MyISAM Beware of charset!
  27. 27. Composite Indexes Index on (last_name,first_name,middle_name) Functions as: (last_name,first_name,middle_name) (last_name,first_name) (last_name)
  28. 28. FULLTEXT Indexing No prefix indexing Only for CHAR, VARCHAR, TEXT No prefix indexing MyISAM only
  29. 29. 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
  30. 30. Questions? Comments? OurSQL Podcast www.oursql.com MySQL Administrator's Bible - tinyurl.com/mysqlbible kimtag.com/mysql planet.mysql.com
  31. 31. Are You Getting the Best Out of Your MySQL Indexes? http://bit.ly/mysqlindex Sheeri Cabral Senior DB Admin/Architect, Mozilla @sheeri www.sheeri.com Northeast PHP 2012
  32. 32. What is an index?
  33. 33. KEY vs. INDEX KEY = KEY CONSTRAINT PRIMARY, UNIQUE, FOREIGN Everything else is an “implementation detail” The plural of INDEX is....?
  34. 34. http://www.sheldoncomics.com/strips/sd120626.png
  35. 35. More Lingo Simple (last_name) Composite (last_name,first_name)
  36. 36. Data Structures B-TREE for InnoDB, MyISAM Can be HASH for MEMORY
  37. 37. B-tree (Image from Wikipedia)
  38. 38. 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)
  39. 39. 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)
  40. 40. 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)
  41. 41. 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)
  42. 42. MySQL Ignores Indexes For... Queries with multiple WHERE clauses not all using the same index joined by AND For example, imagine a test table, with an index on (last_name,first_name)
  43. 43. 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'; SELECT * FROM test WHERE last_name='Cabral' AND (first_name='Tony' OR first_name='Antonio');
  44. 44. 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';
  45. 45. MySQL Ignores Indexes For... “Too much” data, just do a full table scan 6 lookups vs. walking the 10-node tree (Image from Wikipedia) (Image from Wikipedia)
  46. 46. “Too Much” Data “Too much” is about 15-25% How is that calculated?
  47. 47. 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
  48. 48. 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
  49. 49. Composite Index Like a sorted array Can be ASC or DESC But not ASC,DESC or DESC, ASC (Image from Wikipedia)
  50. 50. 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-safe operator is <=> Remember the “too much data” feature/problem
  51. 51. NULL and Value Groups innodb_stats_method, myisam_stats_method nulls_equal (default) nulls_unequal nulls_ignored
  52. 52. PRIMARY Keys Row identifiers Cannot be NULL InnoDB orders on disk in PRIMARY KEY order
  53. 53. UNIQUE Keys Row identifiers Can be NULL Both PRIMARY/UNIQUE can be composite index
  54. 54. FOREIGN Keys Parent/child relationship customer->id vs. payment->customer_id Cascading update/deletes
  55. 55. Numeric vs. Human-readable customer_id status_id status_id status 121 1 1 122 2 2 paid 125 1 3 disabled customer_id status free status 121 free free 122 paid paid 125 free disabled
  56. 56. Prefix Indexing For strings Up to 767 bytes on InnoDB, 1000 on MyISAM Beware of charset!
  57. 57. Composite Indexes Index on (last_name,first_name,middle_name) Functions as: (last_name,first_name,middle_name) (last_name,first_name) (last_name)
  58. 58. FULLTEXT Indexing No prefix indexing Only for CHAR, VARCHAR, TEXT No prefix indexing MyISAM only
  59. 59. 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
  60. 60. Questions? Comments? OurSQL Podcast www.oursql.com MySQL Administrator's Bible - tinyurl.com/mysqlbible kimtag.com/mysql planet.mysql.com

×