Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysqlインデックス #3

Open
tukeJonny opened this issue Oct 21, 2017 · 2 comments
Open

mysqlインデックス #3

tukeJonny opened this issue Oct 21, 2017 · 2 comments

Comments

@tukeJonny
Copy link
Contributor

No description provided.

@tukeJonny
Copy link
Contributor Author

SELECT * FROM message WHERE id > 0 AND channel_id = 884 ORDER BY id DESC LIMIT 100\G

mysql> ALTER TABLE message ADD INDEX idx_message_id_channel_id(channel_id, id);
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> EXPLAIN EXTENDED SELECT * FROM message WHERE id > 0 AND channel_id = 884 ORDER BY id DESC LIMIT 100;
+----+-------------+---------+------------+-------+--------------------------------------------------+---------------------------+---------+------+------+----------+-----------------------+
| id | select_type | table   | partitions | type  | possible_keys                                    | key                       | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+---------+------------+-------+--------------------------------------------------+---------------------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | message | NULL       | range | PRIMARY,idx_message_id,idx_message_id_channel_id | idx_message_id_channel_id | 17      | NULL |    1 |   100.00 | Using index condition |
+----+-------------+---------+------------+-------+--------------------------------------------------+---------------------------+---------+------+------+----------+-----------------------+
1 row in set, 2 warnings (0.00 sec)

mysql> mysql> EXPLAIN EXTENDED SELECT * FROM message WHERE id > 0 AND channel_id = 884 ORDER BY id DESC LIMIT 100 \G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: message
   partitions: NULL
         type: range
possible_keys: PRIMARY,idx_message_id,idx_message_id_channel_id
          key: idx_message_id_channel_id
      key_len: 17
          ref: NULL
         rows: 1
     filtered: 100.00
        Extra: Using index condition
1 row in set, 2 warnings (0.00 sec)

@tukeJonny
Copy link
Contributor Author

mysql> EXPLAIN SELECT * FROM message WHERE channel_id = 903 ORDER BY id DESC LIMIT 20 OFFSET 0 \G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: message
   partitions: NULL
         type: ref
possible_keys: idx_message_id_channel_id
          key: idx_message_id_channel_id
      key_len: 9
          ref: const
         rows: 1
     filtered: 100.00
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

ERROR:
No query specified

mysql>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant