避免使用HAVING子句,HAVING只会在检索出所有记录之后才对结果集进行过滤。这一处理需要排序和总计等操作。如果能通过WHERE子句限制记录的数目,便能减少这方面的开销。

低效示例:

SELECT REGION, AVG(LOG_SIZE) FROM LOCATION

GROUP BY REGION

HAVING REGION != 'SYDNEY' AND REGION != 'PERTH'

高效示例:

SELECT REGION, AVG(LOG_SIZE) FROM LOCATION

WHERE REGION != 'SYDNEY' AND REGION != 'PERTH'

GROUP BY REGION