我想写一个视图,其中一列是否有一些信息,告诉我们是否存在一些左连接行。
CREATE VIEW MyView AS
SELECT
T.column1
<...>
, T.columnN
, ??? AS XTExists
FROM MyTable AS T
LEFT JOIN MyTableExtraField AS XT ON (
<SOMECONDITION>
)
-- I hope this performs well
SELECT
*
FROM MyView
WHERE XTExists = 1
-- I hope this performs (somewhat) well too
SELECT
*
FROM MyView
WHERE XTExists = 0
我虽然在???:
你认为我应该怎么做?
修改
目前,我找到的最有效的方法(我可能错了)是,向MyTableExtraField添加一个列,默认为1并检查1。
然后使用union创建我的视图:
alter view v20130603 as
select
a.id
, 1 as b
from t20130603_a as a
inner join t20130603_b as b on (
b.id = a.id
)
where b.ex = 1
union all
select
a.id
, 0 as b
from t20130603_a as a
where not exists (
select top(1)
1
from t20130603_b as b
where b.id = a.id
)
如果我用值为1(位)的计算持久列替换列,检查1时,它的表现不太好。