最近有人問到這個問題,以前也一直沒有深究聯合索引具體使用邏輯,查閱多篇文章,並通過測試,得出一些結論數據庫
測試環境:SQL Server 2008 R2測試
測試結果與MySql聯合索引查詢機制相似,能夠認爲MySql是同樣的原理spa
====================================================code
聯合索引概念:當系統中某幾個字段常常要作查詢,而且數據量較大,達到百萬級別,可多個字段建成索引blog
使用規則:索引
1.最 左 原則,根據索引字段,由左往右依次and(where字段很重要,從左往右)字符串
2.Or 不會使用聯合索引get
3.where語句中查詢字段包含所有索引字段,字段順序無關,可隨意前後it
4.數據量較少時,通常不會使用索引,數據庫自己機制會自動判斷是否使用索引table
=====================================================
測試腳本(部分借鑑其餘做者的腳本):
/*建立測試數據表*/ create table MyTestTable ( id varchar(10)not null, parent varchar(40) not null, addtime datetime default(getdate()), intcolumn int default(10), bitcolumn bit default(1) ) go /*添加萬條隨機字符串測試數據耗時分鐘*/ declare @count int=3557643 declare @i int =0 declare @id varchar(10),@parent varchar(40) while(@i<@count) begin select @id=left(newid(),10) if(@i % 20=0) begin select @parent=left(newid(),40) end insert MyTestTable(id,parent) values(@id,@parent) select @i=@i+1 end go
/×未建索引查詢測試×/
declare @beginTime datetime =getdate() declare @elapsedSecond int =0 select * from MyTestTable where parent='F92D6A9D-4E9E-4980-8B46-8AD938CEDCB4' and id='FD3687F4-1' select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE()) print '未創建索引時查找數據消耗微秒數' print @elapsedSecond select @beginTime=GETDATE() select * from MyTestTable where parent='F535C18F-BD48-4D45-88DF-9653BB9B422D' select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE()) print '未創建索引時查找第二列數據消耗微秒數' print @elapsedSecond
/*創建索引*/ alter table MyTestTable add constraint PK_id_parent primary key(id asc,parent asc) /*創建索引後的查詢*/ declare @beginTime datetime =getdate() declare @elapsedSecond int =0 select * from MyTestTable where parent='F92D6A9D-4E9E-4980-8B46-8AD938CEDCB4' and id='FD3687F4-1' select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE()) print '創建索引時查找數據消耗微秒數' print @elapsedSecond select @beginTime=GETDATE() select * from MyTestTable where parent='F92D6A9D-4E9E-4980-8B46-8AD938CEDCB4' select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE()) print '創建索引後查找第二列數據消耗微秒數' print @elapsedSecond
/*索引使用測試結論*/ select * from MyTestTable where id='FD3687F4-1' --用索引 select * from MyTestTable where id='FD3687F4-1' and parent='F92D6A9D-4E9E-4980-8B46-8AD938CEDCB4' and intcolumn>0 --用索引 select * from MyTestTable where id='FD3687F4-1' and intcolumn>0 and parent='F92D6A9D-4E9E-4980-8B46-8AD938CEDCB4' --用索引 select * from MyTestTable where id='FD3687F4-1' and intcolumn>0 --用索引 select * from MyTestTable where parent='F92D6A9D-4E9E-4980-8B46-8AD938CEDCB4' and id='FD3687F4-1' --用索引 select * from MyTestTable where parent='F92D6A9D-4E9E-4980-8B46-8AD938CEDCB4' and intcolumn>0 --不用索引 select * from MyTestTable where parent='F92D6A9D-4E9E-4980-8B46-8AD938CEDCB4' or id='FD3687F4-1' --不用索引
若有問題歡迎留言交流!