【发布时间】:2010-12-24 11:37:57
【问题描述】:
我有一个类似于以下内容的查询:
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>)
子选择查询都有自己的多个子选择查询,我没有显示这些查询,以免语句混乱。
我团队中的一位开发人员认为视图会更好。我不同意 SQL 语句使用程序传入的变量(基于用户的登录 ID)。
对于何时应使用视图与何时使用 SQL 语句是否有任何硬性规定?在针对常规表与针对视图单独运行 SQL 语句时,存在什么样的性能增益问题。 (请注意,所有连接 / where 条件都针对索引列,因此这应该不是问题。)
为澄清而编辑...
这是我正在处理的查询:
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)
)
【问题讨论】:
标签: database performance view sql