在一个表中我有 4 列:
(a(int),b(int),c(var-char),d(var-char))
我有两个查询如下:
select *from table where a=1 and b=2 and c="abc";
select *from table where a=1 and b=2 and d="zyz";
现在索引方法:
- 在 (a,b,c) 上建立索引
- 在 (a,b,d) 上建立索引
或
- 索引(a,b)
- 索引(c)
- 索引(d)
哪种方法更好?
最佳答案
最好是尝试一下。除非您知道您计划使用的数据是什么,否则不可能说哪些索引更好。最好的方法是创建表,插入一些样本数据,然后使用 EXPLAIN
或 EXPLAIN ANALYZE
进行一些典型的查询。
一个例子是
EXPLAIN select *from table where a=1 and b=2 and c="abc";
EXPLAIN select *from table where a=1 and b=2 and d="zyz";
或
EXPLAIN ANALYZE 选择 *from table where a=1 and b=2 and c="abc";
EXPLAIN ANALYZE select *from table where a=1 and b=2 and d="zyz";
另见http://www.postgresql.org/docs/9.5/static/sql-explain.html
关于database - 在我的情况下,哪种索引方法更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35147202/