-
替换HAVING子句为WHERE子句以实现ORACLE+SQL性能优化(全面解析)
资源介绍
用Where子句替换HAVING子句
避免使用HAVING子句, HAVING 只会在检索出所有记录之后才对结果
集进行过滤. 这个处理需要排序,总计等操作. 如果能通过WHERE子句限
制记录的数目,那就能减少这方面的开销.
例如:
低效:
SELECT REGION,AVG(LOG_SIZE)
FROM LOCATION
GROUP BY REGION
HAVING REGION REGION != ‘SYDNEY’
AND REGION != ‘PERTH’
高效
SELECT REGION,AVG(LOG_SIZE)
FROM LOCATION
WHERE REGION REGION != ‘SYDNEY’
AND REGION != ‘PERTH’
GROUP BY REGION
顺序
WHERE > GROUP > HAVING