SQL优化选对执行计划,查询速度提升1000倍 | OceanBase 应用实践

news2025/1/16 21:59:14
作者:爱可生数据库高级工程师任仲禹,擅长故障分析和性能优化。

本文通过一个案例,分享使用OceanBase时,SQL走错执行计划,而导致慢SQL的排查方法论。

案例背景

在使用OceanBase 3.2.3 版本的过程中,项目组反映某个 SELECT 语句在指定时间内的查询响应速度异常缓慢,其耗时远超正常情况的1000倍以上。具体细节如下:

  • 慢 SELECT
SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31' ;
  • 关键表结构、记录数信息如下
-- 脱敏处理
show create table renzy\G                  
*************************** 1. row ***************************
       Table: renzy
Create Table: CREATE TABLE `renzy` (
  `ID` char(18) COLLATE utf8mb4_bin NOT NULL COMMENT ,
...
  `ACCT_NO` char(40) COLLATE utf8mb4_bin NOT NULL COMMENT ,
...
  `ACCTG_DT` date DEFAULT NULL COMMENT ,
...
  PRIMARY KEY (`ID`),
...
  KEY `renzy_I2` (`ACCT_NO`) BLOCK_SIZE 16384 LOCAL,
...
  KEY `renzy_I5` (`ACCTG_DT`, `ENQ_INST_CD`, `BLON_INST_CD`, `EMRG_STPY_SRC_CD`) BLOCK_SIZE 16384 LOCAL,
...
) DEFAULT CHARSET = utf8mb4;
1 row in set (0.01 sec)

MySQL > SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882');
+---------+
| TOT_CNT |
+---------+
|       1 |
+---------+
1 row in set (0.02 sec)

MySQL > SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31';
+----------+
| TOT_CNT  |
+----------+
| 25432155 |
+----------+
1 row in set (12.42 sec)

MySQL > SELECT COUNT(*) AS TOT_CNT FROM renzy;                                                            
+----------+
| TOT_CNT  |
+----------+
| 25435024 |
+----------+
1 row in set (10.65 sec)

排查过程

正常执行不慢

MySQL > select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31') as orginal limit 2000; select last_trace_id();
+---------+
| TOT_CNT |
+---------+
|       1 |
+---------+
1 row in set (0.02 sec)

以下是执行计划,从中可见,索引I2是最高效的选择,它在进行等值匹配时仅需要执行一次回表操作。

*************************** 1. row ***************************
Query Plan: ========================================================
|ID|OPERATOR        |NAME               |EST. ROWS|COST|
--------------------------------------------------------
|0 |LIMIT           |                   |1        |92  |
|1 | SCALAR GROUP BY|                   |1        |92  |
|2 |  TABLE SCAN    |renzy(renzy_I2)|1        |92  |
========================================================
...
Outline Data:
-------------------------------------
  /*+
      BEGIN_OUTLINE_DATA
      INDEX(@"SEL$2" "gabsdb.renzy"@"SEL$2" "renzy_I2")
      END_OUTLINE_DATA
  */
...
renzy:table_rows:25419080, physical_range_rows:1, logical_range_rows:1, index_back_rows:1, output_rows:0, est_method:local_storage, optimization_method=cost_based, avaiable_index_name[renzy_I2,renzy_I5], pruned_index_name[renzy_I1,renzy_I3,renzy_I4,renzy_I6], unstable_index_name[renzy], estimation info[table_id:1105009185965290, (table_type:1, version:0-1699898410195654-1699898410195654, logical_rc:1, physical_rc:1), (table_type:7, version:1699898401860480-1699898401860480-1699898433101378, logical_rc:0, physical_rc:0), (table_type:7, version:1699898433101378-1699904137032515-1699905915658079, logical_rc:0, physical_rc:0), (table_type:5, version:1699898433101378-1699904137032515-1699905915658079, logical_rc:0, physical_rc:0), (table_type:0, version:1699905915658079-1699905915658079-9223372036854775807, logical_rc:0, physical_rc:0)]
...

通过 OCP 的 SQL 诊断获取慢 SQL 的 plan_id,检查慢 SQL 实际命中的 plan。

MySQL [oceanbase]> select * from gv$plan_cache_plan_stat where plan_id=7288229 \G                                                                         
*************************** 1. row ***************************
...
              plan_id: 7288229
...
            statement: select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = ?) AND LAWENF_NTIST_TP_CD NOT LIKE ? AND LAWENF_NTIST_TP_CD NOT LIKE ? AND EMRG_STPY_SRC_CD != ? AND ACCTG_DT >= ? AND ACCTG_DT <= ?) as orginal limit 2000
            query_sql: select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '' AND ACCTG_DT <= '') as orginal limit 2000
       special_params: 2000
          param_infos: {1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,22},{1,0,0,-1,17},{1,0,0,-1,17}
             sys_vars: 45,45,12582912,2,4,1,0,0,32,3,1,0,1,1,0,10485760,1,1,0,1,BINARY,BINARY,AL32UTF8,AL16UTF16,BYTE,FALSE,1,100,64,200,0,13,NULL,1,1,1,1
            plan_hash: 10428103352368081688
      first_load_time: 2023-11-14 10:14:11.578250
       schema_version: 1699927892190832
       merged_version: 287
     last_active_time: 2023-11-14 11:04:58.127020
         avg_exe_usec: 35858760
     slowest_exe_time: 2023-11-14 11:04:58.127020
     slowest_exe_usec: 171575101
           slow_count: 2
            hit_count: 7
            plan_size: 81984
           executions: 8
           disk_reads: 1136285
        direct_writes: 0
          buffer_gets: 18067948
application_wait_time: 0
concurrency_wait_time: 0
    user_io_wait_time: 0
       rows_processed: 8
         elapsed_time: 286870087
             cpu_time: 229807460
         large_querys: 2
 delayed_large_querys: 1
    delayed_px_querys: 0
      outline_version: 0
           outline_id: -1
         outline_data: /*+ BEGIN_OUTLINE_DATA INDEX(@"SEL$2" "gabsdb.renzy"@"SEL$2" "renzy_I5") END_OUTLINE_DATA*/
....
1 row in set (0.09 sec)

MySQL [oceanbase]> select * from oceanbase.gv$plan_cache_plan_explain where tenant_id=1005 and port=2882 and plan_id=7288229 and ip='12.240.26.70'\G
....
PLAN_LINE_ID: 2
    OPERATOR:   PHY_TABLE_SCAN
        NAME: renzy(renzy_I5)
        ROWS: 0
        COST: 91
    PROPERTY: table_rows:25419080, physical_range_rows:1, logical_range_rows:1, index_back_rows:0, output_rows:0, est_method:local_storage, avaiable_index_name[renzy_I2,renzy_I5]
...

上述结果的关键信息如下

1.query_sql :为该plan第一次执行时的SQL语句。

2.first_load_time :缓存该plan并hit的时间。

3.slowest_exe_usec :该计划的最慢耗时。

4.outline_id : 是否命中了绑定的outline,-1即未命中。

5.statement :参数化后的SQL语句。

6.name : 该plan走的索引。

分析下第一次的SQL为啥要走 I5 索引

通过下面执行计划和执行耗时可知,第一次执行的语句因为字段 ACCTG_DT 检索不到数据,所以走 I5 效率最高。

MySQL > explain extended select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '' AND ACCTG_DT <= '') as orginal limit 2000\G
*************************** 1. row ***************************
Query Plan: ========================================================
|ID|OPERATOR        |NAME               |EST. ROWS|COST|
--------------------------------------------------------
|0 |LIMIT           |                   |1        |92  |
|1 | SCALAR GROUP BY|                   |1        |92  |
|2 |  TABLE SCAN    |renzy(renzy_I5)|0        |92  |
========================================================
Outline Data:
-------------------------------------
  /*+
      BEGIN_OUTLINE_DATA
      INDEX(@"SEL$2" "gabsdb.renzy"@"SEL$2" "renzy_I5")
      END_OUTLINE_DATA
  */
renzy:table_rows:25419080, physical_range_rows:1, logical_range_rows:1, index_back_rows:0, output_rows:0, est_method:local_storage, optimization_method=cost_based, avaiable_index_name[renzy_I2,renzy_I5], pruned_index_name[renzy_I1,renzy_I3,renzy_I4,renzy_I6], unstable_index_name[renzy]

MySQL > select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '' AND ACCTG_DT <= '') as orginal limit 2000;
+---------+
| TOT_CNT |
+---------+
|       0 |
+---------+
1 row in set, 2 warnings (0.02 sec)

分析下后续SQL为何不淘汰该plan

我们知道,SQL查询并不需要每次生成查询计划,因为这样涉及到硬解析等耗费性能的操作,所以默认每次会先查询 Plan Cache (硬解析操作包含词法/语法/语义解析,优化器统计信息查询等步骤,参考下图)。

1716357409

本案例中,后续的SQL命中该 Plan 就可以理解,因为要走 I5 索引,range太大基本为全索引扫描,所以耗时太慢。,

ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31'

什么时候淘汰这个计划呢?

// 关键代码段如下(410bp1社区版,这里的逻辑和323bp8企业版类似,企业版代码不便贴出)
if (sample_count < SLOW_QUERY_SAMPLE_SIZE) {
        // do nothing when query execution samples are not enough
      } else {
        if (stat_.cpu_time_ <= SLOW_QUERY_TIME_FOR_PLAN_EXPIRE * stat_.execute_times_) {
        // do nothing for fast query
        } else if (is_plan_unstable(sample_count, sample_exec_row_count, sample_exec_usec)) {
          set_is_expired(true);
        }
        ATOMIC_STORE(&(stat_.sample_times_), 0);
      }
    }

bool ObPhysicalPlan::is_plan_unstable(const int64_t sample_count,
                                      const int64_t sample_exec_row_count,
                                      const int64_t sample_exec_usec)
{
  bool bret = false;
  if (sample_exec_usec <= SLOW_QUERY_TIME_FOR_PLAN_EXPIRE * sample_count) {
    // sample query is fast query in the average
  } else if (OB_PHY_PLAN_LOCAL == plan_type_) {
    int64_t first_query_range_rows = ATOMIC_LOAD(&stat_.first_exec_row_count_);
    if (sample_exec_row_count <= SLOW_QUERY_ROW_COUNT_THRESOLD * sample_count) {
      // the sample query does not accesses too many rows in the average
    } else if (sample_exec_row_count / sample_count > first_query_range_rows * 10) {
      // the average sample query range row count increases great
      bret = true;
      LOG_INFO("local query plan is expired due to unstable performance",
               K(bret), K(stat_.execute_times_),
               K(first_query_range_rows), K(sample_exec_row_count), K(sample_count));
    }
  } else if ( OB_PHY_PLAN_DISTRIBUTED == plan_type_) {
    int64_t first_exec_usec = ATOMIC_LOAD(&stat_.first_exec_usec_);
    if (sample_exec_usec / sample_count > first_exec_usec * 2) {
      // the average sample query execute time increases great
      bret = true;
      LOG_INFO("distribute query plan is expired due to unstable performance",
               K(bret), K(stat_.execute_times_), K(first_exec_usec),
               K(sample_exec_usec), K(sample_count));
    }
  } else {
    // do nothing
  }
  return bret;
}

这里淘汰一个 Plan 需要满足的条件有2个:

  • sample_count < SLOW_QUERY_SAMPLE_SIZE)
  • sample_exec_row_count / sample_count > first_query_range_rows * 10

这里的 SLOW_QUERY_SAMPLE_SIZE 是常量,OB410的定义是 20;sample_count(采样次数)实质为Plan的SQL执行次数。

static const int64_t SLOW_QUERY_SAMPLE_SIZE = 20; // smaller than ObPlanStat::MAX_SCAN_STAT_SIZE

结合上下文代码来看,意思是满足如下情况就会淘汰Plan:

  • 命中该Plan的SQL执行大于等于20次。
  • (执行的SQL扫描总行数 / 执行次数) 大于  (第一次SQL执行扫描的行数 * 10)

复现以验证

1.清空 plan cache,执行业务第一次生成 Plan 的 SQL。

MySQL > alter system flush plan cache;
Query OK, 0 rows affected (0.13 sec)

MySQL > select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '' AND ACCTG_DT <= '') as orginal limit 2000;
+---------+
| TOT_CNT |
+---------+
|       0 |
+---------+
1 row in set, 2 warnings (0.02 sec)

2.执行业务 SQL,复现慢的情况。

MySQL > select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31') as orginal limit 2000;
+---------+
| TOT_CNT |
+---------+
|       1 |
+---------+
1 row in set (2 min 51.61 sec)

MySQL > select last_trace_id();
+-----------------------------------+
| last_trace_id()                   |
+-----------------------------------+
| YB420CF01A46-0006009AD91C51ED-0-0 |
+-----------------------------------+
1 row in set (0.04 sec)

MySQL > select * from oceanbase.gv$sql_audit where trace_id='YB420CF01A46-0006009AD91C51ED-0-0'\G                                                                                                         ...
               TRACE_ID: YB420CF01A46-0006009AD91C51ED-0-0
...
                 SQL_ID: 2B53F4C1C330C2C089C7518CD71D667A
              QUERY_SQL: select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31') as orginal limit 2000
...
           ELAPSED_TIME: 171575101
...
           EXECUTE_TIME: 171574843
...
MEMSTORE_READ_ROW_COUNT: 25416176
 SSSTORE_READ_ROW_COUNT: 50832349
...

这里通过 sql_audit 可以观测到重要的信息:

  • ELAPSED_TIME : 执行耗时。
  • MEMSTORE_READ_ROW_COUNT / SSSTORE_READ_ROW_COUNT : 这条SQL扫描的行数。
MySQL [oceanbase]> select * from gv$plan_cache_plan_stat where plan_id=7289113 \G                                                                         
*************************** 1. row ***************************
...
               sql_id: 2B53F4C1C330C2C089C7518CD71D667A
...
            statement: select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = ?) AND LAWENF_NTIST_TP_CD NOT LIKE ? AND LAWENF_NTIST_TP_CD NOT LIKE ? AND EMRG_STPY_SRC_CD != ? AND ACCTG_DT >= ? AND ACCTG_DT <= ?) as orginal limit 2000
            query_sql: select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '' AND ACCTG_DT <= '') as orginal limit 2000
...
           outline_id: -1
         outline_data: /*+ BEGIN_OUTLINE_DATA INDEX(@"SEL$2" "gabsdb.renzy"@"SEL$2" "renzy_I5") END_OUTLINE_DATA*/
...

通过 plan_cache_plan_stat 可看到这条SQL命中了第一次SQL执行时生成的 Plan(不符合预期)。

3.继续通过脚本执行多次。

#!/bin/bash
for i in `seq 1 30`
do
echo ">>> do  $i"
mysql -h12.240.68.36 -P3306 -uroot@tgabsua2g00#obcdcbsuat01 -pOceanBase_123# -Dgabsdb -A -c -NBe "select now();select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31') as orginal limit 2000; select last_trace_id();select now();"
done

# ./1.sh
...
>>> do  17       # 耗时 2分钟27s 命中
2023-11-14 16:05:36
1
YB420CF01A46-0006009B016C66EE-0-0
2023-11-14 16:08:03
>>> do  18       # 耗时 2min12s  命中
2023-11-14 16:08:03
1
YB420CF01A46-0006009AFF8FF46D-0-0
2023-11-14 16:10:15
>>> do  19       # 耗时 2min36s  命中
2023-11-14 16:10:15
1
YB420CF01A46-0006009B012FF1D0-0-0
2023-11-14 16:12:51
>>> do  20        # 耗时 1s内     未命中,恢复正常
2023-11-14 16:12:51
1
YB420CF01A46-0006009AFEBDA7C6-0-0
2023-11-14 16:12:51
>>> do  21
2023-11-14 16:12:51
1
YB420CF01A46-0006009B016F1561-0-0
2023-11-14 16:12:52
...

可以观察到,命中该 Plan 的SQL 执行次数大于 20 次(含手工执行)后,该"不符合预期的" Plan 被淘汰。

4. 再次执行的SQL的 sql_audit 和 plan_cache_plan_stat,可看到重新生成了 Plan。

MySQL > select * from oceanbase.gv$sql_audit where trace_id='YB420CF01A46-0006009AFEBDA7C6-0-0'\G                                                                                                         
                 SQL_ID: 2B53F4C1C330C2C089C7518CD71D667A
              QUERY_SQL: select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31') as orginal limit 2000
...
          ELAPSED_TIME: 207
          PLAN_ID: 7334178
...
MEMSTORE_READ_ROW_COUNT: 1
 SSSTORE_READ_ROW_COUNT: 2               

MySQL [oceanbase]> select * from gv$plan_cache_plan_stat where plan_id=7334178 \G       
*************************** 1. row ***************************
...
              plan_id: 7334178
               sql_id: 2B53F4C1C330C2C089C7518CD71D667A
...
            statement: select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = ?) AND LAWENF_NTIST_TP_CD NOT LIKE ? AND LAWENF_NTIST_TP_CD NOT LIKE ? AND EMRG_STPY_SRC_CD != ? AND ACCTG_DT >= ? AND ACCTG_DT <= ?) as orginal limit 2000
            query_sql: select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31') as orginal limit 2000
...
      first_load_time: 2023-11-14 16:12:51.547434
...
     slowest_exe_time: 2023-11-14 16:12:51.547618
     slowest_exe_usec: 4139
...
         elapsed_time: 8279
...
           outline_id: -1
         outline_data: /*+ BEGIN_OUTLINE_DATA INDEX(@"SEL$2" "gabsdb.renzy"@"SEL$2" "renzy_I2") END_OUTLINE_DATA*/
...

5.obs日志关键信息

#grep YB420CF01A46-0006009B012FF1D0-0-0 observer.log.20231114161*|less
observer.log.20231114161017:[2023-11-14 16:10:15.813150] INFO  [SQL] ob_sql.cpp:1769 [86881][0][YB420CF01A46-0006009B012FF1D0-0-0] [lt=17] [dc=0] It is a large query, need delay, do not need disconnect(avg_process_time=123860984, exec_cnt=20, large_query_threshold=5000000, plan->get_plan_id()=7328133, ret=-4023)
observer.log.20231114161017:[2023-11-14 16:10:15.813208] TRACE [TRACE]obmp_base.cpp:156 [86881][0][YB420CF01A46-0006009B012FF1D0-0-0] [lt=18] [dc=0] [packet retry query](TRACE=begin_ts=1699949415813080 2023-11-14 08:10:15.813080|[start_sql] u=0 addr:{ip:"12.241.29.28", port:16606}|[process_begin] u=0 addr:{ip:"12.241.29.28", port:16606}, in_queue_time:13, receive_ts:1699949415813066, enqueue_ts:1699949415813067, trace_id:YB420CF01A46-0006009B012FF1D0-0-0|[session] u=3 sid:3221784053, tenant_id:1005|[parse_begin] u=10 stmt:"select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31') as orginal limit 2000", stmt_len:287|[process_end] u=85 run_ts:1699949415813082|total_timeu=98)
observer.log.20231114161302:[2023-11-14 16:12:51.412696] INFO  [SQL.ENG] ob_physical_plan.cpp:736 [86881][0][YB420CF01A46-0006009B012FF1D0-0-0] [lt=15] [dc=0] local query plan is expired due to unstable performance(bret=true, stat_.execute_times_=21, first_query_range_rows=0, sample_exec_row_count=1525906500, sample_count=20)
observer.log.20231114161302:[2023-11-14 16:12:51.412725] WARN  [SHARE.SCHEMA] revert (ob_schema_mgr_cache.cpp:131) [86881][0][YB420CF01A46-0006009B012FF1D0-0-0] [lt=11] [dc=0] long time to hold one guard(schema_mgr=0x7ee87934c610, tenant_id=1, version=1697523399752200, cur_timestamp=1699949571412714, ref_timestamp=1699949415812628, lbt()="0xf51231f 0x6158f04 0x4f5992c 0x50c61cc 0x4ed2f6f 0x4ecf518 0x4ecc8ef 0x4ecaa6e 0xb8c71f1 0x4ec9c90 0xb8c4d31 0x4ec58f6 0xb8c52a7 0xf3f17f3 0xf3f164f 0xf6901df")
observer.log.20231114161302:[2023-11-14 16:12:51.412738] WARN  [SHARE.SCHEMA] revert (ob_schema_mgr_cache.cpp:131) [86881][0][YB420CF01A46-0006009B012FF1D0-0-0] [lt=8] [dc=0] long time to hold one guard(schema_mgr=0x7edddba39170, tenant_id=1005, version=1699931135472584, cur_timestamp=1699949571412734, ref_timestamp=1699949415812628, lbt()="0xf51231f 0x6158f04 0x4f5992c 0x50c61cc 0x4ed2f6f 0x4ecf518 0x4ecc8ef 0x4ecaa6e 0xb8c71f1 0x4ec9c90 0xb8c4d31 0x4ec58f6 0xb8c52a7 0xf3f17f3 0xf3f164f 0xf6901df")
observer.log.20231114161302:[2023-11-14 16:12:51.412798] TRACE [TRACE]obmp_base.cpp:147 [86881][0][YB420CF01A46-0006009B012FF1D0-0-0] [lt=5] [dc=0] [slow query](TRACE=begin_ts=1699949415813229 2023-11-14 08:10:15.813229|[start_sql] u=0 addr:{ip:"12.241.29.28", port:16606}|[process_begin] u=0 addr:{ip:"12.241.29.28", port:16606}, in_queue_time:162, receive_ts:1699949415813066, enqueue_ts:1699949415813225, trace_id:YB420CF01A46-0006009B012FF1D0-0-0|[session] u=2 sid:3221784053, tenant_id:1005|[parse_begin] u=6 stmt:"select * from (SELECT COUNT(*) AS TOT_CNT FROM renzy WHERE (ACCT_NO = '6222620117273900882') AND LAWENF_NTIST_TP_CD NOT LIKE '12%' AND LAWENF_NTIST_TP_CD NOT LIKE '05%' AND EMRG_STPY_SRC_CD != 'JZ05' AND ACCTG_DT >= '1900-01-01' AND ACCTG_DT <= '2025-03-31') as orginal limit 2000", stmt_len:287|[exec_begin] u=29 arg1:false, end_trans_cb:false, plan_id:7328133|[do_open_plan_begin] u=8 |[sql_start_stmt_begin] u=1 |[sql_start_participant_begin] u=5 |[storage_table_scan_begin] u=56 |[storage_table_scan_end] u=116 |[get_row] u=155437570 |[result_set_close] u=161554 |[close_plan_begin] u=0 |[revert_scan_iter] u=96 |[end_participant_begin] u=3 |[start_end_stmt] u=1 |[affected_rows] u=0 affected_rows:-1|[store_found_rows] u=1 found_rows:0, return_rows:1|[auto_end_plan_begin] u=0 |[process_end] u=86 run_ts:1699949415813230|total_timeu=155599534)

第21次执行的SQL的关键日志信息:

[2023-11-14 16:12:51.412696] INFO  [SQL.ENG] ob_physical_plan.cpp:736 [86881][0][YB420CF01A46-0006009B012FF1D0-0-0] [lt=15] [dc=0] local query plan is expired due to unstable performance(bret=true, stat_.execute_times_=21, first_query_range_rows=0, sample_exec_row_count=1525906500, sample_count=20)

由该日志,关键信息如下:

1. sample_exec_row_count=1525906500

2. sample_count=20

3. first_query_range_rows=0

结合代码可知该结果满足 Plan 淘汰条件,从而 plan expire。

sample_exec_row_count / sample_count > first_query_range_rows * 10

1525906500 / 20  > 0 * 10  
// 这里 1525906500 的结果,不难得知,是单次SQL扫描行数 * 20. 
// 即(25416176 + 50832349) * 20 = 1524970500 约等于 1525906500 
 MEMSTORE_READ_ROW_COUNT: 25416176
 SSSTORE_READ_ROW_COUNT: 50832349

结论

1.本例主要是想分享SQL走错 Plan 而SQL慢的排查方法论,问题原因还是比较简单,重点是和大家分享处理OB遇到类似问题的思路等。

2.本例问题在当前OB 323版本中没有好的优化方式,给到的建议是:

  • 如果 I5 索引业务上未使用场景,则删除。
  • 绑定 outline,使该SQL走 I2 索引。

3.分享下OB中 Plan Cache 清理策略:

  • 手工清理
-- 租户内执⾏,清除当前租户中所有 Plan Cache。⽣产慎⽤。
ALTER SYSTEM FLUSH PLAN CACHE;
-- sys租户下执⾏,不同粒度。
ALTER SYSTEM FLUSH PLAN CACHE TENANT = 'T_MySQL';
ALTER SYSTEM FLUSH PLAN CACHE sql_id='B601070DFC14CB85FDA3766A69A9E1B3'
databases='myob1' tenant='tenant1' GLOBAL;
  • 自动清理 ob_plan_cache_percentage 参数控制 Plan Cache占用租户内存的百分比。 本例中提到

1. sample_count < SLOW_QUERY_SAMPLE_SIZE) :命中该Plan的SQL执行大于等于20次。 

2.sample_exec_row_count / sample_count > first_query_range_rows * 10 :(执行的SQL扫描总行数 / 执行次数) 大于 (第一次SQL执行扫描的行数 * 10)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1832866.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Spring的SmartLifecycle可以没用过,但没听过就不好了! - 第517篇

历史文章&#xff08;文章累计500&#xff09; 《国内最全的Spring Boot系列之一》 《国内最全的Spring Boot系列之二》 《国内最全的Spring Boot系列之三》 《国内最全的Spring Boot系列之四》 《国内最全的Spring Boot系列之五》 《国内最全的Spring Boot系列之六》 《…

HTML基础结构入门

HTML&#xff08;超文本标记语言&#xff09;是构建网页的基础语言。它用于描述网页的结构和内容。让我们从最基本的HTML文档开始。 HTML基础结构 一个基本的HTML文档结构如下&#xff1a; <!DOCTYPE html> <html lang"zh-CN"> <head><meta …

PMP证书有何用?

PMP证书有何用&#xff1f; PMP项目管理专业人士资格认证证书对从事或希望从事项目管理工作的人员有重要意义&#xff0c;具体体现在以下几个方面&#xff1a; 1. 提供职业机会&#xff1a; PMP是项目管理领域的国际认可标准&#xff0c;拥有该证书的人在求职时具备了更强的…

python-docx-template 的 Replace docx pictures 占位图片名称从哪来?

python-docx-template 的 Replace docx pictures 占位图片名称从哪来&#xff1f; 在 Word 中看占位图片名称用代码输出输出结果找对应图片 使用 replace_pic参考资料 在 Word 中看占位图片名称 右键图片 》查看可选文字 用代码输出 from docxtpl import DocxTemplate# 初始化…

【proteus仿真】基于51单片机的电压检测系统

【proteus仿真】基于51单片机的电压检测系统 资料下载地址&#xff1a;关注公众号 小邵爱电子 获取 1.前言 使用51单片机和ADC模块设计一个数字电压表&#xff0c;将模拟信号0~5V之间的电压转换为数字量信号&#xff0c;并通过LED实时显示电压数据 、 2.仿真原理图 3.硬件…

python安装目录文件说明----Dlls文件夹

在Python的安装目录下&#xff0c;通常会有一个DLLs文件夹&#xff0c;它是Python标准库的一部分。这个文件夹包含了一些动态链接库&#xff08;Dynamic Link Libraries&#xff0c;DLL&#xff09;&#xff0c;这些库提供了Python解释器和标准库的一些关键功能。以下是对这个文…

Linux(Centos7)OpenSSH漏洞修复,升级最新openssh-9.7p1

OpenSSH更新 一、OpenSSH漏洞二、安装zlib三、安装OpenSSL四、安装OpenSSH 一、OpenSSH漏洞 服务器被扫描出了漏洞需要修复&#xff0c;准备升级为最新openssh服务 1. 使用ssh -v查看本机ssh服务版本号 ssh -V虚拟机为OpenSSH7.4p1&#xff0c;现在准备升级为OpenSSH9.7p1…

Windows 与 Java 环境下的 Redis 利用分析

1 前言 在最近的一次攻防演练中&#xff0c;遇到了两个未授权访问的 Redis 实例。起初以为可以直接利用&#xff0c;但后来发现竟然是Windows Java (Tomcat)。因为网上没有看到相关的利用文章&#xff0c;所以在经过摸索&#xff0c;成功解决之后决定简单写一写。 本文介绍了…

洗地机性价比高的是哪一款?行内人告诉你

在浏览前&#xff0c;希望您轻触屏幕上方的“关注”按钮&#xff0c;让我后续为您带来更多实用且精彩的内容&#xff0c;感谢您的支持&#xff01; 洗地机作为现在的流行清洁工具&#xff0c;它的魅力之处在于&#xff1a;性价比极高&#xff0c;大多数家庭无需花费过多就能把…

java的Filter(过滤器),Interceptor(拦截器) 和 Aspect(切面)

文章目录 前言一、过滤器 (Filter) 基本用法二、拦截器 (Interceptor) 基本用法三、Aspect(切面)的基本用法总结 前言 Filter 是servlet层面的&#xff0c;由Servlet容器(如Tomcat)支持&#xff0c;只能在web程序中使用&#xff0c;实现了javax.servlet.Filter接口 Intercept…

2024北京智源大会

北京智源大会是年度国际性人工智能高端学术交流的盛会&#xff0c;定位于内行的AI盛会。智源大会紧密围绕当前人工智能学术领域迫切需要解决的问题&#xff0c;以及产业落地过程中存在的诸多挑战&#xff0c;开展深入探讨。智源研究院是2018年11月份成立的一家人工智能领域的新…

直播美颜SDK技术指南:实现实时美颜效果的算法方案

本篇文章&#xff0c;小编将探讨直播美颜SDK的技术实现和算法方案。 一、美颜技术概述 美颜技术通过一系列图像处理算法&#xff0c;实时美颜效果可以在视频直播过程中实时呈现&#xff0c;提升用户的直播体验。为了实现这些效果&#xff0c;需要结合图像处理和计算机视觉技术…

“奇点”临近,产业应用与人工智能怎样“共同进化”

以下文章来源&#xff1a;新华日报 眼下&#xff0c;以大模型为核心的人工智能技术呈现颠覆性突破。年初&#xff0c;OpenAI公司发布旗下的文生视频大模型Sora&#xff0c;距去年ChatGPT的横空出世&#xff0c;也仅仅过去一年。 “当前&#xff0c;人工智能迈向通用智能‘奇点’…

Hue Hadoop 图形化用户界面 BYD

软件简介 Hue 是运营和开发 Hadoop 应用的图形化用户界面。Hue 程序被整合到一个类似桌面的环境&#xff0c;以 web 程序的形式发布&#xff0c;对于单独的用户来说不需要额外的安装。

綦江蜘蛛池四川官网下载

baidu搜索&#xff1a;如何联系八爪鱼SEO? baidu搜索&#xff1a;如何联系八爪鱼SEO? baidu搜索&#xff1a;如何联系八爪鱼SEO? CCSEO蜘蛛统计开发思路一般包括以下几个步骤: 定义需求:明确统计蜘蛛访问数据的目标和要求,例如需要获取哪些信息,统计的精度和频率等。 确定数…

springboot小型超市商品展销系统-计算机毕业设计源码01635

摘 要 科技进步的飞速发展引起人们日常生活的巨大变化&#xff0c;电子信息技术的飞速发展使得电子信息技术的各个领域的应用水平得到普及和应用。信息时代的到来已成为不可阻挡的时尚潮流&#xff0c;人类发展的历史正进入一个新时代。在现实运用中&#xff0c;应用软件的工作…

thinkphp5使用模型删除与复杂查询EXP

模型删除 应用软删除 表中需要有字段&#xff0c;deletetime 模型中使用下面方法 use SoftDelete;protected $deleteTime delete_time;真实删除 // 软删除 User::destroy(1); // 真实删除 User::destroy(1,true); $user User::get(1); // 软删除 $user->delete(); // 真…

Python批量保存Excel文件中的图表为图片

Excel工作簿作为一款功能强大的数据处理与分析工具&#xff0c;被广泛应用于各种领域&#xff0c;不仅能够方便地组织和计算数据&#xff0c;还支持用户创建丰富多彩的图表&#xff0c;直观展示数据背后的洞察与趋势。然而&#xff0c;在报告编制、网页内容制作或分享数据分析成…

SFTP共享配置

SFTP一般指SSH文件传输协议&#xff0c;在计算机领域&#xff0c;SSH文件传输协议&#xff08;英语&#xff1a;SSH File Transfer Protocol&#xff0c;也称Secret File Transfer Protocol&#xff0c;中文&#xff1a;安全文件传送协议&#xff0c;英文&#xff1a;Secure FT…

FFmpeg中内存分配和释放相关的源码:av_malloc函数、av_mallocz函数、av_free函数和av_freep函数分析

一、av_malloc函数分析 &#xff08;一&#xff09;av_malloc函数的声明 av_malloc函数的声明放在在FFmpeg源码&#xff08;本文演示用的FFmpeg源码版本为5.0.3&#xff0c;该ffmpeg在CentOS 7.5上通过10.2.1版本的gcc编译&#xff09;的头文件libavutil/mem.h中&#xff1a;…