[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

optimize SQL statement

Posted on 2004-10-26
7
Medium Priority
?
387 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
Comment
Question by:cimmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Expert Comment

by:rdrunner
ID: 12412144
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
ID: 12412154
whats a table scan and how do you prevent it?
0
 
LVL 11

Accepted Solution

by:
rdrunner earned 700 total points
ID: 12412230
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:rdrunner
ID: 12412259
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
apirnia earned 324 total points
ID: 12412799
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 70

Expert Comment

by:Scott Pletcher
ID: 12413749
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
ID: 12413914
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

656 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