Make WordPress Core

Opened 12 years ago

Last modified 3 weeks ago

#27736 reopened enhancement

Save one query when inserting a new post

Reported by: ozh's profile ozh Owned by: pbearne's profile pbearne
Milestone: Future Release Priority: normal
Severity: normal Version: 3.0
Component: Posts, Post Types Keywords: needs-patch close
Focuses: administration, performance Cc:

Description

The workflow when inserting a new post is the following :

1) wp_insert_post() sets a few vars (post type, post status, ...), updates $wpdb->posts, then calls wp_set_post_categories()

2) first thing wp_set_post_categories() wants to know is $post_type and $post_status, for which it performs an SQL query, instead of getting this info from caller function

Same things goes for wp_insert_attachment()

Patch passes extra optional parameters $post_type and $post_status to wp_set_post_categories(), to save one query when adding a post, or a whole bunch when importing a batch.

Attachments (4)

27736.post.php.patch (1.5 KB) - added by ozh 12 years ago.
27736.post.php.2.patch (1.8 KB) - added by ozh 12 years ago.
fix wp_insert_attachment() as well
27736.post.php.3.patch (1.8 KB) - added by ozh 12 years ago.
sorry, coding standards... tabs, not spaces
27736.post.php.4.patch (2.1 KB) - added by ozh 12 years ago.
More coding standards: braces on all if

Download all attachments as: .zip

Change History (22)

@ozh
12 years ago

@ozh
12 years ago

fix wp_insert_attachment() as well

#1 @ozh
12 years ago

  • Focuses administration performance added
  • Keywords has-patch added

@ozh
12 years ago

sorry, coding standards... tabs, not spaces

#2 @DrewAPicture
12 years ago

This seems pretty straightforward. We're also now always using braces on all if statements.

@ozh
12 years ago

More coding standards: braces on all if

#3 @johnbillion
12 years ago

  • Severity changed from normal to minor
  • Version changed from trunk to 3.0

#4 @ozh
12 years ago

(why 3.0 ?)

#5 @johnbillion
12 years ago

The issue was introduced in version 3.0 ([13184] and [14883]).

#6 @wonderboymusic
11 years ago

  • Keywords needs-refresh added

The patch does not apply and seems to ignore all of the changes made in 4.0 (extract() removal, et al)

#7 @chriscct7
10 years ago

  • Severity changed from minor to normal

This ticket was mentioned in PR #8289 on WordPress/wordpress-develop by @pbearne.


13 months ago
#8

  • Keywords has-unit-tests added; needs-refresh removed

…st_status, to the wp_set_post_categories()` function. The intention is to improve performance by avoiding redundant database calls when the post type and status are already known.

Trac ticket:

#9 @pbearne
13 months ago

  • Keywords close added
  • Owner set to pbearne
  • Status changed from new to assigned

Hi all

I refreshed the patch and added tests.

But the tests don't show a saving in DB calls.
I support that the new post is still in the object cache, so the saved calls don't now hit the DB

As such, I will close the tick as it won't fix unless someone else can see the DB saving with the patch

#10 @pbearne
13 months ago

  • Milestone set to Awaiting Review

#11 @peterwilsoncc
3 months ago

@pbearne Running clean_post_cache() before calling wp_set_post_categories() shows a difference in the number of database queries: six are made when the calling the latter with the post details provided, nine are made without.

#12 @westonruter
3 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from assigned to closed

I don't think this will be a good idea. In practice, the WP_Post may already be in the object cache, as evidenced by the performance improvement confusion when testing. Also, this relies on the caller providing the $post_type and $post_status that match whatever is for the provided $post_id. What if you provide something that is inconsistent? There's no longer a source of truth which we get by just relying on the $post_id. I concur that this should be closed.

#13 follow-up: @westonruter
3 months ago

  • Keywords close removed
  • Milestone set to 7.0
  • Resolution wontfix deleted
  • Status changed from closed to reopened

Actually, reading the ticket description, I now see this:

2) first thing wp_set_post_categories() wants to know is $post_type and $post_status, for which it performs an SQL query, instead of getting this info from caller function

Maybe a better approach would be to call update_post_cache() so that when wp_set_post_categories() runs it has access to to the post_type and post_status in the object cache?

#14 @westonruter
3 months ago

  • Keywords needs-patch added; has-patch has-unit-tests removed

#15 in reply to: ↑ 13 @peterwilsoncc
3 months ago

Replying to westonruter:

Maybe a better approach would be to call update_post_cache() so that when wp_set_post_categories() runs it has access to to the post_type and post_status in the object cache?

The call to get_post_type() already primes the cache (via get_post via WP_Post) so calling update_post_cache() will result in the same number of queries, the location would just change.

However, looking at wp_insert_post() even with this change the post will end up being queried with the call to $current_guid = get_post_field( 'guid', $post_id ); so passing the data to wp_set_post_categories() may not have an effect on the number of queries anyway.

This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.


3 weeks ago

#17 @westonruter
3 weeks ago

  • Keywords close added
  • Milestone changed from 7.0 to Future Release

#18 @ozh
3 weeks ago

LOL. Seriously, people, this report is _12 years old_ and there was a patch. Why not close it at this point?

Note: See TracTickets for help on using tickets.