Skapa ett sammansatt index antingen på postings (is_active, post_date) (i den ordningen).
Den kommer att användas både för att filtrera på is_active och beställning senast post_date .
MySQL ska visa REF åtkomstmetod över detta index i EXPLAIN EXTENDED .
Observera att du har en RANGE filtreringsvillkor över user_offtopic_count , det är därför du inte kan använda ett index över detta fält både vid filtrering och vid sortering efter andra fält.
Beroende på hur selektivt ditt user_offtopic_count är (dvs. hur många rader uppfyller user_offtopic_count < 10 ), kan det vara mer användbart att skapa ett index på user_offtopic_count och låt post_datumen sorteras.
För att göra detta, skapa ett sammansatt index på postings (is_active, user_offtopic_count) och se till att RANGE åtkomstmetod över detta index används.
Vilket index som blir snabbare beror på din datadistribution. Skapa båda indexen, FORCE dem och se vilket som är snabbast:
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 */