svr*_*ist 11 sql oracle indexing
如果我有一张桌子
create table sv ( id integer, data text )
Run Code Online (Sandbox Code Playgroud)
和索引:
create index myindex_idx on sv (id,text)
Run Code Online (Sandbox Code Playgroud)
如果我进行查询,这仍然有用吗
select * from sv where id = 10
Run Code Online (Sandbox Code Playgroud)
我问的原因是我正在查看一组没有任何索引的表,并查看选择查询的不同组合.有些只使用一列其他有多个列.我是否需要为两个集合建立索引,或者是一个包罗万象的索引吗?我正在添加索引以便比全表扫描更快地查找.
示例(基于Matt Huggins的回答):
select * from table where col1 = 10
select * from table where col1 = 10 and col2=12
select * from table where col1 = 10 and col2=12 and col3 = 16
Run Code Online (Sandbox Code Playgroud)
可以全部由索引表(co1l1,col2,col3)覆盖但是
select * from table where col2=12
Run Code Online (Sandbox Code Playgroud)
还需要另一个指数吗?
Mat*_*ins 19
它应该是有用的,因为(id,text)的索引首先按id分别,然后分别是文本.
编辑:当我说它"有用"时,我的意思是它在查询速度/优化方面很有用.正如Sune Rievers指出的那样,并不意味着只有ID才能获得唯一的记录(除非您在表定义中将ID指定为唯一).
Oracle支持多种使用索引的方法,您应该从了解所有索引开始,所以请快速阅读:http://download.oracle.com/docs/cd/B19306_01/server.102/b14211/ optimops.htm#sthref973
select * from table where col2=12
如果前导列的基数非常低,则查询可以有效地利用索引跳过扫描,如果不是,则可以使用快速完整索引扫描.这些可能适用于运行报告,但是对于OLTP查询,您可能会更好地创建以col2作为前导列的索引.