• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Fixed Poor index condition on getExpiredWarnings

Xon

Well-known member
#1
While troubleshooting some stalls, I noticed the query condition for the getExpiredWarnings (around the time of the stalls, but unrelated) has poor index usage:
Code:
   SELECT *
   FROM xf_warning
   WHERE expiry_date < ?
    AND expiry_date > 0
    AND is_expired = 0
The standard index is just on the expiry_date column, and gives the following query plan:

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'xf_warning', 'ALL', 'expiry', NULL, NULL, NULL, '12755', 'Using where'

Using a multi-column index (is_expired & expiry_date) gives the following query plan:
# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'xf_warning', 'range', 'expiry', 'expiry', '5', NULL, '1', 'Using index condition'


Code:
ALTER TABLE `xf_warning`
DROP INDEX `expiry` ,
ADD INDEX `expiry` (`expiry_date` ASC);

explain
SELECT *
FROM xf_warning
WHERE expiry_date < 1484933945
 AND expiry_date > 0
 AND is_expired = 0;

ALTER TABLE `xf_warning`
DROP INDEX `expiry` ,
ADD INDEX `expiry` (`is_expired` ASC, `expiry_date` ASC);

explain
SELECT *
FROM xf_warning
WHERE expiry_date < 1484933945
 AND expiry_date > 0
 AND is_expired = 0;