次のようなテーブルがあります。
+--------------+--------------+------+-----+--------------------------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+--------------------------------------+-------+
| id | varchar(36) | NO | PRI | NULL | |
| provider_id | varchar(36) | YES | MUL | 00000000-0000-0000-0000-000000000000 | |
| to_provider | int(11) | NO | | NULL | |
| to_customer | int(11) | NO | | NULL | |
| published_at | datetime | NO | | NULL | |
| expired_at | datetime | NO | | NULL | |
| title | varchar(512) | NO | | NULL | |
| content | text | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| created_by | varchar(255) | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| updated_by | varchar(255) | YES | | NULL | |
| deleted_at | datetime | YES | | NULL | |
| deleted_by | varchar(255) | YES | | NULL | |
+--------------+--------------+------+-----+--------------------------------------+-------+
そして、私はこのようなインデックスを作成します:
+---------------+------------+-------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+-------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| announcements | 0 | PRIMARY | 1 | id | A | 76184 | NULL | NULL | | BTREE | | |
| announcements | 1 | idx_announcements | 1 | provider_id | A | 7 | NULL | NULL | YES | BTREE | | |
| announcements | 1 | idx_announcements | 2 | deleted_at | A | 7 | NULL | NULL | YES | BTREE | | |
| announcements | 1 | idx_announcements | 3 | published_at | A | 7 | NULL | NULL | | BTREE | | |
| announcements | 1 | idx_announcements | 4 | to_provider | A | 7 | NULL | NULL | | BTREE | | |
| announcements | 1 | idx_announcements | 5 | to_customer | A | 7 | NULL | NULL | | BTREE | | |
| announcements | 1 | idx_announcements | 6 | expired_at | A | 7 | NULL | NULL | | BTREE | | |
| announcements | 1 | idx_announcements | 7 | updated_at | A | 7 | NULL | NULL | YES | BTREE | | |
+---------------+------------+-------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
私が走るとき
MariaDB [table_name]> EXPLAIN SELECT COUNT(*) FROM `announcements` `t` WHERE (((`t`.provider_id = "3c5e63df-cb9b-f5a8-4eaf-7ed0061b797d") OR (provider_id="00000000-0000-0000-0000-000000000000")) AND (`t`.deleted_at IS NULL))AND(((published_at <= "2015-07-17 14:54:36") AND ( to_provider IN (1))) AND (to_customer IN (0, 1)));
+------+-------------+-------+-------+-------------------+-------------------+---------+------+-------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+-------------------+-------------------+---------+------+-------+--------------------------+
| 1 | SIMPLE | t | range | idx_announcements | idx_announcements | 64 | NULL | 38093 | Using where; Using index |
+------+-------------+-------+-------+-------------------+-------------------+---------+------+-------+--------------------------+
ほら、インデックスを使用できますが、実行すると
MariaDB [table_name]> EXPLAIN SELECT * FROM `announcements` `t` WHERE (((`t`.provider_id = "3c5e63df-cb9b-f5a8-4eaf-7ed0061b797d") OR (provider_id="00000000-0000-0000-0000-000000000000")) AND (`t`.deleted_at IS NULL))AND(((published_at <= "2015-07-17 14:54:36") AND ( to_provider IN (1))) AND (to_customer IN (0, 1)));
+------+-------------+-------+------+-------------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+-------------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | t | ALL | idx_announcements | NULL | NULL | NULL | 76184 | Using where |
+------+-------------+-------+------+-------------------+------+---------+------+-------+-------------+
ほら、キーがnullです!私は本当にそれを取得しません。同じ場所を使用しますが、「select *」はインデックスを使用できません。
誰でも私に言うことができますか?
ああ、私はこのようなSQLを手に入れました
SELECT * FROM `announcements` `t`
WHERE
(((`t`.provider_id = :provider_id) OR (provider_id=:ycp0)) AND (`t`.deleted_at IS NULL))
AND (published_at<="2015-07-17 16:58:57" AND (to_provider IN(1) AND to_customer IN (0,1))
AND expired_at>="2015-07-17 16:58:57"
AND (updated_at > "2015-07-10 16:58:57" || published_at > "2015-07-10 16:58:57" ))
ORDER BY updated_at DESC LIMIT 10
このクエリのインデックスを作成する方法は? 「OR」のことで問題が発生しました ~~~