简体   繁体   中英

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. 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.

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. Please help in this query if there any suggestion please tell. Regards, MaK

The inner join on dbo.IBinCard contains criteria:

      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
  • 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.
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
  • and thus also only CRM_Services_D with POSID = 1
  • only CRM_Services_D with the highest StDate value linked to this item. the linked dbo.CRM_Services_M record fullfills all your criteria so why not reuse it?
  • I've moved the criteria not related to IBinCard to a separate where clause for readability.
  • ISNULL check is not necessary. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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