• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SQL Query - "in" clause vs "="

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:

I was wondering whether "IN" clause is faster or "EQUAL" is faster. For ex:

Query #1
SELECT * FROM dept
WHERE dept_name = 'SALES'

Query #2
SELECT * FROM dept
WHERE dept_name in ('SALES')

In some scneario, I need to pass multiple dept_name, in that case I need to use "IN" clause.

I was wondering if there is any performance impact if I use "IN" clause even though I am passing one dept name?

Thanks in advance.

SK
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,
In terms of performance, both should be the same. I have tried it in db2 and Oracle, so I know that the optimizer does the same thing in those databases.

Having said that, you should use a prepared statement so you gain the efficiency when repeating the same number of in clause parameters.
For example, for one dept, your string would be:
SELECT * FROM dept WHERE dept_name in (?)
For three depts, your string would be:
SELECT * FROM dept WHERE dept_name in (?,?,?)
and so forth.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess it's rdbms-dependent.

And you may replace:



It should be easy to write a performance-test for your rdbms.
 
Jeanne Boyarsky
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The examples that Stefan posted also perform the same in DB2 and Oracle.
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic