{"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":"http:\/\/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<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"mailto:?subject=%5BShared%20Post%5D%20Practical%20MySQL%20Indexing%20guidelines%20by%20St%C3%83%C2%A9phane%20Combaudon&body=http%3A%2F%2Fwww.bytebot.net%2Fblog%2Farchives%2F2012%2F02%2F05%2Fpractical-mysql-indexing-guidelines-by-stephane-combaudon&share=email\" target=\"_blank\" title=\"Click to email a link to a friend\" data-email-share-error-title=\"Do you have email set up?\" data-email-share-error-text=\"If you&#039;re having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.\" data-email-share-nonce=\"745229a184\" data-email-share-track-url=\"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon?share=email\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-2286\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-2286\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-2286\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","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<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"mailto:?subject=%5BShared%20Post%5D%20Practical%20MySQL%20Indexing%20guidelines%20by%20St%C3%83%C2%A9phane%20Combaudon&body=http%3A%2F%2Fwww.bytebot.net%2Fblog%2Farchives%2F2012%2F02%2F05%2Fpractical-mysql-indexing-guidelines-by-stephane-combaudon&share=email\" target=\"_blank\" title=\"Click to email a link to a friend\" data-email-share-error-title=\"Do you have email set up?\" data-email-share-error-text=\"If you&#039;re having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.\" data-email-share-nonce=\"745229a184\" data-email-share-track-url=\"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon?share=email\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-2286\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-2286\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-2286\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/practical-mysql-indexing-guidelines-by-stephane-combaudon?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[1064,23],"tags":[1332,1347],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4vJD-AS","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2250,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/sphinx-user-stories-by-stephane-varoqui","url_meta":{"origin":2286,"position":0},"title":"Sphinx user stories by St\u00c3\u00a9phane Varoqui","date":"5\/2\/2012","format":false,"excerpt":"Stephane Varoqui, Field Services SkySQL, Vlad Fedorkov, Director of PS, Sphinx Inc, Christophe Gesche, LAMP Expert, Delcampe, Herve Seignole, Web Architect, Groupe Pierre & Vacances Center Parcs - this is a big talk! Pros: Filtering takes place on attributes in separate tables. Rely on the optimizer choice. HASH JOIN can\u2026","rel":"","context":"In &quot;MariaDB&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":791,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/15\/mysql-full-text-search-by-alex-rubin","url_meta":{"origin":2286,"position":1},"title":"MySQL Full Text Search by Alex Rubin","date":"15\/4\/2008","format":false,"excerpt":"Download the PDF: http:\/\/www.mysqlfulltextsearch.com\/full_text.pdfDefault search by relevance, default sort is by relevanceBoolean search is also popular. cats AND dogs. No default sorting, so you need to order the results yourselfPhrase searchMySQL Full Text Index, only available with MyISAM, and it supports natural language and Boolean search. ft_min_word_len - 4 characters\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":706,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/01\/28\/morning-sessions-at-mysql-miniconf","url_meta":{"origin":2286,"position":2},"title":"Morning sessions at MySQL MiniConf","date":"28\/1\/2008","format":false,"excerpt":"Upcoming MySQL Features - Stewart SmithStewart's talk on Upcoming MySQL Features was sort of a roadmap of what one might expect to see in MySQL 5.1 and above - he touched on Falcon, online backup, batched key access, Maria, Proxy, Workbench, and some cluster changes. When he shares his slides,\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2298,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2012\/02\/05\/managing-mysql-with-percona-toolkit-by-frederic-descamps","url_meta":{"origin":2286,"position":3},"title":"Managing MySQL with Percona Toolkit by Fr\u00c3\u00a9d\u00c3\u00a9ric Descamps","date":"5\/2\/2012","format":false,"excerpt":"Fr\u00e9d\u00e9ric Descamps of Percona. Percona Toolkit is Maatkit & Aspersa combined. Opensource and the tools are very useful for a DBA. You need Perl, DBI, DBD::mysql, Term::ReadKey. Most tools are written in Perl, and whatever is in Bash is being re-written in Perl. There is also a tarball or RPM\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2832,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2013\/11\/30\/groonga-fulltext-search-library-for-cloud-web","url_meta":{"origin":2286,"position":4},"title":"groonga &#8211; fulltext search library for cloud &#038; web","date":"30\/11\/2013","format":false,"excerpt":"This is an incomplete fragment from 2011. Figure its worth publishing this now, considering MariaDB is likely to get groonga in the near future. The groonga team have released MariaDB 10.0.6 binaries as well. This is all part of the mroonga\u00a0project. These were my quick notes from the groonga talk\u2026","rel":"","context":"In &quot;MariaDB&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":537,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2007\/04\/26\/mysql-at-google","url_meta":{"origin":2286,"position":5},"title":"MySQL at Google","date":"26\/4\/2007","format":false,"excerpt":"MySQL: The Real Grid Database, Mark Callaghan, Chip Turner A tremendous amount of work also done by Wei Li and Gene Pang. Google has a large MySQL deployment, and they enhance it as needed. MySQL@Google: too many queries, transactions, data, and rapid growth. Real workload with OLTP and reporting. Workload\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/2286"}],"collection":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/comments?post=2286"}],"version-history":[{"count":2,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/2286\/revisions"}],"predecessor-version":[{"id":2288,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/2286\/revisions\/2288"}],"wp:attachment":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/media?parent=2286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/categories?post=2286"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/tags?post=2286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}