[{"data":1,"prerenderedAt":295},["ShallowReactive",2],{"post:cant-add-index-to-view":3},{"id":4,"title":5,"body":6,"date":283,"description":12,"draft":284,"extension":285,"meta":286,"navigation":287,"path":288,"seo":289,"stem":290,"tags":291,"__hash__":294},"posts\u002Fposts\u002Fcant-add-index-to-view.md","TIL: You Can't Add An Index to a MySQL View",{"type":7,"value":8,"toc":281},"minimark",[9,13,16,216,219,222,249,274,277],[10,11,12],"p",{},"I hadn't realized this until today: MySQL views can't have indexes!",[10,14,15],{},"So I have this somewhat massive view which groups five separate tables for MLS properties. Something like:",[17,18,23],"pre",{"className":19,"code":20,"language":21,"meta":22,"style":22},"language-sql shiki shiki-themes github-light github-dark","CREATE VIEW view_listings AS \nSELECT \n    `LISTINGID`, ...\nFROM (\n        SELECT `LISTINGID`, ...\n        FROM `residentialproperty`\n    UNION\n        SELECT `LISTINGID`, ...\n        FROM `property`\n    UNION\n        SELECT `LISTINGID`, ...\n        FROM `multifamily`\n    UNION\n        SELECT `LISTINGID`, ...\n        FROM `mobilehome`\n    UNION\n        SELECT `LISTINGID`, ...\n        FROM `lotsandland`\n    UNION\n        SELECT `LISTINGID`, ...\n        FROM `commercial`\n) `listings`\n","sql","",[24,25,26,49,57,67,76,87,96,102,111,119,124,133,141,146,155,163,168,177,185,190,199,207],"code",{"__ignoreMap":22},[27,28,31,35,38,42,45],"span",{"class":29,"line":30},"line",1,[27,32,34],{"class":33},"szBVR","CREATE",[27,36,37],{"class":33}," VIEW",[27,39,41],{"class":40},"sScJk"," view_listings",[27,43,44],{"class":33}," AS",[27,46,48],{"class":47},"sVt8B"," \n",[27,50,52,55],{"class":29,"line":51},2,[27,53,54],{"class":33},"SELECT",[27,56,48],{"class":47},[27,58,60,64],{"class":29,"line":59},3,[27,61,63],{"class":62},"sZZnC","    `LISTINGID`",[27,65,66],{"class":47},", ...\n",[27,68,70,73],{"class":29,"line":69},4,[27,71,72],{"class":33},"FROM",[27,74,75],{"class":47}," (\n",[27,77,79,82,85],{"class":29,"line":78},5,[27,80,81],{"class":33},"        SELECT",[27,83,84],{"class":62}," `LISTINGID`",[27,86,66],{"class":47},[27,88,90,93],{"class":29,"line":89},6,[27,91,92],{"class":33},"        FROM",[27,94,95],{"class":62}," `residentialproperty`\n",[27,97,99],{"class":29,"line":98},7,[27,100,101],{"class":33},"    UNION\n",[27,103,105,107,109],{"class":29,"line":104},8,[27,106,81],{"class":33},[27,108,84],{"class":62},[27,110,66],{"class":47},[27,112,114,116],{"class":29,"line":113},9,[27,115,92],{"class":33},[27,117,118],{"class":62}," `property`\n",[27,120,122],{"class":29,"line":121},10,[27,123,101],{"class":33},[27,125,127,129,131],{"class":29,"line":126},11,[27,128,81],{"class":33},[27,130,84],{"class":62},[27,132,66],{"class":47},[27,134,136,138],{"class":29,"line":135},12,[27,137,92],{"class":33},[27,139,140],{"class":62}," `multifamily`\n",[27,142,144],{"class":29,"line":143},13,[27,145,101],{"class":33},[27,147,149,151,153],{"class":29,"line":148},14,[27,150,81],{"class":33},[27,152,84],{"class":62},[27,154,66],{"class":47},[27,156,158,160],{"class":29,"line":157},15,[27,159,92],{"class":33},[27,161,162],{"class":62}," `mobilehome`\n",[27,164,166],{"class":29,"line":165},16,[27,167,101],{"class":33},[27,169,171,173,175],{"class":29,"line":170},17,[27,172,81],{"class":33},[27,174,84],{"class":62},[27,176,66],{"class":47},[27,178,180,182],{"class":29,"line":179},18,[27,181,92],{"class":33},[27,183,184],{"class":62}," `lotsandland`\n",[27,186,188],{"class":29,"line":187},19,[27,189,101],{"class":33},[27,191,193,195,197],{"class":29,"line":192},20,[27,194,81],{"class":33},[27,196,84],{"class":62},[27,198,66],{"class":47},[27,200,202,204],{"class":29,"line":201},21,[27,203,92],{"class":33},[27,205,206],{"class":62}," `commercial`\n",[27,208,210,213],{"class":29,"line":209},22,[27,211,212],{"class":47},") ",[27,214,215],{"class":62},"`listings`\n",[10,217,218],{},"This works fine, more or less, except that it's pretty slow! Obviously, because it's querying five separate tables. Adding an index should greatly improve the search time for this view, right?",[10,220,221],{},"The \"obvious fix\", or so I thought, was to create an index on the listingid field.",[17,223,225],{"className":19,"code":224,"language":21,"meta":22,"style":22},"CREATE UNIQUE INDEX get_listing_by_id ON view_listings ( listingID ) COMMENT \"Index the view by id\";\n",[24,226,227],{"__ignoreMap":22},[27,228,229,231,234,237,240,243,246],{"class":29,"line":30},[27,230,34],{"class":33},[27,232,233],{"class":33}," UNIQUE INDEX",[27,235,236],{"class":40}," get_listing_by_id",[27,238,239],{"class":33}," ON",[27,241,242],{"class":47}," view_listings ( listingID ) COMMENT ",[27,244,245],{"class":62},"\"Index the view by id\"",[27,247,248],{"class":47},";\n",[10,250,251,252,255,256,267,268,273],{},"Unfortunately, here's what I got: ",[24,253,254],{},"'mydb.view_listings' is not BASE TABLE",". I feel like this should have been mentioned in the ",[257,258,262,263,266],"a",{"href":259,"rel":260},"https:\u002F\u002Fdev.mysql.com\u002Fdoc\u002Frefman\u002F8.0\u002Fen\u002Fcreate-index.html",[261],"nofollow","MySQL ",[24,264,265],{},"CREATE INDEX"," docs",", but whatever. A little more digging turned up ",[257,269,272],{"href":270,"rel":271},"https:\u002F\u002Fstackoverflow.com\u002Fa\u002F22545866",[261],"this better explanation of why MySQL views simply can't support indexing and so forth",".",[10,275,276],{},"Back to the drawing board on performance for that bad boy.",[278,279,280],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":22,"searchDepth":51,"depth":51,"links":282},[],"2018-05-31T14:53:28-04:00",false,"md",{},true,"\u002Fposts\u002Fcant-add-index-to-view",{"title":5,"description":12},"posts\u002Fcant-add-index-to-view",[292,293],"mysql","til","6PQEItd0QnV-kM834x3BVvUIOe-gNNlGtoA9Y0ZXjKI",1790471528170]