I have following query.
This table has millions of rows and table is partitioned by date.
select * from test.testing d where D.code in
('123','124','136','136');
Like we have to pass 100 values. What is the best way to get the results back quickly?
Appreciate your help.
Chand.
IN / OR and by and large the same operation, for example:
SQL> set autotrace traceonly explain
SQL> select *
2 from t
3 where object_id in (123,456,789);
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3 | 345 | 290 (1)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T | 3 | 345 | 290 (1)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=123 OR "OBJECT_ID"=456 OR "OBJECT_ID"=789)
Notice the filter - the IN is mapped to OR anyway in this case.
The answer to your specific question is "it depends"
select * from test.testing d where D.code in ('123','124','136','136');
Out of the "millions of rows", how many of them have code 123,124, etc...?
If it's just a few rows, then an index on code would be a sensible option. If it's hundreds of thousands of rows, using an index on code would be a disasterous option.
Is it query that is run 100's of times per day ? Then we might to look at structures to make it more efficient ? Or it is run once per month - then maybe we dont care if it takes a while. Or is it run out on a server with lots of I/O bandwidth - then we might look at a parallel options for the SQL.