[英]SQL QUERY TAKING TOO MUCH TIME WHEN SERACH ALL
i have a query when i run the query with specific result its showing result in less than 1 sec.当我运行具有特定结果的查询时,我有一个查询,它在不到 1 秒的时间内显示结果。 but when i search the result for all the product its taking half an hour.
但是当我搜索所有产品的结果时,它需要半小时。 i have indexed and also the table from which i am seraching having 2 year data with less than 6000 rows the below is my code.
我已经索引以及我从中检索的表中包含 2 年少于 6000 行的数据,下面是我的代码。
SELECT dbo.IItems.ItCode, dbo.IItems.ItCodeD AS [Item Code], dbo.IItems.ItHead AS Description,
dbo.dbfn_TotalAllSRInq_N(dbo.IItems.ItCode) AS QTY ,
dbo.dbfn_TotalALL_Classification_N(dbo.IItems.ItCode,'Display')
+ dbo.dbfn_TotalALL_Classification_N(dbo.IItems.ItCode,'Trading')
+ dbo.dbfn_TotalALL_Classification_N(dbo.IItems.ItCode,'FOC')
+ dbo.dbfn_TotalALL_Classification_N(dbo.IItems.ItCode,'Liquidation')
- dbo.dbfn_TotalALL_ClassificationBooked_N(dbo.IItems.ItCode) AS Trade,
dbo.dbfn_TotalALL_Classification_N(dbo.IItems.ItCode,'Damage') AS Damage ,
dbo.dbfn_TotalALL_ClassificationBooked_N(dbo.IItems.ItCode) AS Booked,
dbo.CRM_Services_D.Charges AS BottomPrice,
(dbo.CRM_Services_D.SM_Price +((dbo.CRM_Services_D.SM_Price * 0)/100)) AS [SM-Price],
dbo.CRM_Services_D.TagPrice AS Retail, dbo.CRM_Services_D.Pur_Price AS Purchase,
dbo.CRM_Services_D.Inst_Price AS Install, dbo.CRM_Services_D.FixPrice AS Fixed,
dbo.IBinCard.ColorCode , dbo.IBinCard.Mid ,
IBinCard.mid Brandid,''itHeadL3,'' itHeadL2,''itHeadL1,CRM_Services_D.HF_Price as [HF Price],
CRM_Services_D.WebPrice,
CRM_Services_D.comments as [comments],
CRM_Services_D.FCommission as [FCommission],
CRM_Services_D.SalesTax as [SalesTax]
FROM dbo.IItems
left Outer JOIN dbo.CRM_Services_M ON dbo.IItems.ItCode = dbo.CRM_Services_M.ItemCode
left outer JOIN dbo.CRM_Services_D ON
dbo.CRM_Services_M.Service_ID = dbo.CRM_Services_D.Service_ID
AND dbo.CRM_Services_M.POSID = dbo.CRM_Services_D.POSID
AND dbo.CRM_Services_D.StDate = ISNULL((Select Top 1 StDate
from CRM_Services_M M, CRM_Services_D D
Where M.Service_ID = D.Service_ID
and M.POSID = D.POSID
and M.ItemCode= dbo.IItems.itcode
and M.POSID in (1,1)
Order by StDate Desc), GetDate())
INNER JOIN dbo.IBinCard ON
dbo.IItems.ItCode = dbo.IBinCard.Itcode
and iitems.itstatus = 1
and isdisabled = 0
and dbo.CRM_Services_M.POSID = ISNULL((Select Top 1 M.POSID
from CRM_Services_M M,CRM_Services_D D
Where M.Service_ID = D.Service_ID
and M.POSID = D.POSID
and M.ItemCode= dbo.IItems.itcode
and M.POSID in (1,1)
Order by StDate Desc),1)
-- THIS IS THE POINT WHERE I WRITE ANY WORD ITS SHOWING RESULT IN 1 SEC IF I LEFT IT EMPTY ITS GO DOWN
AND IItems.ItHead like '%18cith13%'
--ORDER BY dbo.IItems.ITL1, dbo.IItems.Itl2, dbo.IItems.Itl3, dbo.IItems.Itl4
when i am running the query with the items.ithead like '%any word%' the query show result very fast but when i left the empty totally down.当我使用 items.ithead 运行查询时,如 '%any word%' 查询显示结果非常快,但是当我完全放下空时。 Please help in this query if there any suggestion please tell.
如果有任何建议,请在此查询中提供帮助。 Regards, MaK
问候, 马克
The inner join on dbo.IBinCard contains criteria: dbo.IBinCard 上的内部联接包含条件:
and dbo.CRM_Services_M.POSID = ISNULL((Select Top 1 M.POSID
from CRM_Services_M M,CRM_Services_D D
Where M.Service_ID = D.Service_ID
and M.POSID = D.POSID
and M.ItemCode= dbo.IItems.itcode
and M.POSID in (1,1)
Order by StDate Desc),1)
but:但:
dbo.CRM_Services_M.POSID = 1
as you require M.POSID in (1,1)
and if the recortd is missing, your also return 1.dbo.CRM_Services_M.POSID = 1
相同,因为您需要M.POSID in (1,1)
的 M.POSID 并且如果缺少记录,您也返回 1。FROM dbo.IItems
left Outer JOIN dbo.CRM_Services_M ON
dbo.IItems.ItCode = dbo.CRM_Services_M.ItemCode
AND dbo.CRM_Services_M.POSID = 1
left outer JOIN dbo.CRM_Services_D ON
dbo.CRM_Services_M.Service_ID = dbo.CRM_Services_D.Service_ID
AND dbo.CRM_Services_D.POSID = 1
AND dbo.CRM_Services_D.StDate = (Select max(StDate)
from CRM_Services_D D
Where dbo.CRM_Services_M = D.Service_ID
and D.POSID = 1)
INNER JOIN dbo.IBinCard ON
dbo.IItems.ItCode = dbo.IBinCard.Itcode
WHERE iitems.itstatus = 1
and isdisabled = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.