覆盖索引

来源:互联网 发布:金义网络经济学院 编辑:程序博客网 时间:2024/04/28 09:45

覆盖索引(covering indexex):一个索引包含了(或覆盖了)满足查询结果的数据

 

如:

create tableorders
(
id int identity(1, 1),
customer_id int,
product_id int,
quantity int
primary key (id)
)
go
create table product
(
id int identity(1, 1),
product_name varchar(30)
)
go
--创建覆盖索引
create index idx on orders(id, customer_id, product_id, quantity)
go
insert into orders
select 2001, 1, 100
go
insert into product
select '机器'
go

set showplan_text on
go
select o.id, o.customer_id, o.product_id, o.quantity
from orders o
where o.id = 1
go
select o.id, p.product_name, o.quantity
from orders o
inner join product p on p.id = o.product_id
where o.id = 1
go
set showplan_text off

 

上列中因为访问orders使用所覆盖索引,所以不会再去访问orders的实际表,如果此时orders在其它进程中处于写入状态也不会对orders的覆盖索引访问产生阻塞,记住两点:

  1. 写操作时,不会对相关的索引加锁,而只会对相关的数据加锁。
  2. 读操作时,只会对其访问路径中发现和使用的对象加锁(例如索引、数据行等)。

相关细节参见:http://www.zxbc.cn/html/20080726/63700_6.html

 

原创粉丝点击