I am trying to optimize an Oracle query. Right now it runs pretty slow since the table has ~1M records. I have two tables, the item_location table, and the tariffs table. An item_location record has a dual key, the item_no and item_loc. The tariffs records are stored with a key tariff_code which is a random three-digit identifier, with fields import_tariff and export_tariff. The item_tariff_code is the foreign key for the item’s tariffs record.
Here is an SQLFiddle for simplicity.
I am trying to find the item_no with locations that have export_tariff‘s that match two values inclusive.
For example, if I want to find items with export_tariff equal to both "1111111111" and "2222222222", it would return "12345" because these records were in the database:
item_no | item_loc | export_tariff
----------------------------------------
12345 | B1 | 1111111111
12345 | B2 | 2222222222
But it shouldn’t find "67890" because of this record:
item_no | item_loc | export_tariff
----------------------------------------
67890 | B1 | 1111111111
Since it doesn’t have export_tariff "2222222222".
I’ve added the query I’ve been using so far in the SQLFiddle.
If you cannot use temp tables, Oracle has the WITH clause that can be used in pretty much the same way:
Thanks for SQLFiddle by the way, it’s a great site.
PS: I can’t see how to avoid the scanning of the
item_locationtable twice, because you really have to do the join onitem_no. If you want to get that out of the way quickly, you can turn around the order of execution with a query like this:See it at SQLFiddle