This topic describes how to use the EXPLAIN statement to determine whether to use in-memory column indexes (IMCIs) to accelerate SQL queries.
Examples
In PolarDB for MySQL clusters, the execution plans for queries on data in columnar format are displayed in a horizontal tree format, which is different from the output format of the execution plans for queries on data in row-based format. You can execute the EXPLAIN statement to view the execution plan of SQL queries and determine whether to use IMCIs to accelerate the queries.
SQL query example:
EXPLAIN SELECT l_orderkey, SUM(l_extendedprice * (1 - l_discount)) AS revenue, o_orderdate, o_shippriority
FROM customer, orders, lineitem
WHERE c_mktsegment = 'BUILDING'
AND c_custkey = o_custkey
AND l_orderkey = o_orderkey
AND o_orderdate < date '1995-03-24'
AND l_shipdate > date '1995-03-24'
GROUP BY l_orderkey, o_orderdate, o_shippriority
ORDER BY revenue DESC, o_orderdate;Execution plan when the queried data is in row-based format:
+----+-------------+----------+------------+------+--------------------+------------+---------+-----------------------------+--------+----------+----------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+--------------------+------------+---------+-----------------------------+--------+----------+----------------------------------------------+
| 1 | SIMPLE | customer | NULL | ALL | PRIMARY | NULL | NULL | NULL | 147630 | 10.00 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | orders | NULL | ref | PRIMARY,ORDERS_FK1 | ORDERS_FK1 | 4 | tpch100g.customer.C_CUSTKEY | 14 | 33.33 | Using where |
| 1 | SIMPLE | lineitem | NULL | ref | PRIMARY | PRIMARY | 4 | tpch100g.orders.O_ORDERKEY | 4 | 33.33 | Using where |
+----+-------------+----------+------------+------+--------------------+------------+---------+-----------------------------+--------+----------+----------------------------------------------+
3 rows in set, 1 warning (0.00 sec)Execution plan (in horizontal tree format) when the queried data is in columnar format:
+----+----------------------------+----------+--------+--------+-----------------------------------------------------------------------------+
| ID | Operator | Name | E-Rows | E-Cost | Extra Info |
+----+----------------------------+----------+--------+--------+-----------------------------------------------------------------------------+
| 1 | Select Statement | | | | IMCI Execution Plan (max_dop = 4, max_query_mem = 858993459) |
| 2 | └─Sort | | | | Sort Key: revenue DESC,o_orderdate ASC |
| 3 | └─Hash Groupby | | | | Group Key: (lineitem.L_ORDERKEY, orders.O_ORDERDATE, orders.O_SHIPPRIORITY) |
| 4 | └─Hash Join | | | | Join Cond: orders.O_ORDERKEY = lineitem.L_ORDERKEY |
| 5 | ├─Hash Join | | | | Join Cond: customer.C_CUSTKEY = orders.O_CUSTKEY |
| 6 | │ ├─Table Scan | customer | | | Cond: (C_MKTSEGMENT = "BUILDING") |
| 7 | │ └─Table Scan | orders | | | Cond: (O_ORDERDATE < 03/24/1995) |
| 8 | └─Table Scan | lineitem | | | Cond: (L_SHIPDATE > 03/24/1995) |
+----+----------------------------+----------+--------+--------+-----------------------------------------------------------------------------+
8 rows in set (0.01 sec)References
An IMCI can access only the columns that the IMCI covers. If the tables or columns referenced in your SQL statement are not fully covered by the IMCI, the IMCI does not take effect.
To check whether an IMCI is created for a table referenced in a SQL statement, call the
dbms_imci.check_columnar_index()stored procedure. The stored procedure can parse the SQL statement that you entered, obtain all columns referenced in the statement, and check whether the columns are covered by an IMCI. For more information, see Check whether an IMCI is created for a table in a SQL statement.To obtain the DDL statement used to create an IMCI, call the
dbms_imci.columnar_advise()stored procedure. The stored procedure can return the DDL statement used to create an IMCI based on the SQL statement that you entered. For more information, see Obtain the DDL statement used to create an IMCI. After you execute the DDL statement, an IMCI is created to cover all columns referenced in the SQL statement.