sql GROUP BY需要太长时间

时间:2014-07-24 13:28:21

标签: sql performance oracle aggregate-functions

由于GROUP BY,以下查询似乎没有结束。

没有GROUP BY,它会在不等待的情况下返回记录

你能提出一些想法吗?

提前致谢

   SELECT 
         MPHIST_LF.ID_MEASURING_POINT AS MP_LF,
         MPHIST_LF.FROM_DATE AS FROM_LF,
         MPHIST_LF.TO_DATE AS TO_LF, 
         RM.ID_FARE AS FARE_LF,
         COUNT(*)
   FROM RE_MPOINT_HIST MPHIST_LF
        INNER JOIN ME_MEA_CTYPE_HIST CTHIST_LF ON CTHIST_LF.ID_MEASURER = MPHIST_LF.ID_MEASURER
        INNER JOIN ME_MEASURER ME ON ME.ID_MEASURER = MPHIST_LF.ID_MEASURER
        INNER JOIN ME_COMBINATION_USAGE_TYPE CUT ON ME.ID_COMBINATION_USAGE_TYPE = CUT.ID_COMBINATION_USAGE_TYPE
        INNER JOIN ME_RATE_METER RM ON RM.ID_RATE_METER = CUT.ID_RATE_METER
   WHERE CTHIST_LF.COD_USAGE_TYPE = 'AE0' 
        AND CUT.COD_DH = 'CODDH00001'
        AND (RM.ID_FARE  = 1000072 OR RM.ID_FARE = 1002253)
        AND (RM.COD_GESTION IS NULL OR RM.COD_GESTION =  'TYPCLI0003')
   GROUP BY MPHIST_LF.ID_MEASURING_POINT,MPHIST_LF.FROM_DATE,MPHIST_LF.TO_DATE, RM.ID_FARE 
   HAVING COUNT(*) = 2


GCGT_ME_MEASURER:  
  IDX_GCGTMEMEASURER_01 >> ID_MEASURER, RELEVANT_EQUIP   
  PK_GCGT_ME_MEASURER >> ID_MEASURER 
  IDX_GCGT_ME_MEASURER_99 >> ID_DEVICE   

GCGT_ME_COMBINATION_USAGE_TYPE:  
  IDX_FK_ME_COM_US_TY_DEV_TY_01 >> COD_DEVICE_TYPE 
  PK_GCGT_ME_COMB_USAGE_TYPE >> ID_COMBINATION_USAGE_TYPE    

GCGT_ME_MEA_CTYPE_HIST:  
  IDX_MEA_CTYPE_HIST_01 >> ID_MEASURER 
  IDX_MEA_CTYPE_HIST >> ID_MEA_CTYPE_HIST 

GCGT_ME_RATE_METER:  
  IDX_FK_ME_RATE_TY_RE_SPEC_01 >> COD_SPECIALIZATION 
  PK_GCGT_ME_RATE_METER >> ID_RATE_METER         

GCGT_RE_MPOINT_HIST:  
  IDX_GCGT_RE_MPOINHHIST_03 >> ID_MEASURING_POINT, ID_MEASURER, ID_MP_NATURE, ID_COMBINATION_USAGE_TYPE, ID_RATE_METER 
  IDX_GCGT_RE_MPOINHHIST_01 >> ID_MEASURING_POINT 
  IDX_GCGT_RE_MPOINHHIST_02 >> ID_MEASURER 
  GCGT_RE_MP_HIST_NAT_01 >> ID_MP_NATURE 
  IDX_MPOINT_HIST >> ID_MPOINT_HIST  

Record count: 8064087, without the aggregate.    

Statistcs up to date with, for instance: ::::: 
EXEC DBMS_STATS.GATHER_TABLE_STATS ('user', 'RE_MPOINT_HIST'); 

如果没有聚合函数,结果将以秒为单位显示。可能并非所有结果都显示出来。没有聚合的计数(*)持续约20或25分钟。

所有索引都是重建的。

---------------------------------------------------------------------------------------------------------------------  
| Id  | Operation                          | Name                           | Rows  | Bytes | Cost (%CPU)| Time     |  
---------------------------------------------------------------------------------------------------------------------  
|   0 | SELECT STATEMENT                   |                                |  3970 |   302K|  4622   (1)| 00:00:56 |  
|   1 |  HASH GROUP BY                     |                                |  3970 |   302K|  4622   (1)| 00:00:56 |  
|   2 |   NESTED LOOPS                     |                                |       |       |         |     |  
|   3 |    NESTED LOOPS                    |                                |  3970 |   302K|  4621   (1)| 00:00:56 |  
|   4 |     NESTED LOOPS                   |                                |  4371 |   290K|  1997   (1)| 00:00:24 |  
|   5 |      NESTED LOOPS                  |                                |  2551 |   107K|   466   (0)| 00:00:06 |  
|   6 |       NESTED LOOPS                 |                                |     1 |    33 |     2   (0)| 00:00:01 |  
|   7 |        INLIST ITERATOR             |                                |       |       |            |          |
|*  8 |         TABLE ACCESS BY INDEX ROWID| GCGT_ME_RATE_METER             |     1 |    21 |     1   (0)| 00:00:01 |  
|*  9 |          INDEX RANGE SCAN          | FK_GCGT_ME_RATE_GCCOM_FARE_01  |     4 |       |     1   (0)| 00:00:01 |  
|* 10 |        TABLE ACCESS BY INDEX ROWID | GCGT_ME_COMBINATION_USAGE_TYPE |     1 |    12 |     1   (0)| 00:00:01 |  
|* 11 |         INDEX RANGE SCAN           | FK_ME_COM_US_TY_RATE_METER_01  |     6 |       |     1   (0)| 00:00:01 |  
|  12 |       TABLE ACCESS BY INDEX ROWID  | GCGT_ME_MEASURER               |  8574 | 85740 |   464   (0)| 00:00:06 |  
|* 13 |        INDEX RANGE SCAN            | FK_ME_MEASURER_ME_COMB_US_01   | 17368 |       |     1   (0)| 00:00:01 |  
|  14 |      TABLE ACCESS BY INDEX ROWID   | GCGT_RE_MPOINT_HIST            |     2 |    50 |     1   (0)| 00:00:01 |  
|* 15 |       INDEX RANGE SCAN             | IDX_GCGT_RE_MPOINHHIST_02      |     2 |       |     1   (0)| 00:00:01 |  
|* 16 |     INDEX RANGE SCAN               | IDX_MEA_CTYPE_HIST_01          |     3 |       |     1   (0)| 00:00:01 |  
|* 17 |    TABLE ACCESS BY INDEX ROWID     | GCGT_ME_MEA_CTYPE_HIST         |     1 |    10 |     1   (0)| 00:00:01 |  
---------------------------------------------------------------------------------------------------------------------  

1 个答案:

答案 0 :(得分:0)

  1. 创建一个临时表来存储计数和ID信息。
  2. 左连接此计数临时表。
  3. 过滤您需要的计数数据。