Hopefully this is the right forum, my question seems to overlap the stack exchange community so this seemed best.
I have some custom reports for my WooCommerce orders on my wordpress site. I have one query that is just freezing locally, meaning in my localhost my CPU goes to 100% and it never finishes and I don’t understand why. To the point here is the query:
SELECT SUM(postmeta.meta_value)
FROM pca_postmeta AS postmeta
LEFT JOIN pca_woocommerce_order_items AS orders ON orders.order_id = postmeta.post_id
WHERE postmeta.meta_key = '_order_total'
AND orders.order_item_id IN (
SELECT item_meta.order_item_id
FROM pca_woocommerce_order_itemmeta AS item_meta
LEFT JOIN pca_woocommerce_order_items AS orders ON item_meta.order_item_id = orders.order_item_id
LEFT JOIN pca_posts AS posts ON posts.ID = orders.order_id
WHERE item_meta.meta_value = '23563'
AND posts.post_status IN ('wc-processing','wc-completed')
GROUP BY orders.order_id
)
As you can hopefully see the goal here is to get the summation of all orders from this specific campaign (23563). The nested query works exactly as expected on its own, returning just a list of IDs like so:
NOTE: little curious if 2.6289 secs is long when it only returned 65 total, although there are 148220 total
The problem is this query doesn’t seem to like the nested part. Any suggestions? Completely different approach in mind?
P.S. I use that nested query at other times as well to represent all orders by campaign id in my php reporting class. But for my question PHP has nothing to do with it.
UPDATE/FOLLOW UP:
Is it possible to convert this into a join as described here: Using a SELECT statement within a WHERE clause ? I’m a little light on my SQL so not sure how I would do that but it seems promising
does not make sense because you are selecting only
order_item_id
.pca_woocommerce_order_itemmeta
would benefit fromAn this might be an equivalent query, but avoiding the
IN(SELECT...)
:Edit
Some principles behind what I did. Here I am guessing at what the optimizer will do with various possible formulations of the query.
LEFT
— This may have changed the output. But I needed to avoidLEFT JOIN ( SELECT ... )
which would not be optimizable.WHERE
clause.WHERE pm.meta_key = '_order_total'
.WHERE im.meta_value = '23563
) provides the likely starting point for that set of JOINs. This is further enhanced by it not being ‘right’ of aLEFT JOIN
. Hence, I suggested that index.