Hello All
I have a query which returns all clients in my database and the number of orders they have placed. Client details are stored in table "client" and orders they have placed are stored in "client_order".
"client" contains 725 records
"client_order" contains 930 records
Database in MySQL v4.1.11
This is my query -
select client.*, count(client_order.clientID) as num_orders
from client left join client_order
on client.clientID = client_order.clientID
group by client.clientID
-------------
725 rows in set (0.98 sec)
-------------
Any idea why this is running so slowly? Nearly a second seems to be ridiculously slow and all other queries i use execute in approx 0.01 - 0.06 seconds. If i limit the number of results returned it doesnt make any difference either.
The below query just queries the client_order table and returns the number of orders each client has ordered - it executes in 0.01 seconds.
select client_order.clientID, count(client_order.clientID) as num_orders
FROM client_order
group by client_order.clientID
order by clientID
-------------
698 rows in set (0.01 sec)
-------------
It appears to be the join thats slowing it down, is there a more efficient way of writing the query?
Thanks in advance