database - 在我的情况下,哪种索引方法更好?

标签 database postgresql indexing database-administration

在一个表中我有 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)

哪种方法更好?

最佳答案

最好是尝试一下。除非您知道您计划使用的数据是什么,否则不可能说哪些索引更好。最好的方法是创建表,插入一些样本数据,然后使用 EXPLAINEXPLAIN 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/

相关文章:

python - 通过列表循环名称

c# - (包装器托管到 native )Realms.NativeTable.get_string 中的 Realm 查询出错

sql - 在 PostgreSQL 中添加负间隔

java - 返回未排序列表中第 n 个最高值的索引

sql查询转json结果

sql - 如何修复 'missing FROM-clause entry for table' ?

database - SQLite 中的全文搜索

php - (Laravel) 无法将 group by 与 order by 一起使用

安卓服务器;登录页面

java - Java并发应用中MySQL死锁MySQLTransactionRollbackException