表xm_purc05結構如下:
CREATE TABLE [dbo].[xm_purc05] (
[comm_code] [nvarchar] (10) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[purc_code] [nvarchar] (12) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[purc_fact] [nvarchar] (10) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[mate_code] [nvarchar] (30) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL ,
[in_price] [decimal](16, 5) NULL ,
[price_unit] [nvarchar] (5) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL 
) ON [PRIMARY]
GO請教:因為表xm_purc05中有超過100萬的記錄,且表沒有索引,有沒有辦法優化以下視圖呀?
CREATE view v_xm_purc05_NoOrder as
--取得最大采購單號中的最貴幣別中的最大單價
select d.mate_code,d.purc_code,max(d.in_price) as in_price,d.price_unit
from xm_purc05 d with(nolock),
(
--取得最大采購單號中的最貴幣別
select a.mate_code,a.purc_code,max(price_unit) as price_unit
from(
select distinct mate_code,purc_code,price_unit
from xm_purc05 with(nolock)
where isnull(comm_code,'')='' and isnull(mate_code,'')<>'' and isnull(purc_code,'')<>''
) a,
( --取得最大采購單號
select mate_code,max(purc_code) as purc_code
from xm_purc05 with(nolock)
where isnull(comm_code,'')='' and isnull(mate_code,'')<>'' and isnull(purc_code,'')<>''
group by mate_code
) b
where a.mate_code=b.mate_code and a.purc_code=b.purc_code
group by a.mate_code,a.purc_code
) c
where isnull(d.comm_code,'')='' and isnull(d.mate_code,'')<>'' and isnull(d.purc_code,'')<>''
and d.mate_code=c.mate_code and d.purc_code=c.purc_code and d.price_unit=c.price_unit
group by d.mate_code,d.purc_code,d.price_unit