sql语句查询特别慢问题分析

oceanbase 4.2.5.5版本
sql 语句:SELECT id FROM eeo_storage_flow WHERE es_time < 1771527608 limit 10000
花费时间为8秒
表分区为id分区,
索引:index(idx_et)
执行计划:
| Query Plan |
±---------------------------------------------------------------------------------------------------------------------------------------+
| ========================================================================= |
| |ID|OPERATOR |NAME |EST.ROWS|EST.TIME(us)| |
| ------------------------------------------------------------------------- |
| |0 |EXCHANGE IN REMOTE | |10000 |12950 | |
| |1 |└─EXCHANGE OUT REMOTE| |10000 |9142 | |
| |2 | └─TABLE RANGE SCAN |eeo_storage_flow(idx_et)|10000 |601 | |
| ========================================================================= |

Outputs & filters:
0 - output([eeo_storage_flow.id(0x7fb472e22590)]), filter(nil)
1 - output([eeo_storage_flow.id(0x7fb472e22590)]), filter(nil)
2 - output([eeo_storage_flow.id(0x7fb472e22590)]), filter(nil), rowset=256
access([eeo_storage_flow.id(0x7fb472e22590)]), partitions(p0)
limit(10000(0x7fb472e23f70)), offset(nil), is_index_back=false, is_global_index=true,
range_key([eeo_storage_flow.es_time(0x7fb472e23680)], [eeo_storage_flow.id(0x7fb472e22590)]), range(NULL,MAX ; 1771527608,MIN),
range_cond([eeo_storage_flow.es_time(0x7fb472e23680) < 1771527608(0x7fb472e22c30)(0x7fb472e22f00)])
Used Hint:
-------------------------------------
/*+
*/
Qb name trace:
-------------------------------------
stmt_id:0, stmt_type:T_EXPLAIN
stmt_id:1, SEL$1
Outline Data:
-------------------------------------
/*+
BEGIN_OUTLINE_DATA
INDEX(@“SEL$1” “eo_storage”.“eeo_storage_flow”@“SEL$1” “idx_et”)
OPTIMIZER_FEATURES_ENABLE(‘4.2.1.8’)
END_OUTLINE_DATA
*/
Optimization Info:
-------------------------------------
eeo_storage_flow:
table_rows:716170482
physical_range_rows:544200963
logical_range_rows:521704589
index_back_rows:0
output_rows:521704589
table_dop:1
dop_method:Table DOP
avaiable_index_name:[uniq_u, idx_oesabf, idx_cse, idx_et, eeo_storage_flow]
pruned_index_name:[uniq_u, idx_oesabf, idx_cse, eeo_storage_flow]
stats info:[version=2026-03-01 06:15:33.535370, is_locked=0, is_expired=0]
dynamic sampling level:0
estimation method:[OPTIMIZER STATISTICS, STORAGE]
Plan Type:
REMOTE
Parameters:
:0 => 1771527608
:1 => 10000
Note:
Degree of Parallelisim is 1 because of table property
2 个赞

经过分析,发现是因为es_time < 1771527608 默认按照es_time asc扫描的顺序太多导致
解决办法:

  1. 语句修改为SELECT id FROM eeo_storage_flow WHERE es_time < 1771527608 order by es_time desc limit 10000解决,花费时间为30毫秒
  2. 可以通过对全局索引进行分区,需要注意如果使用全局索引自动分裂在 V4.3.5 BP2 才生效

下面是通过obdiag的2次trace id进行的跟踪对比:
obdiag_gather_pack_20260304141942.tar.gz (472.4 KB)
obdiag_gather_pack_20260304134559.tar.gz (472.3 KB)

2 个赞

学习了!