具有条件的 Postgres 索引 2 列
Postgres index 2 columns with condition
鉴于 table 的商店(~10k 条记录)和 table 的时间表(~200k 条记录),我正在尝试创建和索引供计划者使用,但它忽略了到目前为止。
select * from stores str
inner join schedule sch on sch.store_id = str.store_id
where sch.open is true and sch.tables > 0
create index sch_open_tables_idx on schedule(open, tables) where open is true and tables > 0
有正确的方法吗?
这些是 2 个表,所以使用 2 个索引 - 每个索引一个,无论如何,这么小的表不应该需要部分索引
EXPLAIN select * from stores str
inner join schedule sch USING (store_id)
where sch.open is true and tables > 0;
create index sch_open_idx on schedule(store_id) where open is true;
create index str_tables_idx on stores(store_id) where tables > 0;
vacuum analyze stores;
vacuum analyze schedule;
EXPLAIN select * from stores str
inner join schedule sch USING (store_id)
where sch.open is true and tables > 0;
您需要的索引是:
create index sch_open_tables_id on schedule(store_id)
where open and tables > 0;
store_id
可能是 stores
table 上的主键,因此上面已经有一个索引。如果不是:
create index store_id on stores(store_id);
鉴于 table 的商店(~10k 条记录)和 table 的时间表(~200k 条记录),我正在尝试创建和索引供计划者使用,但它忽略了到目前为止。
select * from stores str
inner join schedule sch on sch.store_id = str.store_id
where sch.open is true and sch.tables > 0
create index sch_open_tables_idx on schedule(open, tables) where open is true and tables > 0
有正确的方法吗?
这些是 2 个表,所以使用 2 个索引 - 每个索引一个,无论如何,这么小的表不应该需要部分索引
EXPLAIN select * from stores str
inner join schedule sch USING (store_id)
where sch.open is true and tables > 0;
create index sch_open_idx on schedule(store_id) where open is true;
create index str_tables_idx on stores(store_id) where tables > 0;
vacuum analyze stores;
vacuum analyze schedule;
EXPLAIN select * from stores str
inner join schedule sch USING (store_id)
where sch.open is true and tables > 0;
您需要的索引是:
create index sch_open_tables_id on schedule(store_id)
where open and tables > 0;
store_id
可能是 stores
table 上的主键,因此上面已经有一个索引。如果不是:
create index store_id on stores(store_id);