Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

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
0
cimmer
Asked:
cimmer
2 Solutions
 
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now