- 最后登录
- 2013-11-14
- 在线时间
- 104 小时
- 威望
- 999
- 金钱
- 271
- 注册时间
- 2013-1-9
- 阅读权限
- 70
- 帖子
- 63
- 精华
- 0
- 积分
- 999
- UID
- 855
|
14#
发表于 2013-3-22 22:45:06
本帖最后由 Stone 于 2013-3-22 22:47 编辑
在这个SQL 外再加一层查询:
SELECT T1.EVENT_ID,
CC.EVENT_FORMAT_ID,
CC.SUBS_ID,
CC.PREFIX,
CC.ACC_NBR,
CC.CREATED_DATE,
CC.STATE,
CC.STATE_DATE,
CC.EVENT_PARAM,
CC.COMMENTS,
CC.SP_ID
FROM (SELECT EVENT_ID
FROM (SELECT EVENT_ID
FROM CC_EVENT
WHERE STATE = 'A'
ORDER BY EVENT_ID)
WHERE ROWNUM < 3) T1,
CC_EVENT CC WHERE T1.EVENT_ID = CC.EVENT_ID
如果不影响业务的话,这个查询是不是可以改下比较好呐 :)- SELECT *
- FROM (SELECT event_id
- event_format_id,
- subs_id,
- prefix,
- acc_nbr,
- created_date,
- state,
- state_date,
- event_param,
- comments,
- sp_id
- FROM cc_event
- WHERE state = 'A'
- ORDER BY event_id)
- WHERE ROWNUM < 3;
复制代码 另外关于“sort order by stopkey”,我的理解:应该不是影响因素,而是在全表扫描下Oracle一种更快的处理Top N选择。详细的解释可以参考:http://betteratoracle.com/posts/18
SORT ORDER BY STOPKEY
In the top N query explain plan, I highlighted the sort operation changing from 'SORT ORDER BY' to 'SORT ORDER BY STOPKEY'. This is a special sort operation that Oracle uses to make top N queries more efficient.
When you add a rownum filter, Oracle knows the query will return at most N rows, it doesn't need to do a full sort of the data, it only needs to remember the top (or bottom) N results of the sort.
Conceptually, Oracle will allocate N slots for the results.
For each row read, it will check if it sorts higher than the N results it is holding, if it does the record in the lowest position gets pushed out, and the new record is slotted into the correct space. This is much more efficient than sorting potentially millions of rows when only 10 are needed. |
|