Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 sql view optimization help

Author  Topic 

tandreasen
Starting Member

5 Posts

Posted - 2011-07-11 : 13:54:36
I have a view that basically queries against several tables, it happens to have a nested SELECT statement:

select ..., CAST(COALESCE ((SELECT COLUMN
FROM SOMETABLE
WHERE (SOMEOTHERCOLUMN = 'SomeValue')), 4) AS bigint) AS MyColumnVariable, ... other columns..

Unfortunately, I do not think this is optimal as every row will execute this, regardless of the fact that it will always be the same value for each execution (it is a static variable from a settings table).

How can I optimize this view to basically perform the query only one time on my SOMETABLE that will never change across multiple rows?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-11 : 14:07:50
Execute it first, put it into a variable, and then use the variable in your query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-11 : 14:09:01
DECLARE @i bigint

select @i = col1 from t1 where ...

select ..., @i as MyColumnVariable, ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tandreasen
Starting Member

5 Posts

Posted - 2011-07-11 : 14:21:26
This is very helpful and should work perfectly, but how can I do all of this inside of a view? It doesn't like the declaration of variables, it seems..

EDIT: Guess I could just create a function.
EDIT2: But, if I use a function, then how can I prevent it from executing that same query per each row (back to the original problem)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-07-11 : 14:27:24
SELECT FROM myView
WHERE col = @localvar



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-11 : 14:30:05
Add the variable when you query the view, do not use a function if you care about performance.

DECLARE @i bigint

select @i = col1 from t1 where ...

select ..., @i as MyColumnVariable, ...
from viewName
...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tandreasen
Starting Member

5 Posts

Posted - 2011-07-11 : 14:36:33
Got it, thanks for the help.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-11 : 14:39:25
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -