4 replies [Last post]
ttamniwdoog's picture
Offline
Joined: 10/28/2008
Juice: 60
Was this information Helpful?

I can't run any of the ubercart reports. I have captured an example of the query that is being executed.

SELECT u.uid, u.name, ou.billing_first_name, ou.billing_last_name,
(SELECT COUNT(DISTINCT(order_id)) FROM uc_orders as o WHERE o.uid = u.uid AND o.order_status IN ('completed')) as orders,

(SELECT count(qty) FROM uc_order_products as ps LEFT JOIN uc_orders as os ON ps.order_id = os.order_id WHERE os.order_status IN ('completed') AND os.uid) as products,

(SELECT SUM(ot.order_total) FROM uc_orders as ot WHERE ot.uid = u.uid AND ot.order_status IN ('completed')) as total,
ROUND((SELECT SUM(ot.order_total) FROM uc_orders as ot WHERE ot.uid = u.uid AND ot.order_status IN ('completed'))/(SELECT COUNT(DISTINCT(order_id)) FROM uc_orders as o WHERE o.uid = u.uid AND o.order_status IN ('completed')), 2) as average
FROM users as u LEFT JOIN uc_orders as ou ON u.uid = ou.uid WHERE u.uid > 0
GROUP BY u.uid
ORDER BY total DESC
LIMIT 0, 30

The section where I count the quantity, "count(qty)", is where it apparently gets hung up. I can run the rest of the query in 1700ms without issue if I remove that line.
Also, if I run the subquery all by itself (minus the where clause) it will run in 100ms.
I have 900 products, 63k orders and 85k records on the uc_orders_products table.

I have verified that fields are properly indexed.

Does anyone have any advice?

Thanks,
Matt

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: slow query when running reports

For starters, try taking the DISTINCT out where it says DISTINCT(order_id). I'm not entirely sure why that's there, because I didn't write the code... but order_id is always going to be unique in the uc_orders table. See if that speeds it up at all.

In general, this SQL looks like it could use some optimization... Sticking out tongue

ttamniwdoog's picture
Offline
Joined: 10/28/2008
Juice: 60
That makes sense

Thanks for the quick reply. I tried it without the DISTINCT and unfortunately, it still won't finish running. Maybe I've hit a threshold. This is a new site and I just imported all of those products, orders and customers. The reporting worked fine with just my test data but once I imported the real data, it slowed tremendously.
I'd hate to write some custom reports if someone out there has the answer.
And BTW, I have been very impressed with Ubercart so far. Great job to all those who've selflessly contributed to this community!
Thanks,
Matt

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: That makes sense

It looks like the slowest query is the one that figures out the total number of products contained in the users' combined orders. When I run that query, all of my "products" rows read 177,000 something - which is a cumulative count.

I was able to rewrite that part of the query to this:

SELECT SUM(qty) FROM uc_order_products as ps
LEFT JOIN uc_orders as os ON ps.order_id = os.order_id
WHERE os.order_status IN ('completed')
GROUP BY os.order_id

which returns the correct count, unfortunately when I add that back as a subquery of the original query I get more than 1 row returned. So I need to refine it but I think this might be the right track. Thoughts?

--
Help directly fund development: Donate via PayPal!

jrust's picture
Offline
Joined: 12/19/2007
Juice: 121
Re: slow query when running reports

Turns out the problem is easily fixed with a new index. See this issue for the patch.