Skip to Main Content
  • Questions
  • performance impact when you have CASE statements in group by clause

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Reji.

Asked: February 11, 2006 - 8:27 pm UTC

Last updated: March 09, 2006 - 12:30 pm UTC

Version: 9.2

Viewed 10K+ times! This question is

You Asked

Tom:

Would you please let us know how the oracle server process the query when you have CASE clauses in the group by clause?

Would you please show and explain how the oracle server will execute this query?

Also let us know what is the best way to improve this query so that the performance can be improved?

Here is an example of the query:

SELECT
ol.OrganizationLevelLabel AS ORG_LEVEL,
CASE WHEN op.D_OrganizationID > 2 THEN OrganizationDesc
WHEN op.D_OrganizationID = 0 THEN 'Unknown Consumer'
WHEN op.D_OrganizationID = 2 THEN 'Undefined Consumer'
ELSE 'Other Traffic' END AS ORGANIZATION,
SUM(op.BytesSent)/1048576.0 AS SENT_MB,
SUM(op.BytesReceived)/1048576.0 AS RECEIVED_MB,
SUM(op.BytesSent)/1048576.0 + SUM(op.BytesReceived)/1048576.0 AS TOTAL_MB
FROM
dbo.F_OrganizationProfileDaily op,
dbo.Organization o,
dbo.OrganizationLevel ol,
dbo.TimeDay t
WHERE
o.OrganizationID = op.D_OrganizationID
AND ol.OrganizationLevel = 3
AND t.ActualDate = '2004-07-04'
GROUP BY
ol.OrganizationLevelLabel,
CASE WHEN op.D_OrganizationID > 2 THEN OrganizationDesc
WHEN op.D_OrganizationID = 0 THEN 'Unknown Consumer'
WHEN op.D_OrganizationID = 2 THEN 'Undefined Consumer'
ELSE 'Other Traffic' END


and Tom said...

it will do exactly what you think it needs to do. It will evaluate the derived column made up by the CASE statement and then group by it.



I can say that '2004-07-04' is a string and I presume that ActualDate is a date - you would be well advised to use

to_date( '2004-07-04', 'YYYY-MM-DD' )

assuming that MM-DD is right, else use DD-MM if that is right. Implicit conversions should always be avoided. Explicit date format masks will save you hours of debugging in the future.

Make sure you mean to do a cartesian join to OL and T as you have coded.


Otherwise statement looks fine.

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Why did you say cartesian join here?

Reji, February 12, 2006 - 6:03 pm UTC

Tom:

Would you please explain why do u think that i am doing a cartesian join here?



Tom Kyte
February 13, 2006 - 8:06 am UTC

"U" - he or she never commented on the issue??


You join O to OP

You caresian product that join to both:

select * from ol where organizationLevel = 3
and
select * from t where actualDate = 'a very bad string'





Why a Cartesian?

Duke Ganote, February 12, 2006 - 8:48 pm UTC

Reji-- Because you have 4 tables and only 1 join condition specified:

FROM
dbo.F_OrganizationProfileDaily op,
dbo.Organization o,
dbo.OrganizationLevel ol,
dbo.TimeDay t
WHERE
o.OrganizationID = op.D_OrganizationID

There are filter, but not join, conditions for ol and t:
AND ol.OrganizationLevel = 3
AND t.ActualDate = '2004-07-04'
so they form a Cartesian join. Perhaps this is OK if your filter conditions result in only 1 row for those tables, but it seems suspicious.

Very good explanation

Reji, February 13, 2006 - 9:40 am UTC

Duke Ganote:

Thanks for your detailed explanation. I will look at the query and see how i can make some changes to improve the performance.

Sequence

Bhavin, March 08, 2006 - 10:40 am UTC

Hi Tom,
One sequence of defination is as below.
CREATE SEQUENCE RMPUSER.PROJECTS_SEQUENCE
START WITH 31
MAXVALUE 999999999999999999999999999
MINVALUE 1
NOCYCLE
CACHE 20
NOORDER;

Now this sequence has been in jump from 10 to 31 as per table where it inserts its value.

My concern is if any activity occuer which flush cache like db restart then it should be start with 21, 41 like that. But it has been started with 30.

It might be that user could use it but it deleted that row. This is also not possible since application is doing soft delete means updating delete flag since it's app requrement.

One more reason, it also not possible to remove some values using "SELECT PROJECTS_SEQUENCE.NEXTVAL FROM DUAL"
but this password has been share with me and web develper lead.

Now can you enlighten me how can I analyse where these number (11 to 29) has been lose.

Regards
Bhavin


Tom Kyte
March 09, 2006 - 12:30 pm UTC

the use of sequences implies one thing:

YOU WILL HAVE GAPS

period, they are 100% unavoidable - period.

they are expected, they will happen, don't worry about it, it is what they do.