问题描述
恐怕此功能可能尚不存在。我想使用一个跨越DB2中多个表的索引。我知道Oracle和sql Server实现了它们(带有或多或少的选项),而Postresql似乎还没有实现它们。
对于某些特定查询,多表索引可能非常有益。
作为参考,以下是Oracle和sql Server的多表索引示例:
Oracle示例
Oracle可以创建位图连接索引,如下所示:
create table dealer (
id int primary key not null,city varchar2(20) not null
);
create table car (
id int primary key not null,brand varchar2(20),price int,dealer_id int references dealer (id)
);
create bitmap index bix1 on car (d.city,c.brand)
from car c,dealer d
where d.id = c.dealer_id;
select avg(c.price)
from dealer d
join car c on c.dealer_id = d.id
where d.city = 'Chicago' and c.brand = 'Buick';
sql Server示例
sql Server可以创建索引视图:
create table dealer (
id int primary key not null,city varchar(20) not null
);
create table car (
id int primary key not null,brand varchar(20),dealer_id int references dealer (id)
);
create view v with schemabinding as
select d.city,c.brand,c.price,c.dealer_id
from dbo.dealer d
join dbo.car c on c.dealer_id = d.id;
create unique clustered index uix1 on v (city,brand,price);
select avg(c.price)
from dealer d
join car c on c.dealer_id = d.id
where d.city = 'Chicago' and c.brand = 'Buick';
DB2中有类似的东西吗?
解决方法
Db2(适用于Linux,UNIX和Windows)支持indexes on tables,即,您只能为单个表建立索引。
表可以是可以基于视图的MQT(物化查询表)。这不同于直接索引多个表。
,是的,就像在SQL Server中一样,您可以在跨越多个表的视图上创建索引。
注意-您必须以特殊方式设置视图(称为物化查询表),以使其正常工作。