Sql – Select statement performance

join;selectsql server

I'm having a performance issue with a select statement I'm executing.

Here it is:

SELECT     Material.*
FROM       Material 
INNER JOIN LineInfo ON Material.LineInfoCtr = LineInfo.ctr 
INNER JOIN Order_Header ON LineInfo.Order_HeaderCtr = Order_Header.ctr
WHERE   (Order_Header.jobNum = 'ttest') 
    AND (Order_Header.revision_number = 0) 
    AND (LineInfo.lineNum = 46)

The statement is taking 5-10 seconds to execute depending on server load.

Some table stats:

- Material has 2,030,xxx records.
- Lineinfo has 190,xxx records
- Order_Header has 2,5xx records.

My statement is returning a total of 18 rows containing about 20-25 fields of data. Returning a single field or all of them makes no difference. Is this performance typical? Is there something I could do to improve it?

I've tried using a sub select to retrieve the foreign key, the IN clause and I found one post where a fella said using a left outer join helped him. For me, they all yield the same 5 to 10 seconds of execution time.

This is MS SQL server 2005 accessed through MS SQL management studio. Times are the elapsed time in query analyzer.

Any ideas?

Best Answer

The first thing you should do is analyze the query plan, to see what indexes (if any) SQL Server is using.

You can probably benefit from some covering indexes in this query, since you only use columns in Lineinfo and Order_Header for the join and the query restriction (the WHERE clause).