我有一个这样的查询...
SELECT cust_num, year,credit_cust FROM
(SELECT cust_num, year,credit_cust,
ROW_NUMBER () OVER(PARTITION BY cust_num, year ORDER BY credit_cust DESC) rnk
FROM credit_cust PARTITION (YEAR_2010)
)
WHERE rnk=1
执行此查询时,托管数据库的服务器的 CPU 使用率至少增加 35%。当 CPU 使用率有时会达到 100% 时,这确实涉及服务器负载高的情况。
要求:
数据如下...
Cust_num Year Credit_cust
456 2010 Y
456 2010 N
456 2009 N
456 2009 N
我希望查询只返回下面给出的记录......
Cust_num Year Credit_cust
456 2010 Y
456 2009 N
注意:我使用 ROW_NUMBER() 作为 GROUP BY 子句的解决方法。如前所述,我认为它只是导致 CPU 使用率高的 GROUP BY 子句
备用查询:(CPU 使用率也超过 35%)
SELECT cust_num, YEAR, MAX (credit_cust)
FROM credit_cust PARTITION (year_2010)
GROUP BY cust_num, YEAR
Server : Sun OS 5.10
Database : Oracle 11g (11.2.0.2.0 -64bit)
表结构credit_cust
如下...
CREATE TABLE CREDIT_CUST
(
CUST_NUM NUMBER,
YEAR NUMBER,
CREDIT_CUST CHAR(1)
)
TABLESPACE PARTITION_01
PARTITION BY RANGE (YEAR)
SUBPARTITION BY HASH (CUST_NUM)
(
PARTITION YEAR_2009 VALUES LESS THAN (2010)
SUBPARTITIONS 16 STORE IN (PARTITION_01,PARTITION_02,PARTITION_03,PARTITION_04,PARTITION_05,PARTITION_06,
PARTITION_01,PARTITION_02,PARTITION_03,PARTITION_04,PARTITION_05,PARTITION_06,
PARTITION_01,PARTITION_02,PARTITION_03,PARTITION_04),
PARTITION YEAR_2010 VALUES LESS THAN (2011)
SUBPARTITIONS 1 STORE IN (PARTITION_01))
PARALLEL ( DEGREE 16 INSTANCES 1 );
请告诉我为什么会出现这个问题。让我知道是否需要更多详细信息。
解释计划:
PLAN_TABLE_OUTPUT
Plan hash value: 3927595547
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop | TQ |IN-OUT| PQ Distrib |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 29M| 1179M| | 3937 (3)| 00:00:01 | | | | | |
| 1 | PX COORDINATOR | | | | | | | | | | | |
| 2 | PX SEND QC (RANDOM) | :TQ10001 | 29M| 1179M| | 3937 (3)| 00:00:01 | | | Q1,01 | P->S | QC (RAND) |
|* 3 | VIEW | | 29M| 1179M| | 3937 (3)| 00:00:01 | | | Q1,01 | PCWP | |
|* 4 | WINDOW SORT PUSHED RANK | | 29M| 364M| 674M| 3937 (3)| 00:00:01 | | | Q1,01 | PCWP | |
| 5 | PX RECEIVE | | 29M| 364M| | 3937 (3)| 00:00:01 | | | Q1,01 | PCWP | |
| 6 | PX SEND HASH | :TQ10000 | 29M| 364M| | 3937 (3)| 00:00:01 | | | Q1,00 | P->P | HASH |
|* 7 | WINDOW CHILD PUSHED RANK| | 29M| 364M| | 3937 (3)| 00:00:01 | | | Q1,00 | PCWP | |
| 8 | PX BLOCK ITERATOR | | 29M| 364M| | 34 (3)| 00:00:01 | 1 | 1 | Q1,00 | PCWC | |
|* 9 | TABLE ACCESS FULL | CREDIT_CUST | 29M| 364M| | 34 (3)| 00:00:01 | 228 | 228 | Q1,00 | PCWP | |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("RNK"=1)
4 - filter(ROW_NUMBER() OVER ( PARTITION BY "CUST_NUM","YEAR" ORDER BY INTERNAL_FUNCTION("CREDIT_CUST") DESC )<=1)
7 - filter(ROW_NUMBER() OVER ( PARTITION BY "CUST_NUM","YEAR" ORDER BY INTERNAL_FUNCTION("CREDIT_CUST") DESC )<=1)
9 - filter("YEAR"=2010)
Note
-----
- dynamic sampling used for this statement (level=5)