godaddy-wordpress / godaddy-wordpress/woocommerce-sequential-order-numbers

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

Open
#36 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
28
Forks
12
PR merge metrics
No merged PRs in 30d

Description

```
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]
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.