Home » RDBMS Server » Performance Tuning » Solution Required for Sql Performance issue in oracle due to Null value check(2 Merged) (Oracle 10gR2)
Solution Required for Sql Performance issue in oracle due to Null value check(2 Merged) [message #509912] Wed, 01 June 2011 10:21 Go to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
Hi All,

I have a Performance issue (time taken is very high)with an sql query.
The query cost is very high and time taken is inacceptible & i found that the reason was the check for null value in the query .
The tables contain more than 100 million records each.
The basic structure of the query is ;

select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ((a.flag <> '10' and a.flag <> '15') or a.flag is null)

I had a few workaround for this but all this did not reduce the time taken. I will enumerate what all i did for a solution :

1. normal index on flag
create index test_flag on table1 (flag)

result : No difference in time taken (index not used)

2. nvl index on flag col and query change

create index test_flag on table1 (nvl(flag,'NA'))

select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ((nvl(a.flag,'NA') <> '10' and (nvl(a.flag,'NA') <> '15') )

result : No difference in time taken and index is getting used

3. create index test_flag on table1 (flag)

select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ( (a.flag > '10' or a.flag < '10')
and (a.flag > '15' or a.flag > '15')or a.flag is null )

result : No difference in time taken ,index is not used)
if ' or' condition is removed then the index is getting used.

Pls help me since this is a very critical issue in the production environment.
If proper values are substituted for flag eg- a.flag='10' then the query is faster.

regards
Ganesh

Solution Required for Sql Performance issue in oracle due to Null value check [message #509913 is a reply to message #509912] Wed, 01 June 2011 10:23 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
Hi All,

I have a Performance issue (time taken is very high)with an sql query.
The query cost is very high and time taken is inacceptible & i found that the reason was the check for null value in the query .
The tables contain more than 100 million records each.
The basic structure of the query is ;

select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ((a.flag <> '10' and a.flag <> '15') or a.flag is null)

I had a few workaround for this but all this did not reduce the time taken. I will enumerate what all i did for a solution :

1. normal index on flag
create index test_flag on table1 (flag)

result : No difference in time taken (index not used)

2. nvl index on flag col and query change

create index test_flag on table1 (nvl(flag,'NA'))

select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ((nvl(a.flag,'NA') <> '10' and (nvl(a.flag,'NA') <> '15') )

result : No difference in time taken and index is getting used

3. create index test_flag on table1 (flag)

select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ( (a.flag > '10' or a.flag < '10')
and (a.flag > '15' or a.flag > '15')or a.flag is null )

result : No difference in time taken ,index is not used)
if ' or' condition is removed then the index is getting used.

Pls help me since this is a very critical issue in the production environment.
If proper values are substituted for flag eg- a.flag='10' then the query is faster.

regards
Ganesh

Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509914 is a reply to message #509912] Wed, 01 June 2011 10:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version, with 4 decimals.

For performances question, Please read http://www.orafaq.com/forum/mv/msg/84315/433888/102589/#msg_433888 and post the required information.

Regards
Michel
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509916 is a reply to message #509912] Wed, 01 June 2011 10:25 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
What %age of rows are being returned?

You really need the explain plans and other detail included.

I'd expect any "OR x IS NULL" to not use a B-Tree index.
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509918 is a reply to message #509916] Wed, 01 June 2011 10:29 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
How many rows where flag is null?
How many rows where flag is 10 or 15?
How many rows does the query return?
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509919 is a reply to message #509916] Wed, 01 June 2011 10:30 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Since NOBODY can optimize SQL just by looking at it, we need a few more details.
http://www.orafaq.com/forum/m/433888/136107/#msg_433888
Please refer to URL above & be sure to provide the details requested:
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509920 is a reply to message #509916] Wed, 01 June 2011 10:35 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
The result set is approx 70000 rows (total rows approx 100 million)
regards
ganesh
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509921 is a reply to message #509918] Wed, 01 June 2011 10:37 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
hi ,
How many rows where flag is null? 60,000
How many rows where flag is 10 or 15? flag '10': 5000 rows ,flag '15': 5000 ros
How many rows does the query return? total returns 70000 rows

regards
ganesh
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509923 is a reply to message #509921] Wed, 01 June 2011 10:40 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
hi,
i also tried using a bitmap index on the flag col but it does not seem to do any good for the response time.moreover the flag field gets updated always

regards
ganesh

Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509924 is a reply to message #509921] Wed, 01 June 2011 10:42 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
According to those figures this query should return 70000 rows:
select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ((a.flag = '10' or a.flag = '15') or a.flag is null)


But that's not what you are running. If this is right:
Quote:

How many rows where flag is null? 60,000
How many rows where flag is 10 or 15? flag '10': 5000 rows ,flag '15': 5000 ros

Then your query should return just under 100 million rows. Which will take a while.

EDIT: typo

[Updated on: Wed, 01 June 2011 10:42]

Report message to a moderator

Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509925 is a reply to message #509924] Wed, 01 June 2011 10:47 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Apologies I forgot to factor in the other table.
How many rows in a? How many rows in b?
How many rows does this return:
select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509926 is a reply to message #509924] Wed, 01 June 2011 10:49 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
yea iam not running that( there is an 'and' condition & not an 'or between '10 and '15') and any change in the logic does not fetch the 70000 records
select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ((a.flag = '10' AND a.flag = '15') or a.flag is null)
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509928 is a reply to message #509926] Wed, 01 June 2011 10:57 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
hi it's ok
.
The actual query structure :
select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ((a.flag <> '10' AND a.flag <> '15') or a.flag is null)

table1 has 10000000 (100 million)
table2 has equal no of recs 10000000(100 million)
result set is 70000 (70 thousand recs)


Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509929 is a reply to message #509926] Wed, 01 June 2011 10:59 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Since flag can't be 10 and 15 simultaneously I doubt you are running that query either.

This is the query you said you were running:
select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
and ((a.flag <> '10' and a.flag <> '15') or a.flag is null)

So, ignoring b for a second, that returns every row in a where flag is not 10 and flag is not 15.
Which is to say it returns every row in table a except the rows where flag is 10 or 15.
You have 10000 rows where flag is 10 or 15 (5000 + 5000).
So that query (ignoring b) will return: 100,000,000 - 10,000 = 999,990,000 rows.

So unless those 999,990,000 rows in a have exactly 70000 matching rows in b, your figures are way off.

You appear to have taken the number of rows where flag is null and added the number of rows where flag is 10 and flag is 15 to get the number of rows the query will return. But that's not what your query is doing.
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509930 is a reply to message #509928] Wed, 01 June 2011 10:59 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
hi the query
select a.*, b.* from table1 a , table2 b
where a.primary_key=b.primary_key
retrieves the 100 million recs

Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509931 is a reply to message #509929] Wed, 01 June 2011 11:01 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Lets see a copy and paste of the execution of this query from sqlplus:
select count(*) from table1 a , table2 b
where a.primary_key=b.primary_key
and ((a.flag <> '10' AND a.flag <> '15') or a.flag is null);


Cause I really don't believe the number of rows is 70,000.

And can you please read this thread on using code tags, and use them in your future posts.
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509932 is a reply to message #509930] Wed, 01 June 2011 11:04 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
yea i have to query the records that appear in table1 which have flag value not equal to '10' and '15' or where the flag value is blank/null.
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509933 is a reply to message #509932] Wed, 01 June 2011 11:06 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
also there are other values of flag col in the table1 and only those that are null or not equal to '10' and '15' are picked up.
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509934 is a reply to message #509932] Wed, 01 June 2011 11:11 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
ganeshkn21 wrote on Wed, 01 June 2011 17:04
yea i have to query the records that appear in table1 which have flag value not equal to '10' and '15' or where the flag value is blank/null.


I know that. What I'm telling you is that if there are 100 million records in the table, and there are only 10 thousand rows where flag is '10' or '15' then such a query will return 999, 990, 000 rows. which is 10 thousand less than 100 million.
Not 70000.

The problem is nothing to do with nulls, or indexes.
The problem is that the query returns
999,990,000 / 70,000 = 14,285 times more rows than you think it does.
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509935 is a reply to message #509933] Wed, 01 June 2011 11:13 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
hi i think i should have coorected the previous reply as ,
How many rows where flag is null? 60,000
How many rows where flag is 10 or 15? flag <> '10': 5000 rows ,flag <> '15': 5000 rows
How many rows does the query return? total returns 70000 rows

i think now u are clear with the situation

regards
ganesh
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509936 is a reply to message #509935] Wed, 01 June 2011 11:17 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
sorry friend i made a mistake
pls read as flag <> '10' & flag <> '15': combiined condition fetches 10000 rows
The total rows fetched by the combined condition (flag <>'10' and flag <> '15' ) is 10000
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509937 is a reply to message #509936] Wed, 01 June 2011 11:18 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
i have not checked the values separately for '10' and '15' since it is part of the logic
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509938 is a reply to message #509936] Wed, 01 June 2011 11:18 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/t/88153/0/ => do #7 -> SHOW Us!
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509939 is a reply to message #509935] Wed, 01 June 2011 11:21 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Well yes, your numbers more or less add up now.
So there are approx 99 million rows where flag is 10 or 15 correct?
I now suggest you follow the link Michel and Blackswan posted above and provide the required information - the explain plan would be a good start.
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509940 is a reply to message #509938] Wed, 01 June 2011 11:21 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
hi,
ok i will post the explain plan by tomorrow for a clear understanding .but this is how it is !
regards
Ganesh
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #509941 is a reply to message #509940] Wed, 01 June 2011 11:21 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
the issue!
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #510302 is a reply to message #509941] Fri, 03 June 2011 09:33 Go to previous messageGo to next message
ganeshkn21
Messages: 36
Registered: June 2011
Location: bangalore
Member
hi iam not able to put the plan table o/p due to security policies
Re: Solution Required for Sql Performance issue in oracle due to Null value check [message #510312 is a reply to message #510302] Fri, 03 June 2011 09:47 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>hi iam not able to put the plan table o/p due to security policies
We are not able to offer more advice.
Previous Topic: Tuning Merge Statetment
Next Topic: Tuning Select Not IN statement(2 Merged)
Goto Forum:
  


Current Time: Thu Apr 25 13:02:49 CDT 2024