0
SELECT *
FROM
(
    SELECT A_WEBC_URL AS FILE_LOCATION
    FROM BATELCO_BILLS_S
    WHERE BTL_BILL_I_NUMBER = :B2 AND BTL_BILL_I_ACC_NUMBER = '0' || :B1 
    UNION
    SELECT A_WEBC_URL AS FILE_LOCATION
    FROM BATELCO_BILLS_S
    WHERE BTL_BILL_I_NUMBER = :B2 AND (BTL_BILL_I_PROFILE_ID = :B1 OR
                                       BTL_BILL_I_PHONE_NUMBER = :B3 ) 
    UNION
    SELECT A_WEBC_URL AS SS
    FROM BATELCO_BILLS_S BILLS
    WHERE BILLS.BTL_BILL_I_PROFILE_ID = :B1 OR
        BTL_BILL_I_ACC_NUMBER = '0' || :B1 AND
        BILLS.BTL_BILL_I_NUMBER = :B2
)
WHERE ROWNUM = 1

The above query taking time to consume.Please help to optimize the code so that we can fetch the results easily.

3
  • This is another query please help Commented Feb 20, 2018 at 7:32
  • I don't see the point of the unions, which all select the same column from the same table. Why do you think you need them? Commented Feb 20, 2018 at 7:36
  • Can't you replace the UNIONs with simple OR logic? If not can you use UNION ALL instead to remove the sort but permit duplicates? Commented Feb 20, 2018 at 7:46

2 Answers 2

3

I don't see the point of the unions, and I think we can rewrite the subquery without unions:

SELECT *
FROM
(
    SELECT A_WEBC_URL AS FILE_LOCATION
    FROM BATELCO_BILLS_S
    WHERE BTL_BILL_I_NUMBER = :B2 AND
         (BTL_BILL_I_ACC_NUMBER = '0' || :B1 OR
          BTL_BILL_I_PROFILE_ID = :B1 OR
          BTL_BILL_I_PHONE_NUMBER = :B3)
)
WHERE ROWNUM = 1

Note: I think the third query in your original union is missing parentheses around the first two terms in the WHERE clause. That is, I think you intended the following logic:

...
UNION
SELECT A_WEBC_URL AS SS
FROM BATELCO_BILLS_S BILLS
WHERE (BILLS.BTL_BILL_I_PROFILE_ID = :B1 OR
    BTL_BILL_I_ACC_NUMBER = '0' || :B1) AND
    BILLS.BTL_BILL_I_NUMBER = :B2
Sign up to request clarification or add additional context in comments.

1 Comment

Although it has no impact on performance, the subquery is not needed.
1

This query should run very fast, 3 unions are not needed but they shouldn't affect performance much. And you don't have any nested loops or joined tables in your query.

If you have performance problems, I think you just need indexes on this table. All of your WHERE conditions require checking of BTL_BILL_I_NUMBER field, so this query will run fast only if this column is indexed.

Do you have this column indexed?

If not, then you (or your DBA) should run:

CREATE INDEX IND_BTL_BILL_I_NUMBER ON BATELCO_BILLS_S (BTL_BILL_I_NUMBER);

If you don't see immediate effect, you (or your DBA) should gather Oracle optimizer stats for this table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.