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

标签 mysql indexing query-optimization

冷运行此查询需要 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/

相关文章:

mysql - 优化查询创建索引以从查询计划中删除使用临时和使用文件排序

sql - MYSQL - 在一个查询中合并两个结果

mysql - 在 HAVING 子句中使用 MAX/MIN 优化查询

mysql - 从 mysql 中选择多于 1 组的事件?

c# - 每当数据库更新时更新 C# 客户端

MySQL语法错误

php - 有没有办法让 PHP 将 sql 结果直接转换为 JSON?

mysql - SELECT 语句不使用 possible_keys

MySQL 根据用户和时间戳日期范围进行分区

django - 我如何限制 Django 中的 prefetch_related?