optimize SQL statement

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
cimmerAsked:
Who is Participating?
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.

rdrunnerCommented:
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
cimmerAuthor Commented:
whats a table scan and how do you prevent it?
0
rdrunnerCommented:
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

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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

rdrunnerCommented:
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
apirniaCommented:
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
Scott PletcherSenior DBACommented:
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
BlackTigerXCommented:
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
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
Microsoft SQL Server

From novice to tech pro — start learning today.