2012-07-20 106 views

回答

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

+0

谢谢。你能否评论你的解决方案与Omesh提到的创建覆盖索引? – Harper 2012-07-20 08:19:18

+0

也许这会帮助你http://stackoverflow.com/questions/8213235/mysql-covering-vs-composite-vs-column-index – Horen 2012-07-20 08:27:31

1

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

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

检查的地位,使用该查询使用索引,其中:

EXPLAIN SELECT COUNT(DISTINCT(EmailAddress)) 
FROM Subscribers 
WHERE Subscribers.OwnedBy = 67; 
相关问题