简体   繁体   English

SQL QUERY 在 SERACH ALL 时花费太多时间

[英]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:但:

  • it has nothing to do with dbo.IBinCard它与 dbo.IBinCard 无关
  • it is the same as 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  
  • you are only interested in CRM_Services_M with POSID = 1 -> I moved that criteria to the first left outer join您只对 POSID = 1 的 CRM_Services_M 感兴趣 -> 我将该条件移至第一个左外连接
  • and thus also only CRM_Services_D with POSID = 1因此也只有 CRM_Services_D POSID = 1
  • only CRM_Services_D with the highest StDate value linked to this item.只有具有最高 StDate 值的 CRM_Services_D 链接到此项目。 the linked dbo.CRM_Services_M record fullfills all your criteria so why not reuse it?链接的 dbo.CRM_Services_M 记录满足您的所有条件,为什么不重用它呢?
  • I've moved the criteria not related to IBinCard to a separate where clause for readability.我已将与 IBinCard 无关的标准移至单独的 where 子句以提高可读性。
  • ISNULL check is not necessary. ISNULL 检查不是必需的。 If the subquery is null then the left outer join will also have no results as they use the same criteria.如果子查询为空,则左外连接也将没有结果,因为它们使用相同的条件。 So either you have linked CRM_Services_D and thus you also have at least one StDate or you have no CRM_Services_D record which respects Service_Id and POSID criteria.因此,要么您已链接 CRM_Services_D,因此您也至少有一个 StDate,要么您没有符合 Service_Id 和 POSID 标准的 CRM_Services_D 记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM