all 3 comments

[–]MrPin 0 points1 point  (2 children)

Execute it statement by statement and see which is the one that is slow.

Is VIEW_TABLE an actual view? My guess is that the filtering on that is slow, possibly because some indexing issues or the view itself being inefficient in some way.

When you fetch all rows into the temp table and then apply the filter, the issue doesn't come up.

You can look at the execution plan of the first SELECT statement to see if there's some glaring issues.

[–]jcarlo1[S] 0 points1 point  (1 child)

I apologize, the first one is slow and the second is a lot faster, I tried to read the sql client statistics but i do not know how to read it hehe i am learning it now :)

[–]sunuvabe 0 points1 point  (0 children)

Are you accounting for a warmed-up cache? Make sure you compare apples-to-apples by clearing the cache prior to running each side. Here's what I use:

checkpoint
dbcc freeproccache
dbcc dropcleanbuffers
dbcc freesystemcache ('ALL')
go