{"id":2286,"date":"2012-02-05T08:01:06","date_gmt":"2012-02-05T13:01:06","guid":{"rendered":"http:\/\/www.bytebot.net\/blog\/?p=2286"},"modified":"2012-02-05T08:01:12","modified_gmt":"2012-02-05T13:01:12","slug":"practical-mysql-indexing-guidelines-by-stephane-combaudon","status":"publish","type":"post","link":"https:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon","title":{"rendered":"Practical MySQL Indexing guidelines by St\u00c3\u00a9phane Combaudon"},"content":{"rendered":"<p>St\u00e9phane Combaudon of Dailymotion.<\/p>\n<p>Index: separate data structure to speed up SELECTs. Think of index in a book. In MySQL, key=index. Consider that indexes are trees.<\/p>\n<p>InnoDB&#8217;s clustered index &#8211; data is stored with the Primary Key (PK) so PK lookups are fast. Secondary keys hold the PK values. Designing InnoDB PK&#8217;s with care is critical for performance.<\/p>\n<p>An index can filter and\/or sort values. An index can contain all the fields needed for the query you don&#8217;t need to go to the table (a covering index).<\/p>\n<p>MySQL only uses 1 index per table per query (not 100% true &#8211; OR clauses), so think of a composite index when you can. Can&#8217;t index TEXT fields (use a prefix). Same for BLOBs and long VARCHARs.<\/p>\n<p>Indexes: speed up queries, increases the size of your dataset, slows down writes. How big is the write slowdown? Simple test by Stephane, for in-memory workloads he says adding 2 keys makes performance 2x worse; for on-disk workloads he says its 40x worse. Never neglect the slowdown of your writes when you have an index. There is a graph in the slidedeck.<\/p>\n<p>What is a bad index? Unused indexes. Redundant indexes. Duplicate indexes.<\/p>\n<p>Indexing is not an exact science, but guessing is probably not the best way to design indexes. Always check your assumptions &#8211; EXPLAIN does not tell you everything, time your queries with different index combinations, SHOW PROFILES is often valuable. Slow query log is a good place to start.<\/p>\n<p>Many slides with examples, so I hope Stephane posts the deck soon. If possible, try to sort &amp; filter (an index is not always the best for sorting).<\/p>\n<p>InnoDB&#8217;s clustered index is always covering. SELECT by PK is the fastest access with InnoDB.<\/p>\n<p>An index can give you 3 benefits: filtering, sorting, covering.<\/p>\n<p>See <a href=\"http:\/\/kb.askmonty.org\/en\/user-statistics\">Userstats v2 <\/a>&#8211; you need Percona Server or MariaDB 5.2+. See also pt-duplicate-key-checker to find redundant indexes easily. See also pt-index-usage to help answer questions not covered by userstats.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>St\u00e9phane Combaudon of Dailymotion. Index: separate data structure to speed up SELECTs. Think of index in a book. In MySQL, key=index. Consider that indexes are trees. InnoDB&#8217;s clustered index &#8211; data is stored with the Primary Key (PK) so PK lookups are fast. Secondary keys hold the PK values. Designing InnoDB PK&#8217;s with care is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1064,23],"tags":[1332,1347],"class_list":["post-2286","post","type-post","status-publish","format-standard","hentry","category-mariadb-databases","category-mysql","tag-fosdem","tag-stephane-combaudon"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p4vJD-AS","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"amp_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/2286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/comments?post=2286"}],"version-history":[{"count":2,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/2286\/revisions"}],"predecessor-version":[{"id":2288,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/2286\/revisions\/2288"}],"wp:attachment":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/media?parent=2286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/categories?post=2286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/tags?post=2286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}