MySQL - 非常慢的查询(在一个表上简单选择)

时间:2014-05-22 12:10:06

标签: mysql indexing query-optimization

此查询的冷运行需要200 - 400 ms。当我重新运行它的瞬间。但在我看来,冷查询极其缓慢。我该怎么做才能提高速度?数据库在core2duo 3,16ghz上运行,具有足够的DDR2内存。

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      |         |               |
        +-------------------+------------+----------------------+--------------+----------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

EXPLAIN:

        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 |       |
        +----+-------------+-------------------+------+----------------------+----------------------+---------+-------+------+-------+

1 个答案:

答案 0 :(得分:0)

您可以尝试将其作为存储过程运行,因为它们被缓存并且可能会更快地运行。

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);