我有一张这样的桌子
Subscribers
Id (PK, int)
OwnedBy (FK, int)
EmailAddress (varchar 50)
如果我这样查询:
SELECT COUNT(DISTINCT(EmailAddress))
FROM Subscribers
WHERE Subscribers.OwnedBy = 67;
列上有一个索引OwnedBy
。假设表很大,我也应该索引EmailAddress
吗?我不担心写性能下降。
我有一张这样的桌子
Subscribers
Id (PK, int)
OwnedBy (FK, int)
EmailAddress (varchar 50)
如果我这样查询:
SELECT COUNT(DISTINCT(EmailAddress))
FROM Subscribers
WHERE Subscribers.OwnedBy = 67;
列上有一个索引OwnedBy
。假设表很大,我也应该索引EmailAddress
吗?我不担心写性能下降。
您应该创建覆盖索引以使其更快。
ALTER TABLE Subscribers ADD KET ix1(OwnedBy, EmailAddress);
检查状态为 Using where Using Index with this query:
EXPLAIN SELECT COUNT(DISTINCT(EmailAddress))
FROM Subscribers
WHERE Subscribers.OwnedBy = 67;
是的你应该。
由于您的查询可以这样重写:
SELECT COUNT(*)
FROM Subscribers
WHERE Subscribers.OwnedBy = 67
GROUP BY EmailAddress
你应该看看 MySQLGROUP 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