- Affected version
- 2.0.9
XF\Job\PostEmbedMetadata::getIdsToRebuild
does a select and in the where clause has "attach_count > 0"This causes what is effectively a full-table scan if there is statistically very few attachments. Worse, the batch-size will be reduced, except this makes the performance worse are it means more table-scans to get usable post-ids.
Instead of;
SQL:
SELECT post_id
FROM xf_post
WHERE post_id > 19209
AND attach_count > 0
ORDER BY post_id
LIMIT 293
Explain
Code:
+------+-------------+---------+-------+---------------+---------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+-------+---------------+---------+---------+------+---------+-------------+
| 1 | SIMPLE | xf_post | range | PRIMARY | PRIMARY | 4 | NULL | 5935051 | Using where |
+------+-------------+---------+-------+---------------+---------+---------+------+---------+-------------+
Why not use;
SQL:
select distinct content_id
from xf_attachment
where content_type = 'post'
and content_id > 19209
order by content_id
limit 293;
Explain
Code:
+------+-------------+---------------+-------+----------------------+----------------------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------------+-------+----------------------+----------------------+---------+------+------+--------------------------+
| 1 | SIMPLE | xf_attachment | range | content_type_id_date | content_type_id_date | 31 | NULL | 414 | Using where; Using index |
+------+-------------+---------------+-------+----------------------+----------------------+---------+------+------+--------------------------+
In my environment this is the difference of ~4 minutes to run the 1st query (per iteration!!) vs 0.01 seconds for the 2nd
This will then be accurate where as attach_count may not be, and is a horrible selector. If needed a join from xf_attachment => xf_post can be added for additional filtering criteria.