Performance of VIEW vs. SQL statement
Posted
by Matt W.
on Stack Overflow
See other posts from Stack Overflow
or by Matt W.
Published on 2009-12-08T15:58:38Z
Indexed on
2010/05/07
22:28 UTC
Read the original article
Hit count: 272
I have a query that goes something like the following:
select <field list>
from <table list>
where <join conditions>
and <condition list>
and PrimaryKey in (select PrimaryKey from <table list>
where <join list>
and <condition list>)
and PrimaryKey not in (select PrimaryKey from <table list>
where <join list>
and <condition list>)
The sub-select queries both have multiple sub-select queries of their own that I'm not showing so as not to clutter the statement.
One of the developers on my team thinks a view would be better. I disagree in that the SQL statement uses variables passed in by the program (based on the user's login Id).
Are there any hard and fast rules on when a view should be used vs. using a SQL statement? What kind of performance gain issues are there in running SQL statements on their own against regular tables vs. against views. (Note that all the joins / where conditions are against indexed columns, so that shouldn't be an issue.)
EDIT for clarification...
Here's the query I'm working with:
select obj_id
from object
where obj_id in(
(select distinct(sec_id)
from security
where sec_type_id = 494
and (
(sec_usergroup_id = 3278
and sec_usergroup_type_id = 230)
or
(sec_usergroup_id in (select ug_gi_id
from user_group
where ug_ui_id = 3278)
and sec_usergroup_type_id = 231)
)
and sec_obj_id in (
select obj_id from object
where obj_ot_id in (select of_ot_id
from obj_form
left outer join obj_type
on ot_id = of_ot_id
where ot_app_id = 87
and of_id in (select sec_obj_id
from security
where sec_type_id = 493
and (
(sec_usergroup_id = 3278
and sec_usergroup_type_id = 230)
or
(sec_usergroup_id in (select ug_gi_id
from user_group
where ug_ui_id = 3278)
and sec_usergroup_type_id = 231)
)
)
and of_usage_type_id = 131
)
)
)
)
or
(obj_ot_id in (select of_ot_id
from obj_form
left outer join obj_type
on ot_id = of_ot_id
where ot_app_id = 87
and of_id in (select sec_obj_id
from security
where sec_type_id = 493
and (
(sec_usergroup_id = 3278
and sec_usergroup_type_id = 230)
or
(sec_usergroup_id in (select ug_gi_id
from user_group
where ug_ui_id = 3278)
and sec_usergroup_type_id = 231)
)
)
and of_usage_type_id = 131
)
and
obj_id not in (select sec_obj_id
from security
where sec_type_id = 494)
)
© Stack Overflow or respective owner