【发布时间】:2020-11-28 02:25:07
【问题描述】:
我已经搜索了一个星期了,恐怕这可能不存在 [还]。我想在 PostgreSQL 中使用跨越多个表的索引。 Oracle 和 SQL Server 似乎实现了它们(或多或少的选项)。
这对于我需要实现的一些搜索可能非常有用。
作为参考,以下是 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),
price int,
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';
【问题讨论】:
-
@dnoeth 哦...我从未意识到 PostgreSQL 的索引可以基于物化视图。有趣的。不理想,因为他们的数据可能过时(有点),但仍然是一个有用的解决方法。
标签: sql postgresql indexing query-performance multi-table