How to get a query to run faster

I have a query that takes a while to run and sometimes times out. Is there a better way to write the query below to run faster?
SELECT a.confirm_date, a.so_br_id, a.so_id, a.so_line_nbr, a.confirm_qty, a.orig_assign_qty,
           b.qty_backordered, b.qty_ordered, b.qty_shipped, a.bo_reason_code, a.part_id, c.cust_id, b.vendor_code 
  FROM CSI.SO_CARTON_DETAIL a, csi.or_line b, csi.or_header c
    WHERE a.CONFIRM_DATE >= @variable ('Start Date')
   and a.CONFIRM_DATE <= @variable ('End Date')
  and a.so_br_id = b.so_br_id
  and a.so_id = b.so_id
  and a.so_line_nbr = b.so_line_nbr
  and a.so_br_id = c.so_br_id
  and a.so_id = c.so_id
  and b.qty_backordered > 0 
ORDER BY a.confirm_date, c.cust_id, a.so_br_id, a.so_id

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are the fields you are using for the where clause indexed??

Especially ...

Confirm_Date and all _id or _nbr fields
Jbancr1Author Commented:
To tell you the truth my knowledge hasn't reached the point of indexing but I do know it helps with the speed which is about what I know for indexing.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

since you're using business-objects, that tells me you have no abiliity to change the query.

1)  make sure stats are correct on the tables
2)  make sure the query is using indexes

if that doesn't help.
re-write the query and create a view.

SELECT   a.confirm_date, a.so_br_id, a.so_id, a.so_line_nbr, a.confirm_qty,
         a.orig_assign_qty, b.qty_backordered, b.qty_ordered, b.qty_shipped,
         a.bo_reason_code, a.part_id,
         (SELECT c.cust_id
            FROM csi.or_header c
           WHERE a.so_br_id = c.so_br_id AND a.so_id = c.so_id) cust_id,
    FROM csi.so_carton_detail a, csi.or_line b
   WHERE a.confirm_date >= :start_date
     AND a.confirm_date <= :end_date
     AND a.confirm_qty <> a.orig_assign_qty
     AND a.so_br_id = b.so_br_id
     AND a.so_id = b.so_id
     AND a.so_line_nbr = b.so_line_nbr
     AND b.qty_backordered > 0
ORDER BY a.confirm_date, 12, a.so_br_id, a.so_id

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jbancr1Author Commented:

I have ability to change the query, I ran your query above and got a error stating 'ORA-01008  Not all variables bound :- 1008'
sorry..  I changed these
   WHERE a.confirm_date >= :start_date
     AND a.confirm_date <= :end_date

change them back to
    WHERE a.CONFIRM_DATE >= @variable ('Start Date')
   and a.CONFIRM_DATE <= @variable ('End Date')

Indexing all fields that are used to search/filter or sort will improve performance...

take the time to learn how to do it in your database....  

nothing looked out of the ordinary with the query....  straight forward and simple.

You are not asking it to do anything that I see as a big problem

If the database is properly indexed, the db engine should be able to optomize the query properly.

Without proper indexes...  the db engine must scan the table rather than the indexes and that takes time

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.