?
Solved

How to get a query to run faster

Posted on 2008-11-14
7
Medium Priority
?
580 Views
Last Modified: 2013-12-19
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.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 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

0
Comment
Question by:Jbancr1
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Assisted Solution

by:RgGray3
RgGray3 earned 585 total points
ID: 22960215
Are the fields you are using for the where clause indexed??

0
 
LVL 11

Assisted Solution

by:RgGray3
RgGray3 earned 585 total points
ID: 22960233
Especially ...

Confirm_Date and all _id or _nbr fields
0
 

Author Comment

by:Jbancr1
ID: 22960573
RgGray3,
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
dbmullen earned 915 total points
ID: 22960909
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,
         b.vendor_code
    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

0
 

Author Comment

by:Jbancr1
ID: 22961114
dbmullen,

I have ability to change the query, I ran your query above and got a error stating 'ORA-01008  Not all variables bound :- 1008'
0
 
LVL 10

Assisted Solution

by:dbmullen
dbmullen earned 915 total points
ID: 22961762
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')
 

0
 
LVL 11

Assisted Solution

by:RgGray3
RgGray3 earned 585 total points
ID: 22962859
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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question