简体   繁体   中英

MySQL - Very slow query (simple select on one table)

Cold running of this query is taking 200 - 400 ms. When I re-run it its instant. But cold query is in my opinion extremly slow. What can I do to boost speed? Database is running on core2duo 3,16ghz with enough DDR2 memory.

SELECT * from item_has_category
        WHERE category_category_id = 18
        LIMIT 10 OFFSET 2000

I dont have much entries in table

        mysql> SELECT COUNT(*) from item_has_category;
        +----------+
        | COUNT(*) |
        +----------+
        |   111611 |
        +----------+

My types are:

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

Indexes are:

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

You could try running it as a Stored Procedure instead as they are cached and might run a bit quicker for you.

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

Then call it with

CALL procedure_name(18,10,2000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM