MySQL Forums
Forum List  »  Performance

Query Efficiency
Posted by: Erik Opmeer
Date: September 20, 2013 06:30AM

I have a question regarding query efficiency in a bigger database, in this case mySQL but could be Oracle or SQL Server or whatever.

I have a query which to me looks awfull....

Code:
Select Cust.CustID, Custname, ErrorNoIP.Errors ErrorNoIP, ErrorNoName.Errors ErrorNoName, ErrorNoX.Errors ErrorNoX, ErrorNoY.Errors ErrorNoY
From ( select CustID, CustName from TableX) as Cust
left join (select Custid, Count(*) as Errors from TableX where ErrorID = 10 Group by CustID) ErrorNoIP on Cust.CustID = ErrorNoIP.CustID
left join (select Custid, Count(*) as Errors from TableX where ErrorID = 20 Group by CustID) ErrorNoName on Cust.CustID = ErrorNoName.CustID
left join (select Custid, Count(*) as Errors from TableX where ErrorID = 32 Group by CustID) ErrorNoX on Cust.CustID = ErrorNoX.CustID
left join (select Custid, Count(*) as Errors from TableX where ErrorID = 25 Group by CustID) ErrorNoY on Cust.CustID = ErrorNoY.CustIDNow this is a sample offcourse and only has 4 errors in reality there are 25 of these subqueries

In my brain this would seem more efficient:

Code:
select CustID
, CustName
, sum(Case when ErrorID = 10 then 1 Else 0 End ) ErrorNoIP
, sum(Case when ErrorID = 20 then 1 Else 0 End ) ErrorNoName
, sum(Case when ErrorID = 32 then 1 Else 0 End ) ErrorNoX
, sum(Case when ErrorID = 25 then 1 Else 0 End ) ErrorNoY
from TableX
where ErrorID in (10,20,25,32)
Group by CustID, CustNameHowever the people that support this query are sure that the initial query is the way to go... despite its seeming inefficient way of doing things. Though they are unable/willing to share exactly why this is true.
Just for FYI the table contains A LOT of records, dont know how many but probably in excess of 100 million and growing.

So I am hoping to find someone that can enlighten me... Which query is more efficient and why?

Options: ReplyQuote


Subject
Views
Written By
Posted
Query Efficiency
2450
September 20, 2013 06:30AM
917
September 20, 2013 11:04AM
964
September 25, 2013 04:29AM
780
October 01, 2013 08:42AM
876
October 01, 2013 08:53AM
812
September 30, 2013 06:13PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.