Skip to content

1.10.1: finding next sequential order number super-slow (15 min) on postmeta model with high row count #36

@lkraav

Description

@lkraav
INSERT INTO wp_postmeta (post_id, meta_key, meta_value) SELECT 8705342, '_order_number', IF( MAX( CAST( meta_value as UNSIGNED ) ) IS NULL, 1, MAX( CAST( meta_value as UNSIGNED ) ) + 1 ) FROM wp_postmeta WHERE meta_key='_order_number'

This query is currently taking 15 minutes to execute on our live server, with millions of posts, few tens of millions of postmeta rows.
What can we do to improve this query on postmeta, other than HPOS migration (it's roadmapped, but not immediate)?

EXPLAIN below:

EXPLAIN PARTITIONS
select 8705342, '_order_number', IF( MAX( CAST( meta_value as UNSIGNED ) ) IS NULL, 1, MAX( CAST( meta_value as UNSIGNED ) ) + 1 ) FROM wp_postmeta WHERE meta_key='_order_number'
___________ Sub-Part 1 ___________
Select Type: SIMPLE               
      Table: wp_postmeta          
 Partitions:                      
       Type: ref                  
 Poss. Keys: meta_key             
      Index: meta_key             
 Key Length: 1022                 
  Index Ref: const                
  Row Count: 196706               
    Special: Using index condition

[This query has been re-written to be explainable]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions