Skip to content

Conversation

@rebeccahum
Copy link
Contributor

As per #417, I have removed the double left join on posts_join_filter.

@rebeccahum rebeccahum requested a review from trepmal May 30, 2017 18:31
@trepmal
Copy link
Contributor

trepmal commented May 30, 2017

In my test, which may not be comprehensive, I'm not seeing a change in the SQL query.

My test query:

$query_args = [
	'post_type'      => 'post',
	'post_status'    => 'publish',
	'posts_per_page' => 10,
	'author_name'    => $curauth->user_nicename,
	'tax_query'      => [
		[
			'taxonomy' => 'category',
			'field'    => 'slug',
			'terms'    => $category_slug,
		]
	],
	'orderby' => 'date',
	'order'   => 'DESC'
];

$featured_posts_author = new WP_Query( $query_args );

builds sql:

/* pre-patch */
SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts
  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
  LEFT JOIN wp_term_relationships AS tr1 ON (wp_posts.ID = tr1.object_id)
  LEFT JOIN wp_term_taxonomy ON ( tr1.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id ) WHERE 1=1  AND (
  wp_term_relationships.term_taxonomy_id IN (4)
) AND (( (wp_term_taxonomy.taxonomy = 'author' AND wp_term_taxonomy.term_id = '5') OR (wp_term_taxonomy.taxonomy = 'author' AND wp_term_taxonomy.term_id = '2'))) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID HAVING MAX( IF ( wp_term_taxonomy.taxonomy = 'author', IF (  wp_term_taxonomy.term_id = '5' OR  wp_term_taxonomy.term_id = '2',2,1 ),0 ) ) <> 1  ORDER BY wp_posts.post_date DESC LIMIT 0, 10

/* with patch */
SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts
  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
  LEFT JOIN wp_term_relationships AS tr1 ON (wp_posts.ID = tr1.object_id)
  LEFT JOIN wp_term_taxonomy ON ( tr1.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id ) WHERE 1=1  AND (
  wp_term_relationships.term_taxonomy_id IN (4)
) AND (( (wp_term_taxonomy.taxonomy = 'author' AND wp_term_taxonomy.term_id = '5') OR (wp_term_taxonomy.taxonomy = 'author' AND wp_term_taxonomy.term_id = '2'))) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID HAVING MAX( IF ( wp_term_taxonomy.taxonomy = 'author', IF (  wp_term_taxonomy.term_id = '5' OR  wp_term_taxonomy.term_id = '2',2,1 ),0 ) ) <> 1  ORDER BY wp_posts.post_date DESC LIMIT 0, 10

I might have missed something though, if you have a better test query let me know

@trepmal
Copy link
Contributor

trepmal commented May 30, 2017

Looks like it was just me bumbling around juggling too many queries...

I ran 3 sets of queries: (a) author + tax_query, (b) just author, and (c) just tax_query

pre and post patch, a and c are identical 👍

for b here's before

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_relationships AS tr1 ON (wp_posts.ID = tr1.object_id)
LEFT JOIN wp_term_taxonomy ON ( tr1.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
 WHERE 1=1  AND ((wp_posts.post_author = 3 OR (wp_term_taxonomy.taxonomy = 'author' AND wp_term_taxonomy.term_id = '6'))) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID HAVING MAX( IF ( wp_term_taxonomy.taxonomy = 'author', IF (  wp_term_taxonomy.term_id = '6',2,1 ),0 ) ) <> 1  ORDER BY wp_posts.post_date DESC LIMIT 0, 10

and after

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts
LEFT JOIN wp_term_relationships AS tr1 ON (wp_posts.ID = tr1.object_id)
LEFT JOIN wp_term_taxonomy ON ( tr1.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
 WHERE 1=1  AND ((wp_posts.post_author = 3 OR (wp_term_taxonomy.taxonomy = 'author' AND wp_term_taxonomy.term_id = '6'))) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID HAVING MAX( IF ( wp_term_taxonomy.taxonomy = 'author', IF (  wp_term_taxonomy.term_id = '6',2,1 ),0 ) ) <> 1  ORDER BY wp_posts.post_date DESC LIMIT 0, 10

Which is just a noisy way of saying it looks good!

edit: before/after examples accidentally reversed 🤦‍♀️

@trepmal trepmal merged commit a5222a9 into Automattic:master May 30, 2017
@rebeccahum rebeccahum deleted the Removed-double-left-join branch May 30, 2017 21:52
@philipjohn philipjohn added this to the 3.3 milestone Jun 17, 2017
@catchmyfame
Copy link

@rebeccahum @trepmal Please see #508 with regard to the SQL changes

rebeccahum pushed a commit that referenced this pull request Mar 26, 2019
Resolves #417

* Removed duplicate left join for optimization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants