1

我对我不想做的查询的速度有疑问。rooms 表有 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      |         |               |
+-------------+------------+--------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4

3 回答 3

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 的房间,减少获取的记录

于 2013-04-27T03:42:59.450 回答
0

按照以下步骤检查性能。

  1. 在字段“caption”上为其前 4 个字符和“active_users”创建索引。
  2. 明确选择字段,而不是“房间。*”。
  3. 使用“caption LIKE 'room%'”而不是“caption LIKE '%room%'”。

如果这仍然很慢,您应该为您的连接查询创建一个视图,然后在您的视图上执行简单的选择语句。

于 2013-04-27T04:01:55.050 回答
0

1 你可能想要一个索引,也许就在命令上?

2 你不需要 %room% 是吗?room% 会更快,如果你不这样做,你可以有效地搜索所有文件

3 如果可以并且只想活跃就不要离开吗?而是内部连接,因为它需要更少的连接(它不连接那些为空的连接)

4 除非需要,否则不要选择 .*?

5.如果你使用oracle,也许你可以使用不同的函数,比如包含

6.在标题上放置索引,它被大量搜索,这可能是问题所在

于 2013-04-27T03:48:12.487 回答