To facilitate the query and management of cached execution plans, OceanBase Database provides the following views:
GV$OB_PLAN_CACHE_PLAN_STAT
The GV$OB_PLAN_CACHE_PLAN_STAT view records the specific information and execution statistics of each execution plan, and each cached plan has one record in this view. The following table describes the key columns in the view.
| Column | Description |
|---|---|
SVR_IP |
The server IP address of the database where the cached plan resides. |
SVR_PORT |
The server port of the database where the cached plan resides. |
PLAN_ID |
The ID of the execution plan for the SQL statement. |
SQL_ID |
The ID of the SQL statement. |
TYPE |
The type of the plan. |
QUERY_SQL |
The original SQL statement for which the plan was generated. |
FIRST_LOAD_TIME |
The time when the plan was generated. |
AVG_EXE_TIME |
The average execution time of the plan. |
EXECUTIONS |
The number of executions of the plan. |
CPU_TIME |
The total amount of CPU time consumed by all executions. |
OUTLINE_ID |
The ID of the outline used for plan generation. The value -1 indicates that the plan was not generated based on a bound outline. |
OUTLINE_DATA |
The information about the outline corresponding to the plan. |
From this view, you can identify and optimize SQL statements that consume the most CPU resources on the OBServer node, and thereby improve the performance of the database.
GV$OB_PLAN_CACHE_PLAN_EXPLAIN
The GV$OB_PLAN_CACHE_PLAN_EXPLAIN view records the shape of each execution plan in the plan cache. You can specify ip, port, tenant_id, and plan_id to query a specific tree-shaped plan. The following table describes the key columns in the view.
| Column | Description |
|---|---|
PLAN_LINE_ID |
The ID of the physical operator. |
OPERATOR |
The name of the physical operator. |
NAME |
The name of the table accessed by the physical operator. |
ROWS |
The number of result rows estimated for the physical operator. |
COST |
The cost estimated for the physical operator. |
PROPERTY |
The properties of the physical operator. |
The following example shows how to query a specific execution plan. The result is similar to the output of the EXPLAIN statement in OceanBase Database. Note that the plan returned from the GV$OB_PLAN_CACHE_PLAN_EXPLAIN view is the one generated for the first execution of the SQL statement, which may be inconsistent with the output of EXPLAIN. In this case, we need to check whether there is a bad case of the plan cache and determine whether to bind an execution plan to the SQL statement.
select plan_line_id, operator, name, rows, cost
from oceanbase.gv$ob_plan_cache_plan_explain
where ip = 'xx.xx.xx.xx'
and port = 30042
and tenant_id = 1001
and plan_id = 248;
+--------------+------------------------+--------------+---------+----------+
| plan_line_id | operator | name | rows | cost |
+--------------+------------------------+--------------+---------+----------+
| 0 | PHY_SORT | NULL | 6 | 16522 |
| 1 | PHY_HASH_GROUP_BY | NULL | 6 | 16506 |
| 2 | PHY_TABLE_SCAN | lineitem | 11473 | 6597 |
+--------------+------------------------+--------------+---------+----------+