冷运行此查询需要 200 - 400 毫秒。当我重新运行它时,它是即时的。但我认为冷查询非常慢。我可以做些什么来提高速度?数据库在具有足够 DDR2 内存的 core2duo 3,16ghz 上运行。
SELECT * from item_has_category
WHERE category_category_id = 18
LIMIT 10 OFFSET 2000
表中没有太多条目
mysql> SELECT COUNT(*) from item_has_category;
+----------+
| COUNT(*) |
+----------+
| 111611 |
+----------+
我的类型是:
mysql> describe item_has_category;
+----------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------+------+-----+---------+-------+
| item_item_id | int(11) | NO | MUL | NULL | |
| category_category_id | int(11) | NO | MUL | NULL | |
+----------------------+---------+------+-----+---------+-------+
索引是:
mysql> SHOW INDEX from item_has_category;
+-------------------+------------+----------------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------------+------------+----------------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| item_has_category | 1 | category_category_id | 1 | category_category_id | A | 56 | NULL | NULL | | BTREE | | |
| item_has_category | 1 | item_item_id_2 | 1 | item_item_id | A | 111855 | NULL | NULL | | BTREE | | |
+-------------------+------------+----------------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
解释:
mysql> EXPLAIN SELECT * from item_has_category WHERE category_category_id = 18 LIMIT 10,2000;
+----+-------------+-------------------+------+----------------------+----------------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+------+----------------------+----------------------+---------+-------+------+-------+
| 1 | SIMPLE | item_has_category | ref | category_category_id | category_category_id | 4 | const | 2840 | |
+----+-------------+-------------------+------+----------------------+----------------------+---------+-------+------+-------+
最佳答案
您可以尝试将其作为存储过程运行,因为它们已被缓存并且可能会为您运行得更快一些。
DELIMITER $$
CREATE PROCEDURE `database_name`.`procedure_name` (
IN category_id INT,
IN rows_limit INT,
IN records_offset INT)
BEGIN
SELECT * from item_has_category
WHERE category_category_id = category_id
LIMIT rows_limit OFFSET records_offset
END
然后调用它
CALL procedure_name(18,10,2000);
关于MySQL - 非常慢的查询(在一张表上简单选择),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23806441/