문제

I've had this query working fine and under 1 seconds until I'd added a DISTINCT to the SELECT statement. I've tried GROUP BY and also tried MAX to each column. All of which still give the same amount of time 5+ seconds. If any one has any suggestions on how to make this run quicker I'd be massively grateful.

This is a follow on from the post here

  SET @query = N'SELECT * FROM (  
  SELECT
        ROW_NUMBER() OVER (ORDER BY ' + @SORTBY + ') AS [rownum],  *  
  FROM (
        SELECT DISTINCT  
              dbo.funcSellIdByPropId(T0.id) as SellerId,  
              dbo.funcDefaultImage(T0.id, 1) as propImage,  
              dbo.funcDefaultImage(T0.id, 2) as propImage2,  
              dbo.funcDefaultImage(T0.id, 3) as propImage3,  
              dbo.funcDefaultImage(T0.id, 4) as propImage4,  
              dbo.funcCountPropertyImages(T0.id) as imageCount,  
              dbo.funcGetPropertyTypeListGB(T0.id) as TypeGB,  
              dbo.funcGetPropertyTypeListFR(T0.id) as TypeFR,  
              dbo.funcGetPropertyEnviListGB(T0.id) as EnviGB,  
              dbo.funcGetPropertyEnviListFR(T0.id) as EnviFR,  
              dbo.funcGetDepartmentByTown(T0.Town) as Department,  
              dbo.funcCheckFeaturedProperty(T0.id) as Featured,  
              T0.id, T0.Price, T0.BedRooms, T0.Town, T0.Postcode,
              T0.Mandate, T0.MinLandArea, T0.Rooms, T0.HabitableSurface, 
              T0.Active, T0.Budget,  
              T1.TitleFR, T1.TitleGB, SUBSTRING(T1.DescFR, 0, 300) as DescFR,
              SUBSTRING(T1.DescGB, 0, 300) as DescGB  
          FROM  
              PROPERTIES T0
              INNER JOIN PROPERTYTRANSLATIONS T1 ON T1.PropertyId = T0.id  
              INNER JOIN MATRIXPROPENVIRONMENT T2 ON T2.PropertyId = T0.id
              INNER JOIN ENVIRONMENT T3 ON T3.id = T2.EnvironmentId  
          WHERE  
              T0.Deleted = 0    
              AND T0.Active = 1   
              AND T3.GB IN (' + @FILTERBY + ')  
    ) T ) TT  
  WHERE  rownum BETWEEN (' + CONVERT(varchar(4), @PERPAGE * @PAGENUM) + ') AND (' + CONVERT(varchar(4), @PERPAGE * (@PAGENUM + 1)) + ')'

Passed Parameters

@PAGENUM = 0
@PERPAGE = 20
@SORTBY = N'Price DESC'
@FILTERBY = N'''City'', ''Village'', ''Coastal'''

Result

Download csv

도움이 되었습니까?

해결책

Here you go, as stated in the comments don't run the functions on everything, only what you are going to return:

Even better, get rid of those functions. Functions in SQL always have the bad code smell of a DBA who is not yet thinking in sets. I'm sure there is a faster way to get those values.

SELECT 
  [rownum],
  dbo.funcSellIdByPropId(T0.id) as SellerId,  
  dbo.funcDefaultImage(T0.id, 1) as propImage,  
  dbo.funcDefaultImage(T0.id, 2) as propImage2,  
  dbo.funcDefaultImage(T0.id, 3) as propImage3,  
  dbo.funcDefaultImage(T0.id, 4) as propImage4,  
  dbo.funcCountPropertyImages(T0.id) as imageCount,  
  dbo.funcGetPropertyTypeListGB(T0.id) as TypeGB,  
  dbo.funcGetPropertyTypeListFR(T0.id) as TypeFR,  
  dbo.funcGetPropertyEnviListGB(T0.id) as EnviGB,  
  dbo.funcGetPropertyEnviListFR(T0.id) as EnviFR,  
  dbo.funcGetDepartmentByTown(T0.Town) as Department,  
  dbo.funcCheckFeaturedProperty(T0.id) as Featured,  
  T0.id, T0.Price, T0.BedRooms, T0.Town, T0.Postcode,
  T0.Mandate, T0.MinLandArea, T0.Rooms, T0.HabitableSurface, 
  T0.Active, T0.Budget,  
  T1.TitleFR, T1.TitleGB, SUBSTRING(T1.DescFR, 0, 300) as DescFR,
  SUBSTRING(T1.DescGB, 0, 300) as DescGB  
  FROM  
(
 SELECT
        ROW_NUMBER() OVER (ORDER BY ' + @SORTBY + ') AS [rownum],  *  
 FROM (
        SELECT DISTINCT  
              T0.id

          FROM  
              PROPERTIES T0
              INNER JOIN PROPERTYTRANSLATIONS T1 ON T1.PropertyId = T0.id  
              INNER JOIN MATRIXPROPENVIRONMENT T2 ON T2.PropertyId = T0.id
              INNER JOIN ENVIRONMENT T3 ON T3.id = T2.EnvironmentId  
          WHERE  
              T0.Deleted = 0    
              AND T0.Active = 1   
              AND T3.GB IN (' + @FILTERBY + ')  
    ) T ) TT  
  WHERE  rownum BETWEEN (' + CONVERT(varchar(4), @PERPAGE * @PAGENUM) + ') 
    AND (' +   CONVERT(varchar(4), @PERPAGE * (@PAGENUM + 1)) + ')'
) TTT
JOIN PROPERTIES T0 ON T0.id = TTT.id
JOIN PROPERTYTRANSLATIONS T1 ON T1.PropertyId = T0.id  
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top