Solved

optimize SQL statement

Posted on 2004-10-26
367 Views
Last Modified: 2008-02-01
is there a way to optimize this sql to run faster or query faster?? or is this the fastest way?


SELECT DISTINCT top 500
super.sup_cat_id AS supid,
sub.prod_id, inv.ID, inv.pattern_num, inv.closeout, inv.theme, inv.msrp, inv.price, inv.newfull, inv.categoryID FROM sub_prod_cat sub
INNER JOIN Inventory inv ON inv.ID = sub.prod_id
INNER JOIN super_sub_cat super ON sub.sub_cat_id = super.sub_cat_id
WHERE inv.hybernate<>'CHECKED' AND super.sup_cat_id=538 ORDER BY inv.closeout desc
0
Question by:cimmer
    7 Comments
     
    LVL 11

    Expert Comment

    by:rdrunner
    Hello

    The statement itself does not have any performance issues. To know what could be optimized here you need to have a look at the execution plan and make sure that there are no table scans and that there is an index on all foreign keys for this querry and on the the inv.hybernate field.

    If you have those the querry will be as fast as possible
    0
     

    Author Comment

    by:cimmer
    whats a table scan and how do you prevent it?
    0
     
    LVL 11

    Accepted Solution

    by:
    Well a table scan is evil...

    A table scan will happen if you have no where clause or a where clause that is not covered by an index. If you have a persons table and there is an age field but no index on the age field then this will result in a table scan

    select * from persons where age = 30

    Basically the server has no clue which age anyone in this ttable is and now he has to check (scan) every person in the table  an remember everyone who is 30.

    This will cause extreme performance issues since he is doing a lot of work here... If you have an index on the age field then the server knows which "99 persons" are 30 and can fetch those directly from his bookmarks (You will see an Index seek + bookmark lookup in the execution plan)

    Those things can really help your querry esp. when more then 1 person is accessing the server.

    Paste that querry into the QA and turn the execution plan on... and look at all the objects there...
    0
     
    LVL 11

    Expert Comment

    by:rdrunner
    Or a better example would be if i give you the telephone book from NYC and ask you to get me all phonnumbers of someone with the FIRST name "John"

    The Phonebook is indexed on the last name (at least in my country) and you have no clue where to find someone with the 1st "John" so you have to read the whole book

    :)
    0
     
    LVL 9

    Assisted Solution

    by:apirnia
    Query itself looks good. If you want your query to run faster you can Index your tables if they are not indexed. This will usually make a big difference.

    Take a look at this link to learn some more about Indexes:

    http://www.experts-exchange.com/Databases/Q_20839511.html?query=&clearTAFilter=true


    To create index on your tables >> right click on the table and then >> All tasks >> manage indexes.

    One other thing you can do if you have complex query open QUERY ANALYZER and run the Query there and then press  Ctrl + L  this shows the execution plan for the query and the loss at each point. based on that you can optimize the query
    0
     
    LVL 68

    Expert Comment

    by:ScottPletcher
    This might be better, depending on the various table sizes.  Find all relevant rows in the super_sub_cat table prior to joining to the other tables:


    SELECT DISTINCT top 500
        super.sup_cat_id AS supid,
        sub.prod_id, inv.ID, inv.pattern_num, inv.closeout, inv.theme, inv.msrp, inv.price,
        inv.newfull, inv.categoryID
    FROM sub_prod_cat sub
    INNER JOIN Inventory inv ON inv.ID = sub.prod_id
    INNER JOIN (
        SELECT sub_cat_id
        FROM super_sub_cat
        WHERE sup_cat_id = 538
    ) AS super ON sub.sub_cat_id = super.sub_cat_id
    WHERE inv.hybernate<>'CHECKED' AND super.sup_cat_id=538
    ORDER BY inv.closeout desc
    0
     
    LVL 13

    Expert Comment

    by:BlackTigerX
    nolock... =o)

    SELECT DISTINCT top 500
    super.sup_cat_id AS supid,
    sub.prod_id, inv.ID, inv.pattern_num, inv.closeout, inv.theme, inv.msrp, inv.price, inv.newfull, inv.categoryID FROM sub_prod_cat sub (nolock)
    INNER JOIN Inventory inv (nolock) ON inv.ID = sub.prod_id
    INNER JOIN super_sub_cat super (nolock) ON sub.sub_cat_id = super.sub_cat_id
    WHERE inv.hybernate<>'CHECKED' AND super.sup_cat_id=538 ORDER BY inv.closeout desc
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I recently came across an interesting Question In EE (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/Q_27070749.html?cid=1135#a35876665) and was puzzled about how to achieve that using SSIS out of the box tasks, which was i…
    I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    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.

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now