測試複合索引在oracle、sql server 、mysql各種情況下是否使用索引

   今天沒有事驗證一下複合索引在不同的數據庫中的使用情況,僅是測試而已,以一個例子測試,結果如下

    首先在oracle,sqlserver, mysql建立表testpayorderinfo  表結構一樣,然後插入20w數據

    分別創建複合索引(testpayorderid,connid,customerid的複合索引)

    以複合索引中的不同條件字段的組合作爲條件進行測試

    說明:

    testpayorderid   對應a

    connid                對應b

    customerid       對應c

測試結果如下:

sql server:(版本sql server 2008)

select * from testpayorderinfo where testpayorderid=194027   --a 走
select * from testpayorderinfo where connid=257277          --b 走
select * from testpayorderinfo where customerid=1304349     --c 走
select * from testpayorderinfo where testpayorderid=194027 and connid=257277 --ab 走
select * from testpayorderinfo where testpayorderid=194027 and customerid=1304349 --ac 走
select * from testpayorderinfo where testpayorderid=194027 and connid=257277 and customerid=1304349  --abc 走
select * from testpayorderinfo where  connid=257277 and customerid=1304349  --bc 走
select testpayorderid,connid,customerid from testpayorderinfo where connid=257277          --b 走
select testpayorderid,connid,customerid from testpayorderinfo where customerid=1304349     --c 走
select testpayorderid,connid,customerid from testpayorderinfo where  connid=257277 and customerid=1304349  --bc 走


oracle:(oracle Release 12.1.0.2.0 )
create index ix_com_acc  on testpayorderinfo(testpayorderid,connid,customerid)
select * from testpayorderinfo where testpayorderid=194027   --a 走
select * from testpayorderinfo where connid=257277          --b 不走
select * from testpayorderinfo where customerid=1304349     --c 不走
select * from testpayorderinfo where testpayorderid=194027 and connid=257277 --ab 走
select * from testpayorderinfo where testpayorderid=194027 and customerid=1304349 --ac 走
select * from testpayorderinfo where testpayorderid=194027 and connid=257277 and customerid=1304349  --abc 走
select * from testpayorderinfo where  connid=257277 and customerid=1304349  --bc 不走

select testpayorderid,connid,customerid from testpayorderinfo where connid=257277          --b 走
select testpayorderid,connid,customerid from testpayorderinfo where customerid=1304349     --c 走
select testpayorderid,connid,customerid from testpayorderinfo where  connid=257277 and customerid=1304349  --bc 走

mysql :(Server version: 5.5.47-log MySQL Community Server (GPL))
select * from testpayorderinfo where testpayorderid=194027   --a 走
select * from testpayorderinfo where connid=257277          --b 不走
select * from testpayorderinfo where customerid=1304349     --c 不走
select * from testpayorderinfo where testpayorderid=194027 and connid=257277 --ab 走
select * from testpayorderinfo where testpayorderid=194027 and customerid=1304349 --ac 走
select * from testpayorderinfo where testpayorderid=194027 and connid=257277 and customerid=1304349  --abc 走
select * from testpayorderinfo where connid=257277 and customerid=1304349  --bc 不走


select testpayorderid,connid,customerid from testpayorderinfo where connid=257277          --b 走
select testpayorderid,connid,customerid from testpayorderinfo where customerid=1304349     --c 走
select testpayorderid,connid,customerid from testpayorderinfo where  connid=257277 and customerid=1304349  --bc 走

測試結果:

oracle和mysql 走的索引是一樣的,sql server 只要是複合索引中的任何一個字段都走索引

發佈了233 篇原創文章 · 獲贊 37 · 訪問量 88萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章