February 11, 2009 at 7:54 am
hi experts
Can anyone please tell me how to avoid Table Scan from temp db .
Tanx 😀
February 11, 2009 at 8:05 am
the same as any other table...after you create it, you'd need to create an index on the table, even if it is in tempdb. that's the only way to improve access and get an index seek or anything like that.
even then, the system will decide whether using an index is going to be more efficient than doing a table scan...if you select * from #tmp, regardless of an index, since it would need to get all columns, it might be a table scan anyway.
Lowell
February 11, 2009 at 8:22 am
Yep, it's like Lowell says. Most of the time temp tables are so small that even putting indexes on them and getting the index used in the execution plan doesn't really appreciably increase performance. It all depends of course.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 12, 2009 at 4:31 am
Tanx
I created indexes for my temp table but still the issue persists . table scan costs 100%.
Tanx 😀
February 12, 2009 at 4:43 am
a table scan isn't necessarily evil....is the select very slow or anything?
how many rows are in the temp table?
what is the CREATE TABLE statement for the table?
is there a performance issue, or are you looking at the execution plan, and it says 100% table scan, and you just want to get rid of the table scan because you know index scan and index seeks are better?
Lowell
February 12, 2009 at 5:01 am
Hi,
You can check this code.
CREATE TABLE #Temp
(
TempID INT
)
GO
CREATE INDEX ID_Temp ON #Temp(TempID)
GO
SELECT * FROM #Temp
WHERE TempID <> 7
GO
DROP TABLE #Temp
Run this query with Execution Plan and Check..
Cheers!
Sandy.
--
February 12, 2009 at 5:34 am
Can you post the query and an execution plan? That would help. Also, can you show the STATISTICS IO and STATISTICS TIME for the query?
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 12, 2009 at 6:13 am
If you are returning large number of row sets then obviously table scan is better. Don't forget to post the query and the table structure.
February 13, 2009 at 7:53 am
Eswin (2/12/2009)
TanxI created indexes for my temp table but still the issue persists . table scan costs 100%.
The optimizer is pretty darn good. table scans can often be much less expensive than using an index and then doing a bookmark lookup to get to the data. You can see this for yourself by explicitly forcing the optimizer to use the index you think it should use. Then show the actual query plan in SSMS and set statistics IO on to see which is better - the optimizers plan or yours.
FROM mytable (INDEX=theindexIthinkshouldbeused)
Note that even if you DO get a less expensive plan, you REALLY REALLY REALLY need to avoid doing this stuff in production!!!!
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply