慢速查询,在哪里,顺序和限制

时间:2013-04-27 03:30:27

标签: mysql

我遇到了查询速度的问题,我不想这样做。 房间表有2685588行,room_active小于100,但正常查询时速度不慢。

查询是:

SELECT rooms.*, room_active.active_users
FROM rooms LEFT JOIN room_active ON (room_active.roomid = rooms.id) 
WHERE caption LIKE '%room%' ORDER BY room_active.active_users LIMIT 50

运行时:4.3s

执行时间不应超过5秒,如下所示:

SELECT rooms.*, room_active.active_users 
FROM rooms LEFT JOIN room_active ON (room_active.roomid = rooms.id) 
WHERE caption LIKE '%room%' LIMIT 50

运行时:0.3s

第一个查询的描述如下:

+----+-------------+-------------+--------+----------------+---------+---------+----------------+---------+----------------------------------------------+
| id | select_type | table       | type   | possible_keys  | key     | key_len | ref            | rows    | Extra                                        |
+----+-------------+-------------+--------+----------------+---------+---------+----------------+---------+----------------------------------------------+
|  1 | SIMPLE      | rooms       | ALL    | NULL           | NULL    | NULL    | NULL           | 2210576 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | room_active | eq_ref | PRIMARY,roomid | PRIMARY | 4       | xukys.rooms.id |       1 | NULL                                         |
+----+-------------+-------------+--------+----------------+---------+---------+----------------+---------+----------------------------------------------+

索引:

+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| rooms |          0 | PRIMARY     |            1 | id          | A         |     2210613 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | owner       |            1 | owner       | A         |     2210613 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | roomtype    |            1 | roomtype    | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | caption     |            1 | caption     | A         |     2210613 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | category    |            1 | category    | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | users_now   |            1 | users_now   | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | tags        |            1 | tags        | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | users_max   |            1 | users_max   | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | description |            1 | description | A         |      368435 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | state       |            1 | state       | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | model_name  |            1 | model_name  | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | public_ccts |            1 | public_ccts | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | score       |            1 | score       | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | password    |            1 | password    | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | icon_bg     |            1 | icon_bg     | A         |         954 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | icon_fg     |            1 | icon_fg     | A         |          18 | NULL     | NULL   |      | BTREE      |         |               |
| rooms |          1 | badge_id    |            1 | badge_id    | A         |         804 | NULL     | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
+-------------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table       | Non_unique | Key_name     | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| room_active |          0 | PRIMARY      |            1 | roomid       | A             |         116 | NULL     | NULL   |      | BTREE      |         |               |
| room_active |          0 | roomid       |            1 | roomid       | A         |         116 | NULL     | NULL   |      | BTREE      |         |               |
| room_active |          1 | active_users |            1 | active_users | A         |          29 | NULL     | NULL   |      | BTREE      |         |               |
+-------------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

3 个答案:

答案 0 :(得分:1)

ORDER BY正在消除LIMIT 50的优势,因为他可以按照他必须获取所有记录的50个第一个元素进行排序。

也许如果你告诉我们你想要什么,我们可以帮助你查询。例如,您是否真的需要进行LEFT JOIN并获得所有没有room_active的房间?

不确定它是否有帮助,但试试这个

SELECT rooms.*, room_active.active_users 
FROM room_active ra JOIN rooms r ON (room_active.roomid = rooms.id) 
WHERE r.caption LIKE '%room%' ORDER BY ra.active_users LIMIT 50

它将忽略没有room_active的房间,减少所提取的记录

答案 1 :(得分:0)

1您可能想要订单上的索引吗?

2你不需要%room%吗?房间%会更快,如果你没有

,你有效地搜索所有文件 如果可以的话,

3不会离开加入并且只想要活动?而是内连接,因为它需要较少的连接(它不会连接为空的连接)

4不要选择。*除非需要?

5。如果您使用的是oracle

,也许可以使用不同的函数,例如Contains

6。在标题上加上一个索引,这可能是问题

答案 2 :(得分:0)

按照以下步骤检查效果。

  1. 在字段'caption'上为其前4个字符和'active_users'创建索引。
  2. 而不是“rooms。*”,请明确选择字段。
  3. 使用“caption LIKE'room%'”代替“caption LIKE'%room%'”。
  4. 如果这仍然很慢,您应该为已加入的查询创建一个视图,然后在视图上执行简单的select语句。