【ORACLE】在结果集字段中只有一列要使用组函数MAX,使用GROUP BY效率并不是最高的。
2010-12-04 18:59 土星的狗狗 阅读(2101) 评论(0) 编辑 收藏 举报原SQL如下,效率低的让人不能忍受,然后就在想有没有其它变通的方式:
SELECT MAX(C.INVDATE), D.HAIER_YEAR, D.HAIER_SWEEK_TO_YEAR, C.GOODS_ID FROM ECC_OMS.V_TJ_STOCK C, ECC_OMS.DATE_WEEK_YEAR D WHERE C.INVDATE = D.SDATE GROUP BY D.HAIER_YEAR, D.HAIER_SWEEK_TO_YEAR, C.GOODS_ID
执行计划如下:
SELECT STATEMENT, GOAL = ALL_ROWS 13641 651105 21486465
HASH GROUP BY 13641 651105 21486465
HASH JOIN 6580 1026081 33860673
TABLE ACCESS FULL ECC_OMS DATE_WEEK_YEAR 5 2397 35955
TABLE ACCESS FULL ECC_OMS V_TJ_STOCK 6563 1026081 18469458
想了一会,试着用ORDER BY DESC和ROWNUM=1变通一下:
SELECT C.INVDATE, D.HAIER_YEAR, D.HAIER_SWEEK_TO_YEAR, C.GOODS_ID FROM ECC_OMS.V_TJ_STOCK C, ECC_OMS.DATE_WEEK_YEAR D WHERE C.INVDATE = D.SDATE AND ROWNUM = 1 ORDER BY C.INVDATE DESC
执行计划如下:
SELECT STATEMENT, GOAL = ALL_ROWS 3 1 33
COUNT STOPKEY
NESTED LOOPS 3 1 33
TABLE ACCESS BY INDEX ROWID ECC_OMS V_TJ_STOCK 2 1026081 18469458
INDEX FULL SCAN DESCENDING ECC_OMS V_TJ_STOCK_IDX01 2 1
TABLE ACCESS BY INDEX ROWID ECC_OMS DATE_WEEK_YEAR 1 1 15
INDEX UNIQUE SCAN ECC_OMS DATE_WEEK_YEAR 1 1
请各位大侠指教。