Unix Technical Forum

most preferable SQL

This is a discussion on most preferable SQL within the Oracle Miscellaneous forums, part of the Oracle Database category; --> Which is most preferable between the below SQLs? In terms of consuming resources or fast execution. 1. Update Table1 ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 12:15 PM
Amritha.Datta@gmail.com
 
Posts: n/a
Default most preferable SQL

Which is most preferable between the below SQLs? In terms of consuming
resources or fast execution.

1. Update Table1 set Reason = 'SX#' where
(Sex <> 'M' or
Sex <> 'F' or
trim(Sex) <> '') and
Key = inKey And
Sub_Key = inSubKey and
Reason is NULL;

2. Update Table1 set Reason = 'SX#' where
(Sex not in ( 'M', 'F','') and
Key = inKey And
Sub_Key = inSubKey and
Reason is NULL;

I am calling this SQL in the stored procedure. If there is another
better way of writing SQL for the above query, please let me know.

Thanks in advance.

Amrith

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 12:15 PM
Anurag Varma
 
Posts: n/a
Default Re: most preferable SQL

On Feb 8, 8:52 am, Amritha.Da...@gmail.com wrote:
> Which is most preferable between the below SQLs? In terms of consuming
> resources or fast execution.
>
> 1. Update Table1 set Reason = 'SX#' where
> (Sex <> 'M' or
> Sex <> 'F' or
> trim(Sex) <> '') and
> Key = inKey And
> Sub_Key = inSubKey and
> Reason is NULL;
>
> 2. Update Table1 set Reason = 'SX#' where
> (Sex not in ( 'M', 'F','') and
> Key = inKey And
> Sub_Key = inSubKey and
> Reason is NULL;
>
> I am calling this SQL in the stored procedure. If there is another
> better way of writing SQL for the above query, please let me know.
>
> Thanks in advance.
>
> Amrith



Those two statements are not equivalent.

ORA92> select 1 from dual where 'A' not in ('','F');

no rows selected

ORA92> select 1 from dual where ('A' <> 'F' or 'A' <> '');

1
----------
1

Be careful when dealing with nulls.
And once you sort out the null issue in your queries,
try testing them yourself to find the answer.

Anurag

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 12:16 PM
Amritha.Datta@gmail.com
 
Posts: n/a
Default Re: most preferable SQL

On Feb 8, 9:24 am, "Anurag Varma" <avora...@gmail.com> wrote:
> On Feb 8, 8:52 am, Amritha.Da...@gmail.com wrote:
>
>
>
> > Which is most preferable between the below SQLs? In terms of consuming
> > resources or fast execution.

>
> > 1. Update Table1 set Reason = 'SX#' where
> > (Sex <> 'M' or
> > Sex <> 'F' or
> > trim(Sex) <> '') and
> > Key = inKey And
> > Sub_Key = inSubKey and
> > Reason is NULL;

>
> > 2. Update Table1 set Reason = 'SX#' where
> > (Sex not in ( 'M', 'F','') and
> > Key = inKey And
> > Sub_Key = inSubKey and
> > Reason is NULL;

>
> > I am calling this SQL in the stored procedure. If there is another
> > better way of writing SQL for the above query, please let me know.

>
> > Thanks in advance.

>
> > Amrith

>
> Those two statements are not equivalent.
>
> ORA92> select 1 from dual where 'A' not in ('','F');
>
> no rows selected
>
> ORA92> select 1 from dual where ('A' <> 'F' or 'A' <> '');
>
> 1
> ----------
> 1
>
> Be careful when dealing with nulls.
> And once you sort out the null issue in your queries,
> try testing them yourself to find the answer.
>
> Anurag


Thanks Anurag. Now I understand the difference.
If I use 'and' instead of 'or which query is efficient?

1. Update Table1 set Reason = 'SX#' where
(Sex <> 'M' and
Sex <> 'F' and
trim(Sex) <> '') and
Key = inKey And
Sub_Key = inSubKey and
Reason is NULL;

2. Update Table1 set Reason = 'SX#' where
Sex not in ( 'M', 'F','') and
Key = inKey And
Sub_Key = inSubKey and
Reason is NULL;


Thank you.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 12:16 PM
Anurag Varma
 
Posts: n/a
Default Re: most preferable SQL

On Feb 8, 9:33 am, Amritha.Da...@gmail.com wrote:
> On Feb 8, 9:24 am, "Anurag Varma" <avora...@gmail.com> wrote:
>
>
>
> > On Feb 8, 8:52 am, Amritha.Da...@gmail.com wrote:

>
> > > Which is most preferable between the below SQLs? In terms of consuming
> > > resources or fast execution.

>
> > > 1. Update Table1 set Reason = 'SX#' where
> > > (Sex <> 'M' or
> > > Sex <> 'F' or
> > > trim(Sex) <> '') and
> > > Key = inKey And
> > > Sub_Key = inSubKey and
> > > Reason is NULL;

>
> > > 2. Update Table1 set Reason = 'SX#' where
> > > (Sex not in ( 'M', 'F','') and
> > > Key = inKey And
> > > Sub_Key = inSubKey and
> > > Reason is NULL;

>
> > > I am calling this SQL in the stored procedure. If there is another
> > > better way of writing SQL for the above query, please let me know.

>
> > > Thanks in advance.

>
> > > Amrith

>
> > Those two statements are not equivalent.

>
> > ORA92> select 1 from dual where 'A' not in ('','F');

>
> > no rows selected

>
> > ORA92> select 1 from dual where ('A' <> 'F' or 'A' <> '');

>
> > 1
> > ----------
> > 1

>
> > Be careful when dealing with nulls.
> > And once you sort out the null issue in your queries,
> > try testing them yourself to find the answer.

>
> > Anurag

>
> Thanks Anurag. Now I understand the difference.
> If I use 'and' instead of 'or which query is efficient?
>
> 1. Update Table1 set Reason = 'SX#' where
> (Sex <> 'M' and
> Sex <> 'F' and
> trim(Sex) <> '') and
> Key = inKey And
> Sub_Key = inSubKey and
> Reason is NULL;
>
> 2. Update Table1 set Reason = 'SX#' where
> Sex not in ( 'M', 'F','') and
> Key = inKey And
> Sub_Key = inSubKey and
> Reason is NULL;
>
> Thank you.



Well you understand the difference .. but now you made both
queries get the SAME wrong result.

You might want to read this:
http://www.psoug.org/reference/null.html

That said, I cannot give the answer which query would
be efficient. In fact if the queries are equivalent, then
oracle might rewrite one to be equal to another.

Run both queries and find out which one is efficient.

Anurag

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:37 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com