Make WordPress Core

Opened 5 years ago

Last modified 5 weeks ago

#52360 new enhancement

speed up update_meta_cache with easy fix

Reported by: vladislavdavarashvili's profile vladislavdavarashvili Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: has-patch
Focuses: performance Cc:

Description

wp-includes/meta.php:1061

<?php
$meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A );

in this request should avoid "ORDER BY $id_column ASC" to speed up this query (at least 3 times faster)

Attachments (1)

52360.patch (709 bytes) - added by iflairwebtechnologies 18 months ago.

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
5 years ago

  • Component changed from General to Options, Meta APIs

Hi there, welcome to WordPress Trac! Thanks for the ticket.

Just linking to some related tickets here: #14254, #42566.

#3 follow-up: @antonynz
18 months ago

25511 has a reason for adding ordering by meta_id to return the same order for get_post_meta. It makes sense, although the use case is an unorthodox way of saving meta, it could have an impact if post meta order was important in custom code?

In most cases the ordering wouldn't be important, and removing it could save 50-100ms+ on large post id look ups (600+ IDs).

#4 @iflairwebtechnologies
18 months ago

@vladislavdavarashvili
Updated query with generated patch 52360.patch

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


18 months ago
#5

  • Keywords has-patch added

#6 in reply to: ↑ 3 @peterwilsoncc
18 months ago

Replying to antonynz:

... it could have an impact if post meta order was important in custom code? ...

I think it's important to consider this use case, as the order returned may differ depending on different database engines. Using MySQL 8.0.36 I didn't see a difference but that may have been for any number of reasons.

It doesn't make this ticket a dead end, it would be worth comparing the performance for sorting via the database with sorting via PHP. If PHP is consistently faster then the code could become:

<?php
$meta_list = $wpdb->get_results( "SELECT * FROM $table WHERE $column IN ($id_list)", ARRAY_A );
$meta_list = wp_list_sort( $meta_list, $id_column );

#7 @iflairwebtechnologies
5 weeks ago

Thanks for the feedback regarding behavior changes.

I agree that unconditional ordering could affect edge cases relying on current meta order.

One possible compromise could be limiting ORDER BY meta_id ASC to scenarios where multiple object IDs are requested, where ordering is less likely to be relied upon, or restricting this optimization to post meta only.

Happy to explore a constrained approach if this direction makes sense.

Note: See TracTickets for help on using tickets.