Der Workaround, den ich gefunden habe, ist zugegebenermaßen unschön, aber aus Gründen, die ich nicht vollständig erklären kann, funktioniert er. Das Hinzufügen des Optimierer-Hinweises STRAIGHT_JOIN senkte die Ausführungszeit von über 18 Sekunden auf rund 0,0022 Sekunden.
Sowohl der gesunde Menschenverstand als auch diese verwandte Frage (When to use STRAIGHT_JOIN with MySQL) legen nahe, dass es eine schlechte Idee ist, und doch ist es das Einzige, das ich ausprobiert habe und das tatsächlich geholfen hat. Für den Moment bleibe ich also dabei. Falls jemand Gedanken dazu hat, warum ich das nicht tun sollte oder was ich stattdessen versuchen könnte, würde ich mich aufrichtig freuen, sie zu hören.
Wenn es dich interessiert: Ich habe es als WordPress-Filter so eingebunden:
function use_straight_join( $distinct_clause ) { $distinct_clause = ( $use_straight_join ) ? 'STRAIGHT_JOIN' . $distinct_clause : $distinct_clause; return $distinct_clause; } add_filter( 'posts_distinct', 'use_straight_join' ); Zur Vollständigkeit hier die EXPLAIN-Ausgabe mit angewandtem STRAIGHT_JOIN. Auch hier bin ich ratlos. Die ursprüngliche Abfrage nutzte nur ref und eq_ref, die meines Verständnisses schneller sind als range, und trotzdem ist diese aus irgendeinem Grund um Größenordnungen schneller.
+-----+--------------+------------------------+--------+---------------------------+-------------------+----------+-----------------+-------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +-----+--------------+------------------------+--------+---------------------------+-------------------+----------+-----------------+-------+----------------------------------------------+ | 1 | SIMPLE | wp_posts | range | PRIMARY,type_status_date | type_status_date | 124 | NULL | 6 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | wp_postmeta | ref | post_id,meta_key | post_id | 8 | db.wp_posts.ID | 2 | Using where | | 1 | SIMPLE | mt1 | ref | post_id,meta_key | post_id | 8 | db.wp_posts.ID | 2 | Using where | | 1 | SIMPLE | mt2 | ref | post_id,meta_key | post_id | 8 | db.wp_posts.ID | 2 | Using where | | 1 | SIMPLE | mt3 | ref | post_id,meta_key | post_id | 8 | db.wp_posts.ID | 2 | Using where | | 1 | SIMPLE | mt4 | ref | post_id,meta_key | post_id | 8 | db.wp_posts.ID | 2 | Using where | | 1 | SIMPLE | mt5 | ref | post_id,meta_key | post_id | 8 | db.mt3.post_id | 2 | Using where | | 1 | SIMPLE | mt6 | ref | post_id,meta_key | post_id | 8 | db.wp_posts.ID | 2 | Using where | | 1 | SIMPLE | wp_term_relationships | ref | PRIMARY,term_taxonomy_id | PRIMARY | 8 | db.wp_posts.ID | 1 | Using where; Using index | | 1 | SIMPLE | tt1 | ref | PRIMARY,term_taxonomy_id | PRIMARY | 8 | db.wp_posts.ID | 1 | Using where; Using index | | 1 | SIMPLE | tt2 | ref | PRIMARY,term_taxonomy_id | PRIMARY | 8 | db.mt1.post_id | 1 | Using where; Using index | | 1 | SIMPLE | tt3 | ref | PRIMARY,term_taxonomy_id | PRIMARY | 8 | db.wp_posts.ID | 1 | Using where; Using index | +-----+--------------+------------------------+--------+---------------------------+-------------------+----------+-----------------+-------+----------------------------------------------+