MySQL: consulta muy lenta (selección simple en una tabla)

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

Mis tipos son:

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

Los índices son:

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

EXPLICAR:

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

preguntado el 22 de mayo de 14 a las 13:05

Is it an InnoDB or MyISAM table? -

Are you happy with the undefined order or the rows, when you are using LIMIT? If you have a specific order you need then possibly add that order field as a second column to the category_category_id index -

Sounds like the index takes a while to load the first time, once it's loaded the operation should be fast (which seems to be what you're seeing) -

If I execute sequence of some another queries and re-run this one. I'm back on 200 - 400 ms :( -

LIMIT without ORDER BY is a nonsense -

1 Respuestas

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

Entonces llámalo con

CALL procedure_name(18,10,2000);

contestado el 27 de mayo de 14 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.