Maak een samengestelde index op postings (is_active, post_date) (in die volgorde).
Het wordt zowel gebruikt om te filteren op is_active en bestellen voor post_date .
MySQL zou REF moeten tonen toegangsmethode over deze index in EXPLAIN EXTENDED .
Merk op dat je een RANGE . hebt filtervoorwaarde over user_offtopic_count , daarom kunt u geen index over dit veld gebruiken, zowel bij het filteren als bij het sorteren op een ander veld.
Afhankelijk van hoe selectief uw user_offtopic_count . is (d.w.z. hoeveel rijen voldoen aan user_offtopic_count < 10 ), kan het handiger zijn om een index te maken op user_offtopic_count en laat de post_dates worden gesorteerd.
Maak hiervoor een samengestelde index op postings (is_active, user_offtopic_count) en zorg ervoor dat het RANGE toegangsmethode via deze index wordt gebruikt.
Welke index sneller zal zijn, hangt af van uw gegevensdistributie. Maak beide indexen, FORCE en kijk wat sneller is:
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 */