我有以下查询,它的工作原理。但由于它的积累,它需要花费很长的时间来处理。因此,我需要帮助才能更快地获得此查询。
在查询中,PRODUCT_ID应替换为' '和产品ID号。
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
我需要所有信息,因为我想列出已购买的给定产品的所有用户的名字,姓氏,公司,地址,邮政编码等。因此查询本身可行,但在处理时间方面是一个杀手。
我可以使用max(CASE WHEN ...... ... THEN ... END)作为a_name,但如果我使用左连接,我只知道如何成功。
有关如何让此查询更好地运行的任何提示?
答案 0 :(得分:0)
首先,EAV是一种糟糕的架构设计。但我不会咆哮。我只是指出可能缺失或格式错误的索引:
wp_usermeta: PRIMARY KEY(user_id, meta_key)
没有任何(191)
加上。
同样适用于wp_woocommerce_order_itemmeta
。
我可能会受到更多的虐待;请为正在使用的表提供SHOW CREATE TABLE
。
答案 1 :(得分:0)
添加Rick James描述的索引将使原始查询更快。
但是我设法改变它以使用LEFT JOIN,这是迄今为止最快的,如果有人需要它,这里显示:
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