【发布时间】:2021-01-27 10:30:33
【问题描述】:
我有 3 张表(这里都简化了)
job (id int, type char(1))
data (job_id int, owner int, creator int, value int)
user (id int)
我的查询是
select user.id, job.id, sum(data.value)
from job
join data on job.id = data.job_id
join user on case job.type when 'O' then data.owner else data.creator end = user.id
group by user.id, job.id
如何在 Postgres 中创建一个索引来满足连接中的 case 语句?
Job 可能有十几行、1000 多个用户和数百万数据。
谢谢
【问题讨论】:
-
条件需要来自两个不同表的值。您无法为此创建索引。只能使用您为其定义索引的表中的列在表达式上定义索引。
-
顺便说一句:这是 CASE 表达式而不是陈述。
-
恐怕是这样……然后回到绘图板上。还是谢谢。
-
您的查询的执行计划是什么?
-
将其更改为两个带有
UNION ALL的查询。
标签: database postgresql indexing