高手进来,关于SQL语句的优化。
下面这几条语句都是我“事件探测器”抓出来的,发现这几条语句在CQU那列都是大与500的。大家帮忙看下有什么办法可以优化,提高速度的吗?
--SELECT F.*,S.FSubSysID FROM t_FuncControl F,t_SysFunction S WHERE F.FFuncID = S.FFuncID AND (F.FFuncID IN (SELECT FFuncID FROM t_Mutex Where FType = 8) OR (FYear = 2005 AND F.FFuncID IN (SELECT FFuncID FROM t_Mutex Where FType = 4 AND FForbidden = 21)) OR F.FFuncID IN (SELECT FFuncID FROM t_Mutex WHERE FType = 2 AND FForbidden = 21) OR (FYear = 2005 AND FPeriod = 6 AND F.FFuncID IN (SELECT FFuncID FROM t_Mutex Where FType = 9 AND FForbidden = 21)) OR (FRowID = 137555 AND F.FFuncID IN (SELECT FFuncID FROM t_Mutex WHERE FType = 10 AND FForbidden IN (SELECT FFuncID FROM t_SysFunction WHERE FNumber = 'Cc0244'))) OR (8 IN (SELECT FType FROM t_Mutex WHERE FFuncID = 20244) And FStation <> 'LIQIHUA') OR (Exists(SELECT FFuncID From t_Mutex Where FFuncID = 20244 AND FType = 2 AND FForbidden = 21) And F.FStation <> 'LIQIHUA' And S.FSubSysID = 21) OR (Exists(SELECT
FFuncID From t_Mutex Where FFuncID = 20244 AND FType = 4 AND FForbidden = 21) and .FStation <> 'LIQIHUA' AND F.FYear = 2005) OR (Exists(SELECT FFuncID From t_Mutex Where FFuncID = 20244 AND FType = 9 AND FForbidden = 21) And F.FStation <> 'LIQIHUA' AND F.FPeriod = 6) OR F.FFuncID IN (SELECT FFuncID FROM t_Mutex WHERE FType = 1 AND Forbidden IN (SELECT FFuncID FROM t_SysFunction WHERE FNumber = 'Cc0244')))
--Select t1.FFieldName as FSourceFieldName, t3.FBrNo,t3.FID,t3.FROB,t3.FName AS FName,t3.FType,t3.FTemplateID,t3.FVchTemplateID,t3.FHeadTable,t3.FEntryTable ,t3.FCheckPRO,t3.FFormWidth,t3.FFormHeight,t3.FFixCols,t3.FAllowDefined,t3.FBillFlow From ICTemplate t1 Inner Join ICListTemplate t2 On t1.FSelBill=t2.FID Inner Join ICTransactionType t3 On t1.FID=t3.FTemplateID Where t1.FVisForBillType<>0 AND t2.FID in (11) ORDER BY t3.FName
--Select * From (Select distinct t_Item.* From t_Item ,(Select i.FItemID FItemID1 ,i.FNumber FNumber1 From t_ItemRight r,t_Item i Where FTypeID=1 AND r.FItemID=i.FItemID AND (FUserID =22 OR FUserID=16708)) ir Where (FNumber=ir.FNumber1
OR(FNumber<>ir.FNumber1 and FNumber Like ir.FNumber1+'.%' )) AND FDeleteD=0 And FItemClassID = 1) i Where 1=1 And (FItemID = 255) Order by FItemClassID, FNumber
问题点数:100、回复次数:3Top
1 楼tx1icenhe(冒牌马可 V0.4)回复于 2005-07-03 09:06:11 得分 100
第一句:
in和or太多,用exists代替(部分你已经用exists了,应该会改的)
第二句:
检查索引情况,特别是几个连接字段和排序字段
第三句:
嵌套查询这么多,索引都用不上了
(FNumber=ir.FNumber1 OR(FNumber<>ir.FNumber1 and FNumber Like ir.FNumber1+'.%' ))
应该可以改成
(FNumber>=ir.FNumber1 and FNumber<cast(ir.FNumber1 as int)+1)
最后改成:
Select distinct t_Item.*
From t_Item ,(
Select i.FItemID FItemID1 ,i.FNumber FNumber1
From t_ItemRight r,t_Item i
Where FTypeID=1
AND r.FItemID=i.FItemID
AND (FUserID =22 OR FUserID=16708)
) ir
Where (t_Item.FNumber>=ir.FNumber1 and t_Item.FNumber<cast(ir.FNumber1 as int)+1)
AND t_Item.FDeleteD=0 And t_Item.FItemClassID = 1
And (t_Item.FItemID = 255)
Order by t_Item.FItemClassID, t_Item.FNumber
--没有测试
Top
2 楼zhuzhi()回复于 2005-07-03 09:14:22 得分 0
感谢tx1icenhe呀,还有那位有高见吗?100分如果不够我可以再开帖给分的。Top
3 楼zhuzhi()回复于 2005-07-03 09:15:39 得分 0
还有这个语句
SELECT * FROM t_Account,(Select Distinct c1.FAccountID FItemID From t_Account c1,(Select Distinct r.FItemID FItemID,FNumber FNumber1 From t_ItemRight r,t_Account a Where (r.FAuthtype & 2=2) AND FTypeID=105 AND r.FItemID=a.FAccountID AND (FUserID IN (Select FGroupID From t_Group Where FGroupID =21 ) OR FUserID=16411)) c Where c1.FNumber=c.FNumber1 OR PATINDEX(c.FNumber1+'.%',c1.FNumber)>0 ) c WHERE ( FDelete=1 Or FDelete=0 Or FIsAcnt=1) AND FAccountID=c.FItemID ORDER BY FNumberTop




