我在MySQL数据库中有两个整数列的表格,例如:SQL查询找到两个特定组中的用户数
userid | groupid
10 | 300
11 | 300
11 | 301
12 | 302
给定两个groupid,我正在寻找最好和最快的方法来找到两个组中的userid。我的表格包含23M行,我需要为每个不同的组对。目前,两列都被编入索引,但即使对于一对组,也需要很长时间才能得到结果,而且我有1000个不同的组别。我现在正在运行的查询是:
select count(t2.userid)
from usergroup t1, usergroup t2
where t1.groupid = 27 and t2.groupid = 714 and t1.userid = t2.userid
有没有办法快速做到这一点?
define indexed?你有什么指标。 – 2011-06-16 00:47:08
那么当我发送创建方案到SQL编辑器的索引定义是: KEY'uindex'('userid'), KEY'gindex'('groupid') – Nasir 2011-06-16 00:50:56
如果你覆盖你的索引,你会得到更好的性能。用(groupid,userid)创建一个唯一的索引 – 2011-06-16 01:00:17