En las publicaciones tengo un campo de fecha y un rango de fechas de inicio y fin. Necesito consultar si coincide con la fecha o si coincide entre las fechas del rango. Esta consulta funciona, pero ¿cómo puedo optimizarla para que sea más rápida?
$args = array(
'post_type' => array('post'),
'post_status' => array( 'publish' ),
'posts_per_page' => 20,
'paged' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'this_day',
'value' => $startday,
'compare' => 'LIKE',
),
array(
'relation' => 'AND',
array(
'key' => 'date_start',
'value' => $endday,
'compare' => '<=',
'type' => 'DATE',
),
array(
'key' => 'date_end',
'value' => $startday,
'compare' => '>=',
'type' => 'DATE',
),
)
));
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1 AND ( ( ( wp_postmeta.meta_key = 'this_day' AND wp_postmeta.meta_value="20220701" ) OR ( ( mt1.meta_key = 'date_start' AND CAST(mt1.meta_value AS DATE) <= '20220701' ) AND ( mt2.meta_key = 'date_end' AND CAST(mt2.meta_value AS DATE) >= '20220701' ) ) ) ) AND wp_posts.post_type="post" AND ((wp_posts.post_status="publish")) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 20
.