postings (is_active, post_date)のいずれかに複合インデックスを作成します (この順序で)
is_activeでのフィルタリングの両方に使用されます post_dateによる注文 。
MySQL REFを表示する必要があります EXPLAIN EXTENDEDのこのインデックスに対するアクセス方法 。
RANGEがあることに注意してください user_offtopic_countでのフィルタリング条件 、そのため、フィルタリングと他のフィールドによる並べ替えの両方で、このフィールドのインデックスを使用することはできません。
user_offtopic_countの選択度によって異なります (つまり、user_offtopic_count < 10を満たす行の数 )、user_offtopic_countにインデックスを作成すると便利な場合があります post_datesを並べ替えます。
これを行うには、postings (is_active, user_offtopic_count)に複合インデックスを作成します RANGEを確認してください このインデックスに対するアクセス方法が使用されます。
どのインデックスが高速になるかは、データの分散によって異なります。両方のインデックスを作成します、FORCE それらを確認し、どちらが速いかを確認します:
CREATE INDEX ix_active_offtopic ON postings (is_active, user_offtopic_count);
CREATE INDEX ix_active_date ON postings (is_active, post_date);
SELECT
`postings`.`id`,
UNIX_TIMESTAMP(postings.post_date) as post_date,
`postings`.`link`,
`postings`.`title`,
`postings`.`author`,
`postings`.`excerpt`,
`postings`.`long_excerpt`,
`feeds`.`title` AS feed_title,
`feeds`.`website` AS feed_website
FROM
`postings` FORCE INDEX (ix_active_offtopic)
JOIN
`feeds`
ON
`feeds`.`id` = `postings`.`feed_id`
WHERE
`feeds`.`type` = 1 AND
`postings`.`user_offtopic_count` < 10 AND
`postings`.`is_active` = 1
ORDER BY
`postings`.`post_date` desc
LIMIT
15
/* This should show RANGE access with few rows and keep the FILESORT */
SELECT
`postings`.`id`,
UNIX_TIMESTAMP(postings.post_date) as post_date,
`postings`.`link`,
`postings`.`title`,
`postings`.`author`,
`postings`.`excerpt`,
`postings`.`long_excerpt`,
`feeds`.`title` AS feed_title,
`feeds`.`website` AS feed_website
FROM
`postings` FORCE INDEX (ix_active_date)
JOIN
`feeds`
ON
`feeds`.`id` = `postings`.`feed_id`
WHERE
`feeds`.`type` = 1 AND
`postings`.`user_offtopic_count` < 10 AND
`postings`.`is_active` = 1
ORDER BY
`postings`.`post_date` desc
LIMIT
15
/* This should show REF access with lots of rows and no FILESORT */