我有一张表,里面有大约 1200 万行数据。这是表结构:
SYSTEM_ID
BATCH_ID
MEASUREMENT_INDEX,
PARAMETER_ONE,
PARAMETER_TWO
主键由前三列组成。我也有一个关于SYSTEM_ID ASC
,的索引BATCH_ID ASC
。
如果我执行以下查询:
SELECT PARAMETER_ONE
FROM RESULTS_TABLE
WHERE SYSTEM_ID=1
AND BATCH_ID=100;
结果在大约 0.002 秒内返回。
但是,当我尝试执行任何聚合函数时,例如SUM
, MAX
, AVG
,STDDEV
查询时间会增加到 400 秒左右!我尝试了这个查询的各种排列,例如:
SELECT AVG(PARAMETER_ONE)
FROM ( SELECT PARAMETER_ONE
FROM RESULTS_TABLE
WHERE SYSTEM_ID=1
AND BATCH_ID=100
);
和:
SELECT DISTINCT
AVG(PARAMETER_ONE) over (partition by system_id, batch_id)
FROM RESULTS_TABLE
WHERE SYSTEM_ID = 123
AND BATCH_ID = 10;
这些似乎都没有什么不同。有没有其他人有这个问题??
============================
更多细节
聚合函数查询的解释计划是:
Plan hash value: 2759933517
-------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 32 | 2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 32 | | |
| 2 | TABLE ACCESS BY INDEX ROWID| RESULTS_DATA | 1 | 32 | 2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | RESULTS_DATA_INDEX1 | 1 | | 1 (0)| 00:00:01 |
谓词信息(由操作 id 标识):
3 - 访问(“SYSTEM_ID”='123' AND “BATCH_ID”='10')
一个简单的选择的解释计划是:
Plan hash value: 1958859493
------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 32 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| RESULTS_DATA | 1 | 32 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | RESULTS_DATA_INDEX1 | 1 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("SYSTEM_ID"='123' AND "BATCH_ID"='10')
令人惊讶的是,两者的解释计划似乎都声称执行该查询只需要 1 秒,但在 SQL Developer 或 SQL Plus 中绝对不会......
====================================================
更新
这是根据要求使用聚合函数的查询的自动跟踪:
SQL> set autotrace traceonly explain statistics
SQL> SELECT avg(tail_intensity) FROM RESULTS_DATA WHERE SYSTEM_ID = '12
3' AND BATCH_ID = '10';
Execution Plan
----------------------------------------------------------
Plan hash value: 2759933517
--------------------------------------------------------------------------------
-----------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 32 | 2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 32 | | |
| 2 | TABLE ACCESS BY INDEX ROWID| RESULTS_DATA | 1 | 32 | 2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | RESULTS_DATA_INDEX1 | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("SYSTEM_ID"='123' AND "BATCH_ID"='10')
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
437569 consistent gets
437129 physical reads
0 redo size
433 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
感谢您的所有意见 - 我似乎已经解决了。我使用以下方法重新计算了表统计信息:
在此之后,查询工作正常。