distinct是否需要索引(count(ColumnName))和where子句的索引?

时间:2012-07-20 07:57:04

标签: mysql indexing sql query-performance

我有一张这样的表

Subscribers
  Id (PK, int)
  OwnedBy (FK, int)
  EmailAddress (varchar 50)

如果我这样查询:

SELECT COUNT(DISTINCT(EmailAddress))
FROM Subscribers
WHERE Subscribers.OwnedBy = 67;

OwnedBy列上有一个索引。假设表非常大,我还应该索引EmailAddress吗?我并不担心写性能下降。

2 个答案:

答案 0 :(得分:1)

您应该创建覆盖索引以使其更快。

ALTER TABLE Subscribers ADD KET ix1(OwnedBy, EmailAddress);

将状态检查为使用索引与此查询的位置:

EXPLAIN SELECT COUNT(DISTINCT(EmailAddress))
FROM Subscribers
WHERE Subscribers.OwnedBy = 67;

答案 1 :(得分:1)

是的,你应该。

因为您的查询可以像这样重写:

SELECT COUNT(*)
FROM Subscribers
WHERE Subscribers.OwnedBy = 67
GROUP BY EmailAddress

您应该查看MySQL GROUP BY性能优化页面:

http://dev.mysql.com/doc/refman/5.0/en/group-by-optimization.html

在那里,陈述如下:

The most efficient way to process GROUP BY is when an index is used to 
directly retrieve the grouping columns

因此,如果您不关心表的插入/更新性能和大小,那么您应该使用EmailAddress上的索引