Make WordPress Core

Opened 4 years ago

Last modified 5 months 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 5 months ago.

Download all attachments as: .zip

Change History (6)

#1 @SergeyBiryukov
4 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
5 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
5 months ago

@vladislavdavarashvili
Updated query with generated patch 52360.patch

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


5 months ago
#5

  • Keywords has-patch added

#6 in reply to: ↑ 3 @peterwilsoncc
5 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 );
Note: See TracTickets for help on using tickets.