SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using a View in aa query is slower than using the table directly


Using a View in aa query is slower than using the table directly

Author
Message
dazmaul
dazmaul
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 13
Hi all,

I have a query that basically links together three Tables and a View which has started running extremely slowly... it used to be OK but is now tediously slow.

The View is simply a table separation - the table used contains a list of data each with an assigned "ID" of which multiple "ID" entries can exist with a different "Type" column value i.e. Count, Description, Cost. The table has an index which uses both fields. The View just selects ALL entries where the "Type" column value = "Count".

The three tables and the view are linked using LEFT OUTER JOINs on a common field from each table - which runs slow.

BUT if I remove the view and link directly to the table and apply the WHERE [Type]='Count' to the query it runs almost instantly...

My question is how/why is SQL treating the two differently? It's almost like the index from the table used in the view is not used - is this to be expected?

Any comments/help always welcomed.

Regards
dazmaul
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66969 Visits: 18570
Have you checked the execution plan for both methods?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

dazmaul
dazmaul
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 13
Thanks for the advice SQLRNNR, I have checked the Execution Plans and it appears that the first method using the view generates an extra Hash Table when joining the View to the first table... which I have just noticed is actually an Inline Table Valued Function which uses a parameter to return only entries with a specific ID.

If I take this out and just specify the table referenced directly and apply parameter as a WHERE condition directly it runs quickly even when referencing the view as well...

Again, I though the Inline Table Valued Functions would still operate as a View does OR is that not the case?
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66969 Visits: 18570
It depends on how it is being used in the query. Also, is it possible that it is a multistatement instead of an iTVF?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13340 Visits: 4077
post the definition of all related queries along with view

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search