{"id":1408,"date":"2012-10-05T12:25:44","date_gmt":"2012-10-05T19:25:44","guid":{"rendered":"http:\/\/xiehang.com\/blog\/?p=1408"},"modified":"2012-10-05T12:25:44","modified_gmt":"2012-10-05T19:25:44","slug":"now-i-know-why-mysql-refused-to-use-the-index","status":"publish","type":"post","link":"https:\/\/xiehang.com\/blog\/2012\/10\/05\/now-i-know-why-mysql-refused-to-use-the-index\/","title":{"rendered":"Now I know why MySQL refused to use the index"},"content":{"rendered":"

A book table like this:
\n
\n...
\nPRIMARY KEY (`bookid`),
\nKEY `authorid` (`authorid`)
\n...
\n<\/code>
\nand the query is something like this:
\n
\nSELECT * FROM book WHERE authorid = 123
\n<\/code>
\nbut MySQL refused to use authorid index, instead, it did a full table scan, how stupid!<\/p>\n

However, later on it turned to be how stupid I AM. The authorid column was, by whatever reason, defined as varchar instead of int, so MySQL decided not to use the index.<\/p>\n

Anyway, I was stupid, but MySQL is stubborn.<\/p>\n","protected":false},"excerpt":{"rendered":"

A book table like this: … PRIMARY KEY (`bookid`), KEY `authorid` (`authorid`) … and the query is something like this: SELECT * FROM book WHERE authorid = 123 but MySQL refused to use authorid index, instead, it did a full table scan, how stupid! However, later on it turned to be how stupid I AM. […]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[367,103,368],"_links":{"self":[{"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/posts\/1408"}],"collection":[{"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/comments?post=1408"}],"version-history":[{"count":2,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/posts\/1408\/revisions"}],"predecessor-version":[{"id":1410,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/posts\/1408\/revisions\/1410"}],"wp:attachment":[{"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/media?parent=1408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/categories?post=1408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xiehang.com\/blog\/wp-json\/wp\/v2\/tags?post=1408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}