{"id":90,"date":"2009-11-11T09:12:10","date_gmt":"2009-11-11T15:12:10","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=90"},"modified":"2011-11-28T09:28:07","modified_gmt":"2011-11-28T15:28:07","slug":"choosing-index-prefix-length","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=90","title":{"rendered":"Choosing index prefix length"},"content":{"rendered":"<p>It can be handy to index just part of a long character column. The strings might vary enough in just the first few characters to get the same cardinality out of a partial index as a full index would give, and the partial index takes up much less disk space and memory.<\/p>\n<p><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/mysql-indexes.html\">Indexes in The Manual<\/a><\/p>\n<p>But how long should that prefix be? Should it be the same cardinality as the original data? Within 10%?<\/p>\n<p>I dunno; somebody else can blog about that decision. I&#8217;m just interested in a query that finds the number which satisfies your requirements. <\/p>\n<p>I have a table called &#8216;numbers&#8217; with an int field &#8216;num&#8217; that just holds values 1 through a large number; for this case, it needs to be no larger than the length of the longest string in the table.<\/p>\n<p>Then:<\/p>\n<pre>SELECT\r\n  numbers.num AS prefixLength,\r\n  dt.originalCardinality,\r\n  (SELECT COUNT(DISTINCT LEFT(stringField, numbers.num)) FROM yourTable) AS prefixCardinality\r\nFROM\r\n  numbers\r\n  JOIN (SELECT COUNT(DISTINCT stringField) AS originalCardinality FROM yourTable) AS dt\r\nWHERE\r\n  (SELECT COUNT(DISTINCT LEFT(stringField, numbers.num)) FROM yourTable) >= .90 * originalCardinality\r\nORDER BY numbers.num\r\nLIMIT 1;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It can be handy to index just part of a long character column. The strings might vary enough in just the first few characters to get the same cardinality out of a partial index as a full index would give, and the partial index takes up much less disk space and memory. Indexes in The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_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}},"categories":[4],"tags":[],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-mysql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-1s","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":6,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions\/177"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}