5

I have the following query and it works. But it takes insanely long to process due to it's buildup. I'm therefor in the need of assistance to get this query faster.

SQL Query

In the query PRODUCT_ID should be replaced by ' ' and the productID number.

SELECT
    b.order_id,
    customer_meta.meta_value AS customer_id,
    users.user_email,
    qty_table.meta_value AS qty,
    user_meta1.meta_value AS firstname,
    user_meta2.meta_value AS lastname,
    user_meta3.meta_value AS company,
    user_meta4.meta_value AS address,
    user_meta5.meta_value AS city,
    user_meta6.meta_value AS postcode,
    user_meta7.meta_value AS state,
    user_meta8.meta_value AS user_phone
FROM
    wp_woocommerce_order_itemmeta a,
    wp_woocommerce_order_items b,
    wp_postmeta customer_meta,
    wp_users users,
    wp_woocommerce_order_itemmeta qty_table,
    wp_usermeta user_meta1,
    wp_usermeta user_meta2,
    wp_usermeta user_meta3,
    wp_usermeta user_meta4,
    wp_usermeta user_meta5,
    wp_usermeta user_meta6,
    wp_usermeta user_meta7,
    wp_usermeta user_meta8
WHERE
    a.meta_key = '_product_id'
    AND a.meta_value = PRODUCT_ID
    AND a.order_item_id = b.order_item_id
    AND customer_meta.meta_key = '_customer_user'
    AND customer_meta.post_id = b.order_id
    AND user_meta1.meta_key = 'first_name'
    AND user_meta1.user_id = users.id
    AND user_meta2.meta_key = 'last_name'
    AND user_meta2.user_id = users.id
    AND user_meta3.meta_key = 'billing_company'
    AND user_meta3.user_id = users.id
    AND user_meta4.meta_key = 'billing_address_1'
    AND user_meta4.user_id = users.id
    AND user_meta5.meta_key = 'billing_city'
    AND user_meta5.user_id = users.id
    AND user_meta6.meta_key = 'billing_postcode'
    AND user_meta6.user_id = users.id
    AND user_meta7.meta_key = 'billing_state'
    AND user_meta7.user_id = users.id
    AND user_meta8.meta_key = 'billing_phone'
    AND user_meta8.user_id = users.id
    AND users.ID = customer_meta.meta_value
    AND qty_table.meta_key = '_qty'
    AND qty_table.order_item_id = b.order_item_id
ORDER BY user_meta3.meta_value ASC

I need all the information, since I want to list all the users with their Firstname, lastname, company, address, postcode, etc for a given product which has been bought. So the query in itself works, but is a killer in process time.

I could use max( CASE WHEN ... ... THEN ...END ) as a_name but I only know how to do it successfully if I use a left join.

Any tips on how to get this query to run better?

3 Answers 3

5

WP, are you listening? WooCommerce, are you listening? I am getting tired of optimizing your database app.

First and foremost, EAV is a terrible schema design. But I won't rant about that. I'll just point out the index(es) that are probably missing or mal-formed:

wp_usermeta:  PRIMARY KEY(user_id, meta_key)

Without any (191) tacked on.

Similarly for wp_woocommerce_order_itemmeta.

I may have more abuse to dish out; please provide SHOW CREATE TABLE for the tables being used.

4
4

Years later, after talking to @RickJames, here's another answer to your question.

  1. You have refactored the query from comma-join to LEFT JOINs. That is good. Your comma-join syntax omitted users with missing metadata from the result set.

  2. If you haven't already done so, make sure you upgrade your MySQL server so it's version 5.7 or later. If you use MariaDB, upgrade it so it's version 10.3 or later The version is important for performance.

  3. Install and use the WordPress plugin Index WP MySQL for Speed. Rick and I put it together to address precisely your problem. It reindexes wp_usermeta (and some other WordPress tables) to make these sorts of queries more efficient.

Or, if you prefer not to use a plugin to reindex your tables, you can do it yourself via phpmyadmin or some other SQL client. (These statements won't work if you're stuck on an old version of MySQL. The plugin contains a workaround for old versions that's a bit too complex to explain in a StackOverflow answer.)

ALTER TABLE wp_usermeta ENGINE=InnoDB, ROW_FORMAT=Dynamic;
ALTER TABLE wp_usermeta ADD UNIQUE KEY umeta_id (umeta_id);
ALTER TABLE wp_usermeta DROP PRIMARY KEY;
ALTER TABLE wp_usermeta ADD PRIMARY KEY (user_id, meta_key, umeta_id);
ALTER TABLE wp_usermeta DROP KEY user_id;
ALTER TABLE wp_usermeta DROP KEY meta_key;
ALTER TABLE wp_usermeta ADD KEY meta_key (meta_key, user_id);

This set of indexes allows your queries to retrieve those left-joined meta_value attributes much more efficiently. Why? In brief, the table needs an index on (user_id, meta_key) for the purpose. And in InnoDB, the data in the table is actually stored in the primary key's index (it's called a clustered index). So MySQL can very quickly retrieve the meta_values you need.

4
  • dumb question, but I'm not so advanced: if I want to query orders via phone number in the shop_order cpt, where should I add a primary key? in usermeta or in postmeta? secondly why hasn't the WP & WC Core committers done anything bout this?
    – RwkY
    Jan 21, 2022 at 13:40
  • I can't begin answer your first question without knowing whether the order phone numbers are associated with the order (postmeta) or the user (usermeta). At any rate, if you're "not so advanced" you should not change primary keys except on tables you're willing to lose. Read this plumislandmedia.net/index-wp-mysql-for-speed/tables_and_keys . It describes the key changes made in the WordPress plugin mentioned in my answer.
    – O. Jones
    Jan 21, 2022 at 13:55
  • As for why the core team hasn't reworked their indexes, I don't know. I hope they'll make the plugin obsolete one day by putting in better indexes. They can't rework the table definitions without breaking many many sites.
    – O. Jones
    Jan 21, 2022 at 13:57
  • yes, they are associated with the order number; and I used the plugin and it helped a lot!
    – RwkY
    Jan 31, 2022 at 8:55
1

Adding the indexes described by Rick James will make the original query much faster.

But I have managed to change it to use LEFT JOIN and this is by far the fastest and is shown here if anybody else would need it:

SELECT
  b.order_id, 
  customer_meta.meta_value as customer_id,
  u.display_name as customer_name,
  u.user_email, 
  qty_table.meta_value as qty,
  um_last_name.meta_value as last_name,
  um_first_name.meta_value as first_name,
  um_address1.meta_value as address1,
  um_address2.meta_value as address2,
  um_address_city.meta_value as city,
  um_address_state.meta_value as state,
  um_company.meta_value as company,
  um_email.meta_value as billing_email
FROM wp_woocommerce_order_itemmeta a
  LEFT JOIN wp_woocommerce_order_items b on a.order_item_id = b.order_item_id
  LEFT JOIN wp_postmeta customer_meta on customer_meta.post_id = b.order_id AND customer_meta.meta_key = '_customer_user'
  LEFT JOIN wp_users u on u.ID = customer_meta.meta_value
  LEFT JOIN wp_woocommerce_order_itemmeta qty_table on qty_table.order_item_id = b.order_item_id and qty_table.meta_key = '_qty'
  LEFT JOIN wp_usermeta um_last_name on u.ID = um_last_name.user_id and um_last_name.meta_key = 'last_name'
  LEFT JOIN wp_usermeta um_first_name on u.ID = um_first_name.user_id and um_first_name.meta_key = 'first_name'
  LEFT JOIN wp_usermeta um_address1 on u.ID = um_address1.user_id and um_address1.meta_key = 'billing_address_1'
  LEFT JOIN wp_usermeta um_address2 on u.ID = um_address2.user_id and um_address2.meta_key = 'billing_address_2'
  LEFT JOIN wp_usermeta um_address_city on u.ID = um_address_city.user_id and um_address_city.meta_key = 'billing_city'
  LEFT JOIN wp_usermeta um_address_state on u.ID = um_address_state.user_id and um_address_state.meta_key = 'billing_state'
  LEFT JOIN wp_usermeta um_email on u.ID = um_email.user_id and um_email.meta_key = 'billing_email'
  LEFT JOIN wp_usermeta um_company on u.ID = um_company.user_id and um_company.meta_key = 'billing_company'
WHERE
  a.meta_key = '_product_id' AND a.meta_value = PRODUCT_ID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.