MySQL-技術專題-性能優化—索引篇

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"前言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 今天我們來講講如何優化MySQL的性能,主要從索引方面優化。下期文章講講"},{"type":"codeinline","content":[{"type":"text","text":"MySQL慢查詢日誌"}]},{"type":"text","text":",我們是依據慢查詢日誌來判斷哪條SQL語句有問題,然後在進行優化,敬請期待"},{"type":"codeinline","content":[{"type":"text","text":"MySQL慢查詢日誌篇"}]}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"建表"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/65/652c54cad31c38977a27e2b7cac41745.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"優化一:全部用到索引"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"介紹"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 建立的複合索引包含了幾個字段,查詢的時候最好能全部用到,而且嚴格按照索引順序,這樣查詢效率是最高的。(最理想情況,具體情況具體分析)"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"SQL 案例"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/62/62c5481faf31b3a9173fab8288e66778.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"優化二:最左前綴法則"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"介紹"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 如果建立的是複合索引,索引的順序要按照建立時的順序,即從左到右,如:a->b->c(和 B+樹的數據結構有關)"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"無效索引舉例"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"a->c:a 有效,c 無效"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"b->c:b、c 都無效"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"c:c 無效"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"SQL 案例"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2f/2f546e05d4eb6634485101dc28e55d8f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"優化三:不要對索引做下處理"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"以下用法會導致索引失效"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"計算,如:+、-、*、/、!=、<>、is null、is not null、or"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"函數,如:sum()、round()等等"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"手動/自動類型轉換,如:id = \"1\",本來是數字,給寫成字符串了"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"SQL 案例"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/26/26383fc029c545b210290bf8aa1d464f.webp","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"優化四:索引不要放在範圍查詢右邊"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"舉例"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":" 比如複合索引:a->b->c,當 where a=\"\" and b>10 and 3=\"\",這時候只能用到 a 和 b,c 用不到索引,因爲在範圍之後索引都失效(和 B+樹結構有關)"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"SQL 案例"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c2/c2bbcabdca2d3b1ba71ae5eef7e6ff30.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"優化五:減少 select * 的使用"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"使用覆蓋索引"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即:select 查詢字段和 where 中使用的索引字段一致。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"SQL 案例"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a6/a6c192fb92820af5f7a006d41fb692e9.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"優化六:like 模糊搜索"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"失效情況"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"like \"%張三%\""}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"like \"%張三\""}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"解決方案"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"使用複合索引,即 like 字段是 select 的查詢字段,如:select name from table where name like \"%張三%\""}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"使用 like \"張三%\""}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"SQL 案例"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"優化七:order by 優化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 當查詢語句中使用 order by 進行排序時,如果沒有使用索引進行排序,會出現 filesort 文件內排序,這種情況在數據量大或者併發高的時候,會有性能問題,需要優化。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"filesort 出現的情況舉例"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"order by 字段不是索引字段"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"order by 字段是索引字段,但是 select 中沒有使用覆蓋索引,如:"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"select * from staffs order by age asc;"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"order by 中同時存在 ASC 升序排序和 DESC 降序排序,如:"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"select a, b from staffs order by a desc, b asc;"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"order by 多個字段排序時,不是按照索引順序進行 order by,即不是按照最左前綴法則,如:"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"select a, b from staffs order by b asc, a asc;"}],"marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}]}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"索引層面解決方法"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"使用主鍵索引排序"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"按照最左前綴法則,並且使用覆蓋索引排序,多個字段排序時,保持排序方向一致"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"在 SQL 語句中強制指定使用某索引,force index(索引名字)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"不在數據庫中排序,在代碼層面排序"}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"order by 排序算法"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"雙路排序Mysql4.1 之前是使用雙路排序,字面的意思就是兩次掃描磁盤,最終得到數據,讀取行指針和 ORDER BY 列,對他們進行排序,然後掃描已經排好序的列表,按照列表中的值重新從列表中讀取對數據輸出。也就是從磁盤讀取排序字段,在 buffer 進行排序,再從磁盤讀取其他字段。"}]}]}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件的磁盤 IO 非常耗時的,所以在 Mysql4.1 之後,出現了第二種算法,就是單路排序。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"單路排序從磁盤讀取查詢需要的所有列,按照 orderby 列在 buffer 對它們進行排序,然後掃描排序後的列表進行輸出, 它的效率更快一些,避免了第二次讀取數據,並且把隨機 IO 變成順序 IO,但是它會使用更多的空間, 因爲它把每一行都保存在內存中了。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 當我們無可避免要使用排序時,索引層面沒法在優化的時候又該怎麼辦呢?儘可能讓 MySQL 選擇使用第二種單路算法來進行排序。這樣可以減少大量的隨機 IO 操作,很大幅度地提高排序工作的效率。下面看看單路排序優化需要注意的點"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"單路排序優化點"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"增大 max_length_for_sort_data"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"去掉不必要的返回字段,避免select *"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"增大 sort_buffer_size 參數設置"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是sort_buffer_size 不是越大越好:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Sort_Buffer_Size 是一個 connection 級參數,在每個 connection 第一次需要使用這個 buffer 的時候,一次性分配設置的內存。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"Sort_Buffer_Size 並不是越大越好,由於是 connection 級的參數,過大的設置和高併發可能會耗盡系統內存資源。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"據說 Sort_Buffer_Size 超過 2M 的時候,就會使用 mmap() 而不是 malloc() 來進行內存分配,導致效率降低。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"優化八:group by"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 其原理也是先排序後分組,其優化方式可參考order by。where高於having,能寫在where限定的條件就不要去"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong"}],"text":"having"},{"type":"text","text":"限定了。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章