Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:56:15+00:00 2026-06-08T16:56:15+00:00

I am trying to optimize an Oracle query. Right now it runs pretty slow

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:56:26+00:00Added an answer on June 8, 2026 at 4:56 pm

    If you cannot use temp tables, Oracle has the WITH clause that can be used in pretty much the same way:

    WITH t as (SELECT tariff_code FROM tariffs WHERE 
        (export_tariff LIKE '3916906000%' OR export_tariff LIKE '39191080%'))
    SELECT t1.item_no FROM
    (SELECT * FROM item_location il, t WHERE il.item_tariff_code = t.tariff_code) t1,
    (SELECT * FROM item_location il, t WHERE il.item_tariff_code = t.tariff_code) t2
    WHERE t1.item_no = t2.item_no AND t1.item_tariff_code <> t2.item_tariff_code
    

    Thanks for SQLFiddle by the way, it’s a great site.

    PS: I can’t see how to avoid the scanning of the item_location table twice, because you really have to do the join on item_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:

    WITH il AS (
      SELECT
        il1.item_no,
        il1.item_tariff_code AS tc1,
        il2.item_tariff_code AS tc2
      FROM item_location il1
      JOIN item_location il2
        ON il1.item_no = il2.item_no AND
           il1.item_loc <> il2.item_loc)
    SELECT item_no FROM il
    JOIN tariffs t1
      ON il.tc1 = t1.tariff_code
    JOIN tariffs t2
      ON il.tc2 = t2.tariff_code
    WHERE
      t1.export_tariff LIKE '3916906000%' AND
      t2.export_tariff LIKE '39191080%'
    

    See it at SQLFiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to optimize this query because the DB it runs against is huge
I am trying to optimize my search engine. Right now, I am running a
I'm trying to optimize a report query run on an ecommerce site. I'm pretty
I'm trying to optimize query performance and have had to resort to using optimizer
When trying to optimize a query for getting store records based on location, I
I'm trying to optimize a slow query that was generated by the Django ORM.
I am trying to optimize this query as good as possible,but still i am
I'm trying to optimize my code using Neon intrinsics. I have a 24-bit rotation
I'm currently trying to optimize my program. I have a large database which consists
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.