Buat indeks komposit baik pada postings (is_active, post_date) (dalam urutan itu).
Ini akan digunakan untuk memfilter is_active dan memesan sebelum post_date .
MySQL harus menampilkan REF metode akses melalui indeks ini dalam EXPLAIN EXTENDED .
Perhatikan bahwa Anda memiliki RANGE memfilter kondisi di atas user_offtopic_count , itu sebabnya Anda tidak dapat menggunakan indeks di atas bidang ini baik dalam pemfilteran maupun pengurutan berdasarkan bidang lain.
Bergantung pada seberapa selektif user_offtopic_count Anda (yaitu berapa banyak baris yang memenuhi user_offtopic_count < 10 ), mungkin lebih berguna untuk membuat indeks di user_offtopic_count dan biarkan tanggal_posting diurutkan.
Untuk melakukannya, buat indeks gabungan pada postings (is_active, user_offtopic_count) dan pastikan RANGE metode akses melalui indeks ini digunakan.
Indeks mana yang akan lebih cepat tergantung pada distribusi data Anda. Buat kedua indeks, FORCE mereka dan lihat mana yang lebih cepat:
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 */