这是MySQL中的低效查询吗?

时间:2011-12-08 06:03:45

标签: mysql sql database indexing

mysql> desc posts_posts;
+------------+------------+------+-----+---------+----------------+
| Field      | Type       | Null | Key | Default | Extra          |
+------------+------------+------+-----+---------+----------------+
| id         | int(11)    | NO   | PRI | NULL    | auto_increment |
| user_id    | int(11)    | NO   | MUL | NULL    |                |
| body       | text       | YES  |     | NULL    |                |
| created_at | datetime   | YES  |     | NULL    |                |
| updated_at | datetime   | YES  |     | NULL    |                |
| is_deleted | tinyint(1) | NO   |     | 0       |                |
+------------+------------+------+-----+---------+----------------+
6 rows in set (0.06 sec)

mysql> desc posts_personas_assc;
+------------+----------+------+-----+---------+----------------+
| Field      | Type     | Null | Key | Default | Extra          |
+------------+----------+------+-----+---------+----------------+
| id         | int(11)  | NO   | PRI | NULL    | auto_increment |
| post_id    | int(11)  | NO   | MUL | NULL    |                |
| persona_id | int(11)  | NO   | MUL | NULL    |                |
+------------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

基本上,用户创建帖子。每个帖子都可以附加“角色”。换句话说,这是一种多对多的关系。 (这里没有显示另一个人物表)

假设我有<000>个文件左右。我想运行此查询:

SELECT pp.id FROM posts_posts pp 
 INNER JOIN posts_persona_assc ppa ON pp.id = ppa.post_id
 WHERE ppa.persona_id IN(id1, id2, id3)
   AND start_time > pp.created_at
 ORDER BY created_at DESC LIMIT num;
  • 我会将ppa.persona_id和pp.created_at编入索引,当然
  • “IN”部分永远不会超过3个IDS。

运行此查询对我来说是否足够高效?它会足够快吗?

每次有人点击用户个人资料页面时都会运行此查询。

1 个答案:

答案 0 :(得分:0)

替代IN子句可能会为您提供更快的结果,请查看IN Slowness

处的IN变体