一个sql视图速度问题
create view xxx as
select x,xx,xxx...
from Product_Info a
left join v_f_product_info_property i on a.ProductID = i.[product_info._mainid]
left join Product_Type j on a.TypeID = j.ProductTypeID
inner join Product_Spec b on a.ProductId = b.ProductId and b.Status='0'
left join Shop_Cabinet h on b.ProductspecID = h.ProductspecID
left join Product_Spec_property g on b.ProductspecID=g.MainID
当查询select * from xxx where ProductTypeID = 39时,耗时1.8秒,但如果不关联 Shop_Cabinet表,则只需要0.9秒,Shop_Cabinet表有800行记录,且都是数值型数据,总共4列,其中ProductspecID是主键,视图中所有连接用的字段均有索引。很奇怪为什么多关联的Shop_Cabinet表为什么能有这么大的影响,求教
[解决办法]
--打开"包含实际的执行计划" Ctrl + M
SET STATISTICS IO, TIME ON
--看一下两次执行的执行计划,分析一下原因。
--从你的描述看,不应该有这么大的性能差别。改成下面的试试:
CREATE VIEW xxx
AS
SELECT *
FROM Product_Info a
INNER JOIN Product_Type j
ON a.TypeID = j.ProductTypeID
INNER JOIN Product_Spec b
ON a.ProductId = b.ProductId
AND b.Status = '0'
LEFT JOIN v_f_product_info_property i
ON a.ProductID = i.[product_info_mainid]
LEFT JOIN Shop_Cabinet h
ON b.ProductspecID = h.ProductspecID
LEFT JOIN Product_Spec_property g
ON b.ProductspecID = g.MainID
GO
SET STATISTICS IO, TIME ON
SELECT * FROM xxx WHERE ProductTypeID = 39
--还是看一下IO,TIME都消耗在哪儿了;看一下执行计划,是否有问题