Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IN vs OR of Oracle, which faster?

Tags:

I'm developing an application which processes many data in Oracle database.
In some case, I have to get many object based on a given list of conditions, and I use SELECT ...FROM.. WHERE... IN..., but the IN expression just accepts a list whose size is maximum 1,000 items.

So I use OR expression instead, but as I observe -- perhaps this query (using OR) is slower than IN (with the same list of condition). Is it right? And if so, how to improve the speed of query?

like image 619
Đinh Hồng Châu Avatar asked Jun 29 '11 01:06

Đinh Hồng Châu


People also ask

Which is faster in or or in Oracle?

Answers. Exist is more faster than IN because IN doesn't use indexes at the time of fetching but Exist uses Index at the time of fetching.

Which one is faster in Oracle?

IN operator has less complexity.. it will be faster.

Which is faster in or in SQL?

The EXISTS clause is much faster than IN when the subquery results is very large. Conversely, the IN clause is faster than EXISTS when the subquery results is very small. Also, the IN clause can't compare anything with NULL values, but the EXISTSclause can compare everything with NULLs. How can we use “between” in SQL?

Which is faster in or exists?

EXISTS Is Faster in Performance than IN.


2 Answers

IN is preferable to OR -- OR is a notoriously bad performer, and can cause other issues that would require using parenthesis in complex queries.

Better option than either IN or OR, is to join to a table containing the values you want (or don't want). This table for comparison can be derived, temporary, or already existing in your schema.

like image 118
OMG Ponies Avatar answered Sep 22 '22 08:09

OMG Ponies


In this scenario I would do this:

  1. Create a one column global temporary table
  2. Populate this table with your list from the external source (and quickly - another whole discussion)
  3. Do your query by joining the temporary table to the other table (consider dynamic sampling as the temporary table will not have good statistics)

This means you can leave the sort to the database and write a simple query.

like image 38
WW. Avatar answered Sep 24 '22 08:09

WW.