April 24, 2004 at 11:42 pm
Do a view execute everytime it is query by another query? Example, I have a query like this:
select ...
(select ... from MyView where ...) as field1
(select ... from MyView where ...) as field2
(select ... from MyView where ...) as field3
....
from MyView
The subquery will query from the view. The subquery is about 10 times repeated based on condition. That select statement of mine takes about 15 seconds to execute eventhough the MyView will just query and filter about 100 records only from one of its table. MyView will join 5 tables and 1 view.
Is the view is the making my query slow or the subqueries?
Thanks!
AUXilliary COMmunication 1
April 25, 2004 at 7:02 am
Worse than that, it's been my experience (in SQL Server 7) that a SELECT such as yours will create a separate instance of the view, in memory, for each named instance in the SELECT. If the views have a lot of rows, that could quickly drive your server into a "swap file" run. Perhaps they fixed this "little" problem in SQL Server 2000?
One way to get around this problem is to use CASE statments instead of SELECTs to create each field.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 25, 2004 at 2:56 pm
The select subqueries contains agregrate functions like:
(select count(*) from myview where ...) as field1
(select sum(field) from my where ...) as field2
How to use case for sql? Can you show some code?
Thanks!
AUXilliary COMmunication 1
April 25, 2004 at 9:27 pm
>(select count(*) from myview where ...) as field1
>(select sum(field) from my where ...) as field2
Your last post said that you used aggregate functions for each field. Looking at the code you posted, are the WHERE clauses all different or are they the same?
For a CASE statement example (very simple example, will get an argument from others)...
--Jeff Moden
Change is inevitable... Change for the better is not.
April 25, 2004 at 10:16 pm
Easiest way to check out what's happening is to paste the full statement into Query Analyzer and execute with "Show Execution Plan" selected, or use "SET SHOWPLAN_ALL ON" at the start of the statement.
"Show Execution Plan" will present a graphical view of the steps taken by the query processor as it determines how to execute the statement.
The "SET SHOWPLAN_ALL ON" statement returns the same information but as a series of rows that form a hierarchical tree.
Check Books Online for more detail on each.
Once you can see the execution plan you'll be able to determine what part of your query is taking the most time/resources.
--------------------
Colt 45 - the original point and click interface
April 25, 2004 at 11:13 pm
The subquery is a correlated subquery. All the where clause are different. Example:
select ...,
(select count(*) from myview where myview.id = m.id and myview.field1 = 1 and ....) as fieldname,
(select count(*) from myview where myview.id = m.id and myview.field2 > 10 and ....) as fieldname,
...
from myview as m
where ...
group by ...
AUXilliary COMmunication 1
April 25, 2004 at 11:22 pm
Can you paste the full query?
--------------------
Colt 45 - the original point and click interface
April 26, 2004 at 2:20 am
If possible I've found that it speeds things up if you can split queries like this this down, perhaps using intermediate table to hold the summary figures and then linking to them, or variables if appropriate. If you run the query and take a look at the locking do you see screenfuls of locks against tempdb? I have found that the locking sometimes uses all the memory available and by not making the query do everything at once this is averted.
J Kenny
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply