4

对于稍微复杂的查询,怎么判断要对哪些字段加索引呢

 2 years ago
source link: https://www.v2ex.com/t/800002
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  PostgreSQL

对于稍微复杂的查询,怎么判断要对哪些字段加索引呢

  zxCoder · 1 小时 18 分钟前 · 46 次点击

比如我有一个查询,包括一个子查询,这个子查询有四个 and 的 where 条件,我就给这四个属性加了一个联合索引,

然后 where 之后还对 userId 进行分组求 MIN(某个属性),然后外层查询就有一个某个属性 IN 这个子查询的结果, 这里的操作我还需要用到什么索引吗?

最后我还需要对这个结果进行排序,按某个字段排序,这里加索引还有用吗?

大概是这样子的,( sql 不太熟悉,写的这个可能有错

items(userId,value,a,b,c,d,e)

SELECT * FROM items WHERE value IN 
    (
        SELECT MIN(value) WHERE a=1 AND b=2 AND c=3 AND d=4 GROUP BY userId  
    )
    ORDER BY e;

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK