加速MSSQL SELECT DISTINCT语句

时间:2014-04-02 10:25:42

标签: sql sql-server database tsql relational-database

我已经让这个查询正常工作,并且在1秒内我将DISTINCT添加到SELECT语句中。我已尝试GROUP BY并尝试MAX到每一列。所有这些仍然给予相同的时间5秒。如果任何人对如何更快地运行这一点有任何建议,我将非常感激。

这是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)) + ')'

传递参数

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

结果

Download csv

1 个答案:

答案 0 :(得分:2)

在这里,如评论中所述,不会在所有内容上运行函数,只会返回您要返回的内容:

更好的是,摆脱这些功能。 SQL中的函数总是具有DBA的不良代码味道,而DBA尚未在集合中思考。我确信有更快的方法来获得这些价值。

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