Mysql
 SQL >> Database >  >> RDS >> Mysql

Velocizzare una query WordPress lenta con STRAIGHT_JOIN

Il workaround in cui mi sono imbattuto è sinceramente brutto ma, per ragioni che non riesco a spiegare del tutto, funziona. Aggiungere il suggerimento all'ottimizzatore STRAIGHT_JOIN ha ridotto il tempo di esecuzione da oltre 18 secondi a circa 0,0022 secondi.

Sia il buon senso sia questa domanda correlata (When to use STRAIGHT_JOIN with MySQL) lo sconsigliano, eppure è l'unica cosa che ho provato ad avermi aiutato davvero. Quindi per ora lo tengo. Se qualcuno ha idee sul perché non dovrei farlo o su cosa provare altrimenti, sarei davvero felice di sentirle.

Se sei curioso, l'ho inserito come filtro di WordPress così:

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' ); 

Per completezza, ecco l'output di EXPLAIN con STRAIGHT_JOIN applicato. Sono di nuovo perplesso. La query originale usava solo ref ed eq_ref, che mi risultano più veloci di range, eppure per qualche motivo questa versione è di ordini di grandezza più rapida.

+-----+--------------+------------------------+--------+---------------------------+-------------------+----------+-----------------+-------+----------------------------------------------+ | 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 | +-----+--------------+------------------------+--------+---------------------------+-------------------+----------+-----------------+-------+----------------------------------------------+