日期:2014-05-18  浏览次数:20492 次

求教一SQL优化
SELECT   [SellID]
            ,[SortID]
            ,[Title]
    FROM   [TestSort].[dbo].[T_Sell]   where   title   like   '%测% '
    and   SellID   IN   (SELECT   SellID   FROM   [T_Sell_Property]   where   ValueString= '黑色 '   AND   LabelID=2)

一直说IN   效率不高,   可我现在有这样的需求,应该怎样优化呢?



------解决方案--------------------
select a.* from
(
SELECT [SellID]
,[SortID]
,[Title]
FROM [TestSort].[dbo].[T_Sell] where title like '%测% '
) a,
(
SELECT SellID FROM [T_Sell_Property] where ValueString= '黑色 ' AND LabelID=2
) b
where a.SellID = b.SellID

------解决方案--------------------
SELECT [SellID]
,[SortID]
,[Title]
FROM [TestSort].[dbo].[T_Sell] a join [T_Sell_Property] b
on a.title like '%测% ' and b.ValueString= '黑色 ' and b.LabelID=2 and a.SellID = b.SellID

------解决方案--------------------
SELECT [SellID]
,[SortID]
,[Title]
FROM [TestSort].[dbo].[T_Sell] a where title like '%测% '
and SellID exists (SELECT * FROM [T_Sell_Property] where a.sellid=sellid and ValueString= '黑色 ' AND LabelID=2)

------解决方案--------------------
刚才发的有点问题。。exists比in效率高,好多人都这么说,我没测试过,你可以试试。。
SELECT [SellID]
,[SortID]
,[Title]
FROM [TestSort].[dbo].[T_Sell] a where title like '%测% '
and exists (SELECT * FROM [T_Sell_Property] where a.sellid=sellid and ValueString= '黑色 ' AND LabelID=2)

------解决方案--------------------
SELECT [SellID]
,[SortID]
,[Title]
FROM [TestSort].[dbo].[T_Sell] a,(SELECT SellID FROM [T_Sell_Property] where ValueString= '黑色 ' AND LabelID=2) b
where title like '%测% '
and a.SellID=b.SellID