是优化选择max(date)sql查询吗?

时间:2017-12-05 12:12:17

标签: sql sql-server

此查询太慢了。是否有可能对其进行优化?

SELECT 
    CONVERT(CHAR(10), MAX(dat), 101) AS MaxDate 
FROM 
    X_Prices price      
INNER JOIN 
    RMetal cat ON cat.SubCat = price.Subcat AND cat.Category = price.Category     
WHERE
    cat.Country = 'India' 
    AND cat.Section = 1 
    AND cat.active = 1 
    AND cat.inactive = 0 
    AND price.Dat < GETDATE() - 60 

3 个答案:

答案 0 :(得分:1)

对于此查询:

SELECT CONVERT(CHAR(10), MAX(dat), 101) AS MaxDate
FROM X_Prices price INNER JOIN
     RMetal cat
     ON cat.SubCat = price.Subcat AND cat.Category = price.Category     
WHERE cat.Country = 'India' AND
      cat.Section = 1 AND
      cat.active = 1 AND
      cat.inactive = 0 AND
      price.Dat < GETDATE()- 60 ;

我会从RMetal(Country, Section, Active, Inactive, Category, SubCat)X_Prices(Category, SubCat, date)上的索引开始。

答案 1 :(得分:0)

如果你可以改变数据库: 用国家代码替换国家,确保SubCat&amp;类别是数字,&amp;有索引(作为外键)

如果这是数据库结构,你应该根据执行计划添加索引,可能是在整个where子句

答案 2 :(得分:0)

上应用索引
 Country , Section , active , inactive column of RMetal 
table And Category, SubCat, date column of X_Prices table