IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

BETWEEN clause in SQL


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
harijax

New User


Joined: 16 Jul 2008
Posts: 22
Location: Bangalore

PostPosted: Tue Aug 12, 2008 6:05 pm
Reply with quote

Hi,

Can somebody tell me which way is better in terms of performance and Why?

WHERE
EMPLOYEE_NO BETWEEN 10000 AND 20000

or

WHERE
EMPLOYEE_NO >= 10000 AND EMPLOYEE_NO <= 20000
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1020
Location: India

PostPosted: Tue Aug 12, 2008 7:22 pm
Reply with quote

I would say between. Why ?

1. Between is a predicate given in DB2 itself for limitation purpose. So, i think they must have coded something relating to performance. When using between it checks for the number between the 2 limitations

2. When using WHERE it has to check for 3 conditions, i think.
a) >= condition
b) <= condition
c) AND

All the things i said, are to my thinking only.

And ofcourse, i could be wrong
Sushanth
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Aug 12, 2008 8:39 pm
Reply with quote

Your answer is correct but explanation is wrong ....

BETWEEN is usually more efficient than <= predicate and the >= predicate
except when comparing a host variable to 2 columns ...

Stage 2 :

WHERE
:hostvar BETWEEN col1 and col2

Stage 1:

WHERE
Col1 <= :hostvar AND col2 >= :hostvar
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1020
Location: India

PostPosted: Wed Aug 13, 2008 10:35 am
Reply with quote

So, my 2 POINT is CORRECT.
It has to check for three conditions.
>= , <=.
Both these conditions has to be satisfied. That is taken care by AND condtion.

And i like to shorten my 1st point.
Why make query bigger, when u can make it smaller.

Alway use in-built functions.
[ Why create new functions & methods, when they already exist]
____________________________________________________________

Ashimer,

HOST VARIABLES are gonna be holding values(place holders) when the query is sent to DB2, Host variables will be replaced by values.
So, how on earth they can affect anything regarding performance.
Back to top
View user's profile Send private message
Cristopher

New User


Joined: 31 Jul 2008
Posts: 53
Location: NY

PostPosted: Wed Aug 13, 2008 2:44 pm
Reply with quote

Sushanth - as we all know Stage 1 predicate are more efficient thn Stage 2 predicate and what ashimer has suggested is the way a particular value of hostvaraible defining a predicate as Stage 1 or Stage 2.

Predicates in the format of a literal compared to a literal are Stage 2 predicates; and so are predicates that compare a host variable to a host variable and those that compare a host variable to a literal.

In case of between

COL BETWEEN value1 AND value2 is Stage 1
value BETWEEN COL1 AND COL2 is not .

where value is a constant, host variable, or special register

Hope this helps.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Aug 13, 2008 10:02 pm
Reply with quote

Hello,

Quote:
Why make query bigger, when u can make it smaller.
Do not confuse a smaller amount of code with a smaller amount of recource consumption or that "smaller is always better". That is totally wrong.

It is completely inappropriate to worry about the one-time cost of writing a well-performing query versus the forever cost of running a poorly constructed query just to "make it smaller".

Possibly there is something i misunderstand. . .
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To search DB2 table based on Conditio... DB2 1
No new posts NOT IN clause in COBOL pgm COBOL Programming 8
No new posts SUSBSCRIPT WITH SIGN IN PIC CLAUSE COBOL Programming 3
No new posts usage of CASE in WHERE clause DB2 10
No new posts Cobol redefines for Signed pictured c... COBOL Programming 4
Search our Forums:

Back to Top