国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL復合索引性能比較

2024-07-24 12:37:48
字體:
來源:轉載
供稿:網友

復合索引是mysql中常用的一種數據優化形式了,下面我來給大家詳細介紹在mysql中復合索引的性能比較,各位朋友不防進入參考.

我們來看一些測試實例,代碼如下:

select * from dlog_user order by online_status, username

先看上面這個內聯的SQL語句,username是dlog_user表的主鍵,dlog_friend有一個由 username和friend_username組合而成的復合主鍵.

測試條件一:dlog_user 和 dlog_friend 兩個表除了主鍵外沒有建任何索引,對這條SQL語句EXPLAIN的結果是 dlog_user 做了全表查詢(type=ALL),Extra信息是use filesort

測試條件二:dlog_user 增加復合索引,代碼如下:

create index idx_online_status on dlog_user(username,online_status);

再次EXPLAIN SQL語句,還是全表查詢以及 use filesort.

測試條件三:

修改復合索引,將 username 和 online_status 順序調換一下,這回得到的結果是:type=index,Extra=空

索引起作用了.

測試條件四:

更改SQL語句如下:

  1. select a.* from dlog_user a inner join dlog_friend b on a.username=b.friend_username where b.username='ld' order by a.online_status desc,a.username 

也就是ORDER BY的時候,兩個字段的排序方式相反,這時不管怎么調整索引,執行此SQL語句都要進行全表查詢以及 user filesort.

結論:

1.ORDER BY 中的字段必須按照SQL語句中的順序來建索引.

2.ORDER BY 中的字段的排序順序必須一直,否則索引無效.

3.建了索引不一定就有效,用實際的SQL檢查一下.

下面用幾個例子對比查詢條件的不同對性能影響.

  1. create table test( a int, b int, c intKEY a(a,b,c) ); 
  2. --Vevb.com 
  3. 優: select * from test where a=10 and b>50 
  4. 差: select * from test where a50 
  5. 優: select * from test where order by a 
  6. 差: select * from test where order by b 
  7. 差: select * from test where order by c 
  8. 優: select * from test where a=10 order by a 
  9. 優: select * from test where a=10 order by b 
  10. 差: select * from test where a=10 order by c 
  11. 優: select * from test where a>10 order by a 
  12. 差: select * from test where a>10 order by b 
  13. 差: select * from test where a>10 order by c 
  14. 優: select * from test where a=10 and b=10 order by a 
  15. 優: select * from test where a=10 and b=10 order by b 
  16. 優: select * from test where a=10 and b=10 order by c 
  17. 優: select * from test where a=10 and b=10 order by a 
  18. 優: select * from test where a=10 and b>10 order by b 
  19. 差: select * from test where a=10 and b>10 order by c 

索引原則

1.索引越少越好

原因:主要在修改數據時,第個索引都要進行更新,降低寫速度.

2.最窄的字段放在鍵的左邊

3.避免file sort排序,臨時表和表掃描.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 七台河市| 武川县| 鲁山县| 娄烦县| 四子王旗| 天全县| 高碑店市| 双辽市| 普定县| 高唐县| 和林格尔县| 平潭县| 太和县| 通江县| 莲花县| 松桃| 江油市| 洞口县| 南京市| 阿拉尔市| 井研县| 台安县| 乌兰县| 松阳县| 广东省| 泽普县| 嘉黎县| 永和县| 息烽县| 北海市| 丽江市| 马关县| 那曲县| 德清县| 桃园县| 淮南市| 东宁县| 金乡县| 黄骅市| 嫩江县| 南和县|