合并在一起时,非​​常快速和简单的查询非常慢

时间:2018-06-01 17:16:10

标签: mysql sql database query-performance mysql-5.7

我有这两张桌子。我正在尝试查找id为1的用户已加入的组。 这是每个表的10行(只是为了显示它们的布局):

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

考虑到每个查询本身运行速度超快但合并时速度慢,会出现什么问题?

4 个答案:

答案 0 :(得分:4)

如果您的问题得到简化,并且 var users = []; class User { constructor(email, name) { this.email = email; this.name = name; users.push(this) } } var user1 = new User("john@sds.com", "John"); var user2 = new User("san@wp.com", "San"); 包含的列数超出了您向我们展示的列数,那么您的PostTable子句会导致大量浪费的排序。

您可以进行所谓的"延期加入。"首先获取适当的id值,然后使用它们来检索行。

ORDER BY ... LIMIT ...

这会将昂贵的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值仅在主表中点击25次。

答案 1 :(得分:2)

我会使用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 :(得分:1)

要加快第一个查询,请添加以下索引:

create index ix1 on joinedgroupstable (userid, groupid);

create index ix2 on PostTable (id);

第二个查询对我来说不对。

答案 3 :(得分:0)

不同答案:

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;