2 非常快速和简单的查询合并在一起时非常慢
2 very fast and simple queries are terribly slow when merged together
我有这 2 个 table。我正在尝试查找 ID 为 1 的用户已加入的组。
这里有 10 行,每行 table(只是为了显示它们的布局):
PostTable(57,272 行,主键 id):
+----+---------+
| id | groupid |
+----+---------+
| 0 | 1 |
| 1 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 9 | 1 |
| 10 | 1 |
| 13 | 1 |
| 15 | 1 |
| 17 | 1 |
+----+---------+
JoinedGroupsTable(258,404 行,唯一索引 userid,groupid):
+--------+---------+--------+
| id | groupid | userid |
+--------+---------+--------+
| 258010 | 1 | 1 |
| 258484 | 6 | 1 |
| 172 | 1 | 2 |
| 173 | 2 | 2 |
| 174 | 3 | 2 |
| 175 | 4 | 2 |
| 176 | 5 | 2 |
| 177 | 6 | 2 |
| 178 | 8 | 2 |
| 179 | 9 | 2 |
+--------+---------+--------+
当我尝试 运行 这个查询时,它在将近 3 秒内完成,这真的很慢:
SELECT * FROM posttable p
WHERE groupid in (SELECT groupid FROM joinedgroupstable WHERE userid=1)
ORDER BY p.ID DESC LIMIt 25;
我也尝试过使用 INNER JOIN 而不是 WHERE IN,但结果大致相同:
SELECT * FROM posttable p
INNER JOIN joinedgroupstable jg ON userid=1 AND jg.groupid=p.groupid
ORDER BY p.ID DESC LIMIt 25;
这是两个查询的 EXPLAIN SELECT(两个查询的结果相同):
|| *id* || *select_type* || *table* || *partitions* || *type* || *possible_keys* || *key* || *key_len* || *ref* || *rows* || *filtered* || *Extra* ||
|| 1 || SIMPLE || jg || || ref || UserID_GroupID,userid || UserID_GroupID || 4 || const || 2 || 100.00 || Using index; Using temporary; Using filesort ||
|| 1 || SIMPLE || p || || ref || groupid || groupid || 4 || thyra.jg.groupid || 60 || 100.00 || ||
关键是 运行单独执行每个查询非常快:
SELECT * FROM posttable p ORDER BY p.ID DESC LIMIt 25;
SELECT * FROM joinedgroupstable WHERE userid=1
考虑到每个查询 运行 本身超快但合并时很慢,可能有什么问题?
为了加快第一个查询的速度,添加以下索引:
create index ix1 on joinedgroupstable (userid, groupid);
create index ix2 on PostTable (id);
我觉得第二个查询不对。
不同的答案:
create index ix10 on joinedgroupstable (userid);
create index ix11 on posttable (groupid);
select p.*
from posttable p
join joinedgroupstable g on p.groupid = g.groupid
where g.userid = 1
order by p.id desc
limit 25;
如果您的问题被简化了,并且您的 PostTable
包含的列比您向我们展示的要多,那么您的 ORDER BY ... LIMIT ...
子句会导致大量无用的排序。
您可以执行所谓的 "deferred join." 首先获取适当的 id 值,然后使用它们检索行。
select p.*
from posttable p
join (
select a.id
from posttable a
join joinedgroupstable g on a.groupid = g.groupid
where g.userid = 1
order by a.id desc
limit 25
) sel on sel.id = p.id
order by p.id desc
limit 25;
这将昂贵的 ORDER BY ... LIMIT ...
操作限制在 id
列,然后使用选择的 id
值仅命中主 table 25 次。
我会用 EXISTS
来代替它也可以表现得更好 :
select p.*
from posttable p
where exists (select 1
from joinedgroupstable jg
where jg.groupid = p.groupid and jg.userid = 1
)
order by p.id desc
limit 25;
我有这 2 个 table。我正在尝试查找 ID 为 1 的用户已加入的组。 这里有 10 行,每行 table(只是为了显示它们的布局):
PostTable(57,272 行,主键 id):
+----+---------+
| id | groupid |
+----+---------+
| 0 | 1 |
| 1 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 9 | 1 |
| 10 | 1 |
| 13 | 1 |
| 15 | 1 |
| 17 | 1 |
+----+---------+
JoinedGroupsTable(258,404 行,唯一索引 userid,groupid):
+--------+---------+--------+
| id | groupid | userid |
+--------+---------+--------+
| 258010 | 1 | 1 |
| 258484 | 6 | 1 |
| 172 | 1 | 2 |
| 173 | 2 | 2 |
| 174 | 3 | 2 |
| 175 | 4 | 2 |
| 176 | 5 | 2 |
| 177 | 6 | 2 |
| 178 | 8 | 2 |
| 179 | 9 | 2 |
+--------+---------+--------+
当我尝试 运行 这个查询时,它在将近 3 秒内完成,这真的很慢:
SELECT * FROM posttable p
WHERE groupid in (SELECT groupid FROM joinedgroupstable WHERE userid=1)
ORDER BY p.ID DESC LIMIt 25;
我也尝试过使用 INNER JOIN 而不是 WHERE IN,但结果大致相同:
SELECT * FROM posttable p
INNER JOIN joinedgroupstable jg ON userid=1 AND jg.groupid=p.groupid
ORDER BY p.ID DESC LIMIt 25;
这是两个查询的 EXPLAIN SELECT(两个查询的结果相同):
|| *id* || *select_type* || *table* || *partitions* || *type* || *possible_keys* || *key* || *key_len* || *ref* || *rows* || *filtered* || *Extra* ||
|| 1 || SIMPLE || jg || || ref || UserID_GroupID,userid || UserID_GroupID || 4 || const || 2 || 100.00 || Using index; Using temporary; Using filesort ||
|| 1 || SIMPLE || p || || ref || groupid || groupid || 4 || thyra.jg.groupid || 60 || 100.00 || ||
关键是 运行单独执行每个查询非常快:
SELECT * FROM posttable p ORDER BY p.ID DESC LIMIt 25;
SELECT * FROM joinedgroupstable WHERE userid=1
考虑到每个查询 运行 本身超快但合并时很慢,可能有什么问题?
为了加快第一个查询的速度,添加以下索引:
create index ix1 on joinedgroupstable (userid, groupid);
create index ix2 on PostTable (id);
我觉得第二个查询不对。
不同的答案:
create index ix10 on joinedgroupstable (userid);
create index ix11 on posttable (groupid);
select p.*
from posttable p
join joinedgroupstable g on p.groupid = g.groupid
where g.userid = 1
order by p.id desc
limit 25;
如果您的问题被简化了,并且您的 PostTable
包含的列比您向我们展示的要多,那么您的 ORDER BY ... LIMIT ...
子句会导致大量无用的排序。
您可以执行所谓的 "deferred join." 首先获取适当的 id 值,然后使用它们检索行。
select p.*
from posttable p
join (
select a.id
from posttable a
join joinedgroupstable g on a.groupid = g.groupid
where g.userid = 1
order by a.id desc
limit 25
) sel on sel.id = p.id
order by p.id desc
limit 25;
这将昂贵的 ORDER BY ... LIMIT ...
操作限制在 id
列,然后使用选择的 id
值仅命中主 table 25 次。
我会用 EXISTS
来代替它也可以表现得更好 :
select p.*
from posttable p
where exists (select 1
from joinedgroupstable jg
where jg.groupid = p.groupid and jg.userid = 1
)
order by p.id desc
limit 25;