A customer's database system experienced a performance failure, characterized by significant transaction congestion. The direct cause was that the CPU usage on some OceanBase Database server nodes reached 100%, preventing transactions from being processed normally. By querying the GV$OB_SQL_AUDIT view to capture and analyze information, it was identified that an problematic SQL statement averaged 2 seconds in duration and consumed over 70% of the server node's total CPU resources. Combining this with the EXPLAIN information and the DDL definitions of the related tables and views, the root cause of the performance issue was determined. After a series of DDL optimizations, the SQL statement's CPU time was reduced to 4 ms, completely resolving the performance bottleneck.
Troubleshooting Approach
The troubleshooting and optimization approach for this case is as follows:
Locate top SQL statements by querying the
GV$OB_SQL_AUDITview.Execute the following SQL statement to query the
GV$OB_SQL_AUDITview for the problematic SQL. In this case, mainly query information such as the execution frequency and CPU usage of SQL statements on the specified OBServer node (svr_ip) within the 30-minute period when the issue occurred (GROUP BY sql_id) compared to the total CPU usage during that period (SUM(execute_time)/(30*60*1000*1000) AS cpu_cnt).SELECT sql_id, COUNT(*) AS executions, SUM(execute_time) AS tot_cpu_time, AVG(execute_time) AS avg_cpu_time, SUM(execute_time)/(30*60*1000*1000) AS cpu_cnt, query_sql FROM oceanbase.GV$OB_SQL_AUDIT WHERE tenant_id= 'mysql001' AND svr_ip='xxx.xxx.xxx.xxx' AND request_time BETWEEN (TIME_TO_USEC(NOW())-30*60*1000*1000) AND TIME_TO_USEC(NOW()) AND is_executor_rpc =0 GROUP BY sql_id HAVING COUNT(*)>1 ORDER BY cpu_cnt DESC LIMIT 10;The query results show that SQL statements of the
SELECT ... FROM V_TT01 WHERE (COL001 IN (...))category have a high execution frequency and CPU usage, approximately 70% (80 c/108 c), and are concentrated on a single OBServer node.+----------------------------------+------------+--------------+--------------+---------+------------------------------------------------------------------------------------------------------------------------------------------------------+ | sql_id | executions | tot_cpu_time | avg_cpu_time | cpu_cnt | query_sql | +----------------------------------+------------+--------------+--------------+---------+------------------------------------------------------------------------------------------------------------------------------------------------------+ | 2705182A6EAB699CEC8E59DA80710B64 | 23,425 | 33,818,182,526 | 1,443,679 | 56 | SELECT ... FROM V_TT01 WHERE (COL001 IN (20017476)) | | 32AB97A0126F566064F84DDDF4936F82 | 7,112 | 7,926.295,919 | 1,114,496 | 13 | SELECT ... FROM V_TT01 WHERE (COL001 IN (20035891,20035892)) | | A5F514E873BE9D1F9A339D0DA7481D69 | 4,046 | 6,053,872,739 | 1,496,261 | 10 | SELECT ... FROM V_TT01 WHERE (COL001 IN (20017892,20007654,20025634)) | | 31FD78420DB07C11C8E3154F1658D237 | 186 | 646,926,207 | 3,478,098 | 1 | SELECT ... FROM V_TT01 WHERE (COL001 IN (20027596,20035891)) | | C48AEE941D985D8DEB66892228D5E845 | 28 | 569,004,505 | 20,321,589 | 1 | SELECT ... | | 101B7B79DFA9AE801BEE4F1A234AD294 | 155 | 440,190,675 | 2,839,940 | 1 | SELECT ... | | 1D0BA376E273B9D622641124D8C59264 | 423 | 429,140,148 | 1,014,516 | 1 | SELECT ... | | 64CF75576816DB5614F3D5B1F35B1472 | 113 | 326,338,159 | 2,887,948 | 1 | SELECT ... | | 23D1C653347BA469396896AD9B20DCA1 | 381 | 309.367.170 | 811,987 | 1 | SELECT ... | | FA4F493FA5CE2DCC64F51CF3754F96C6 | 7 | 289,191,515 | 41,313,074 | 0 | SELECT ... | +------------+----------------------------+--------------+------------+--------------+------------------------------------------------------------------------------------------------------------------------------------------------------+ 10 rows in setQuery the EXPLAIN information for the problematic SQL to analyze the key points affecting performance.
Running the
EXPLAINcommand provides the EXPLAIN information for this problematic SQL:================================================================================= |ID|OPERATOR |NAME |EST. ROWS|COST | --------------------------------------------------------------------------------- |0 |EXCHANGE IN REMOTE | |177 |432961| |1 | EXCHANGE OUT REMOTE | |177 |397632| |2 | SUBPLAN SCAN |V_TT01 |177 |397632| |3 | UNION ALL | |177 |397629| |4 | NESTED-LOOP JOIN CARTESIAN | |0 |151990| |5 | TABLE SCAN |TBL1(IDX_TBL1_COL001)|1 |92 | |6 | MATERIAL | |0 |151899| |7 | NESTED-LOOP ANTI JOIN CARTESIAN| |0 |151899| |8 | TABLE SCAN |TT01(IDX_TT01_COL001)|1 |92 | |9 | MATERIAL | |177 |151781| |10| SUBPLAN SCAN |VIEW1 |177 |151780| |11| TABLE SCAN |TBL3(IDX_TBL3_COL170)|177 |151777| |12| NESTED-LOOP JOIN CARTESIAN | |177 |236327| |13| TABLE GET |TBL2 |1 |46 | |14| TABLE SCAN |TBL3(IDX_TBL3_COL170)|177 |235510| ================================================================================= Outputs & filters: ------------------------------------- 5 - output(......), filter(nil), access(......), partitions(p0), is_index_back=true, range_key([TBL1.COL001(0x7f510ed97860)], [TBL1.COL002(0x7f48eec9baa0)]), range(20017476,MIN ; 20017476,MAX), range_cond([TBL1.COL001(0x7f510ed97860) = 20017476(0x7f510edbd750)]) 8 - output(......), filter(nil), access(......), partitions(p0), is_index_back=true, range_key([TT01.COL001(0x7f510ed97570)], [TT01.COL003(0x7f510ed97b50)]), range(20017476,MIN ; 20017476,MAX), range_cond([TT01.COL001(0x7f510ed97570) = 20017476(0x7f510edbcc50)]) 11 - output(......), filter([20017476 = cast(cast(TBL3.COL170(0x7f3f5b51c090), VARCHAR2(20 BYTE))(0x7f3f5b51d6b0), NUMBER(-1, -85))(0x7f3f5b51ce80)(0x7f3f5b51c380)]), access(......), partitions(p0), is_index_back=false, filter_before_indexback[false], range_key([TBL3.COL170(0x7f3f5b51c090)], [TBL3.COL002(0x7f3f5b528e80)]), range(MIN,MIN ; MAX,MAX)always true 13 - output(......), filter(nil), access(......), partitions(p0), is_index_back=false, range_key([TBL2.COL004(0x7f0cb0ff9f20)]), range[20017476 ; 20017476], range_cond([TBL2.COL004(0x7f0cb0ff9f20) = 20017476(0x7f0cb1001170)]) 14 - output(......), filter([20017476 = cast(cast(TBL3.COL170(0x7f0cb0ffa210), VARCHAR2(20 BYTE))(0x7f0cb1002fa0), NUMBER(-1, -85))(0x7f0cb1002770)(0x7f0cb1001c70)]), access(......), partitions(p0), is_index_back=true, filter_before_indexback[true], range_key([TBL3.COL170(0x7f0cb0ffa210)], [TBL3.COL002(0x7f3f5b582c10)]), range(MIN,MIN ; MAX,MAX)always trueBased on the above
EXPLAINinformation, the estimated total cost for executing this SQL is 432961. The cost is primarily reflected in operator 3UNION ALL(estimated cost 397629). Both branches ofUNION ALLcontain aNESTED-LOOP JOIN, with estimated costs of 151990 and 236327 respectively. The main cost for the first branch of theUNION ALLpart (from operator 4 to operator 11) is on operator 11, which performs aTABLE SCANthrough indexTBL3(IDX_TBL3_COL170). The main cost for the second branch (from operator 12 to operator 14) is also on operator 14, which performs aTABLE SCANthrough the same indexTBL3(IDX_TBL3_COL170). It appears that the high cost for operators 11 and 14 may both be due to theTBL3index, preliminarily identifying this as the bottleneck for the performance issue in this case.Next, analyze the Outputs & filters information for operators 11 and 14. The
range(MIN,MIN ; MAX,MAX)always trueindicates no index matching occurred during the SQL execution. Thefilter([20017476 = cast(cast(TBL3.COL170(0x7f0cb0ffa210), VARCHAR2(20 BYTE))(0x7f0cb1002fa0), NUMBER(-1, -85))(0x7f0cb1002770)(0x7f0cb1001c70)])indicates a data type conversion occurred during access. This suggests the data types of the associated fields might be inconsistent, leading to the conversion and preventing theTBL3 Colbert170index from being used for matching. Based on this conclusion, further analysis is conducted in conjunction with the DDL definitions.11 - output(......), filter([20017476 = cast(cast(TBL3.COL170(0x7f3f5b51c090), VARCHAR2(20 BYTE))(0x7f3f5b51d6b0), NUMBER(-1, -85))(0x7f3f5b51ce80)(0x7f3f5b51c380)]), access(......), partitions(p0), is_index_back=false, filter_before_indexback[false], range_key([TBL3.COL170(0x7f3f5b51c090)], [TBL3.COL002(0x7f3f5b528e80)]), range(MIN,MIN ; MAX,MAX)always true 14 - output(......), filter([20017476 = cast(cast(TBL3.COL170(0x7f0cb0ffa210), VARCHAR2(20 BYTE))(0x7f0cb1002fa0), NUMBER(-1, -85))(0x7f0cb1002770)(0x7f0cb1001c70)]), access(......), partitions(p0), is_index_back=true, filter_before_indexback[true], range_key([TBL3.COL170(0x7f0cb0ffa210)], [TBL3.COL002(0x7f3f5b582c10)]), range(MIN,MIN ; MAX,MAX)always trueAnalyze the DDL definitions of the related tables and views to determine the cause of the performance issue.
First, from the definition of view
V_TT01, it is known thattbl3exists in theNOT EXISTSsubquery within the first branch ofUNION ALL. Combined with theEXPLAINinformation, it can be concluded that the related subquery was rewritten as aNESTED-LOOP ANTI JOIN. Furthermore, the viewV_TT01undergoes aTO_NUMBERconversion, wherecol001andcol004are passed to theTBL3index(IDX_TBL3_COL170)viacol170.Next, analyzing the definition of table
TBL3, it is known that the indexTBL3(IDX_TBL3_COL170)is defined on fieldCOL170, with a data type ofVARCHAR2(20). This field is the one that associates viewV_TT01with other tables. From the definition of viewV_TT01, it is known that in the first branch ofUNION ALL, the association corresponding to operator 11 is"tbl3"."col170" = "tt01"."col001" = 20017476, and in the second branch, the association corresponding to operator 14 is"tbl3"."col170" = "tbl2"."col004" = 20017476. Due to the inconsistent data types of the associated fields, aTO_NUMBERconversion occurs, forcibly converting theVARCHAR2type ofCOL170toNUMBER. This matches thefilterinformation in the Outputs & filters of operators 11 and 14.Considering the DDL definitions and
EXPLAINinformation together, it is evident that during the execution of this category of SQL statements, the forcedTO_NUMBERconversion in viewV_TT01is unreasonable, leading to degraded index performance and high estimated costs for operators 11 and 14. TheTO_NUMBERconversion should be removed./*Definition of the problematic SQL statement*/ SELECT ...... FROM v_tt01 WHERE COL001 IN (20017476); /*Definition of view V_TT01*/ CREATE VIEW "V_TT01_OLD" AS (SELECT To_number("LOCAL_CXZX"."tt01"."col001") AS "COL001", ... ... FROM "LOCAL_CXZX"."tt01", "LOCAL_CXZX"."tbl1" WHERE ( "LOCAL_CXZX"."tt01"."col001" = "LOCAL_CXZX"."tbl1"."col001" ) AND NOT EXISTS((SELECT 1 FROM "LOCAL_CXZX"."tbl3" WHERE ( "LOCAL_CXZX"."tt01"."col001" = "LOCAL_CXZX"."tbl3"."col170" )))) UNION ALL (SELECT To_number("LOCAL_CXZX"."tbl2"."col004") AS "COL001", ... ... FROM "LOCAL_CXZX"."tbl2", "LOCAL_CXZX"."tbl3" "TBL3" WHERE ( "LOCAL_CXZX"."tbl2"."col004" = "tbl3"."col170" )) ; /*Definition of table TBL1*/ CREATE TABLE "LOCAL_BDZGBGYY"."TBL1" ( "COL001" NUMBER(16) NOT NULL , // 5: To_number("tt01"."col001") = "tbl1"."col001" = 20017476, To_number(NUMBER(16)) = NUMBER(20) ... ... PRIMARY KEY ("COL002") ) ; CREATE INDEX "LOCAL_BDZGBGYY"."IDX_TBL1_COL001" on "LOCAL_BDZGBGYY"."TBL1" ( "COL001" ) GLOBAL ; /*Definition of table TT01*/ CREATE TABLE "EINP_BASICINFO"."TT01" ( ... ... "COL001" NUMBER(20) NOT NULL, // 8: To_number("tt01"."col001") = "tbl1"."col001" = 20017476, To_number(NUMBER(16)) = NUMBER(20) ... ... PRIMARY KEY ("COL003") ) ; CREATE INDEX "EINP_BASICINFO"."IDX_TT01_COL001" on "EINP_BASICINFO"."TT01" ( "COL001" ) GLOBAL ; /*Definition of table TBL3*/ CREATE TABLE "LOCAL_BDZGBGYY"."TBL3" ( "COL170" VARCHAR2(20) NOT NULL, // 11: "tbl3"."col170" = "tt01"."col001" = 20017476, VARCHAR2(20) changed to NUMBER (20) ... ... // 14: "zb10"."col170" = "tbl2"."col004" = 20017476, VARCHAR2(20) is modified to NUMBER (20) PRIMARY KEY ("COL002") ) ; CREATE INDEX "LOCAL_BDZGBGYY"."IDX_TBL3_COL170" on "LOCAL_BDZGBGYY"."TBL3" ( "COL170" ) GLOBAL ; /*Definition of table TBL2*/ CREATE TABLE "SICP4_BASICINFOCENTER_EINP"."TBL2" ( "COL004" NUMBER(20) NOT NULL, // 13: COL004 = 20017476 ... .... PRIMARY KEY ("COL004") ) ;
Optimization Approach
The DDL optimization plan for the tables and views in this case is as follows:
- Remove the
TO_NUMBERtype conversion in the view. The optimizer will choose a better JOIN order, reducing the SQL response time from 2 seconds to 150 milliseconds. (The input parameter to the view is of theNUMBERdata type, andCOL001in the view is also of theNUMBERtype in the original table, so the forcedTO_NUMBERconversion in the view is unnecessary.) - Unify the data type definition of the associated fields in the table and view to
NUMBER(20). After avoiding type conversion,TBL3(IDX_TBL3_COL170)can be matched using an index, further reducing the SQL response time to 20 milliseconds. (BecauseTBL3 Colbert170is of theVARCHARdata type, the query conditionCOL001 IN ( value,value,...)in the view undergoes a type conversion fromVARCHARtoNUMBERwhen passed toTBL3 Colbert170, preventing the index onTBL3 Colbert170from being used for matching.) - Partition and reorganize the table groups for the four associated tables in the view. This view serves as the entry query view for business operations. Previously, SQL statements using this view caused the computing resources of a few OBServer nodes to become fully utilized. To prevent this hot view from becoming a bottleneck again, the query load on the view is distributed across various OBServer nodes through partitioning. Specifically, the four associated tables in the view are uniformly hashed and partitioned based on the association keys, and table groups are added to avoid cross-node join queries. After the reorganization, the SQL response time further drops to 4 milliseconds.
The following table describes the parameters of the EXPLAIN statement.
====================================================================
|ID|OPERATOR |NAME |EST. ROWS|COST|
--------------------------------------------------------------------
|0 |PX COORDINATOR | |3 |477 |
|1 | EXCHANGE OUT DISTR |:EX10000 |3 |405 |
|2 | SUBPLAN SCAN |V_TT01 |3 |405 |
|3 | PX PARTITION ITERATOR| |3 |405 |
|4 | UNION ALL | |3 |405 |
|5 | NESTED-LOOP JOIN | |2 |273 |
|6 | MERGE ANTI JOIN | |2 |183 |
|7 | TABLE SCAN |TT01(INDX_TT01_COL001) |2 |137 |
|8 | SUBPLAN SCAN |VIEW1 |1 |46 |
|9 | TABLE SCAN |TBL3(INDEX_TBL3_COL170)|1 |46 |
|10| TABLE SCAN |TBL1(INDEX_TBL1_COL001)|1 |44 |
|11| NESTED-LOOP JOIN | |1 |132 |
|12| TABLE SCAN |TBL3(INDEX_TBL3_COL170)|1 |92 |
|13| TABLE GET |TBL2 |1 |40 |
====================================================================