Using System Variable tidb_last_query_info to Query RU Issues

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 使用系统变量 tidb_last_query_info 查询RU问题

| username: TiDBer_tkwATiUa

The official documentation describes:

Use the system variable tidb_last_query_info to query the RU consumption of the last executed SQL statement

TiDB provides the system variable tidb_last_query_info, which records the information of the last executed DML statement, including the RU consumed by the SQL execution.

My question is:
Is the RU obtained by querying this variable limited to DML statements only? Or can it also check the RU consumed by DQL statements? Thank you.

| username: 有猫万事足 | Original post link

Sure, after all, it’s called query_info.
Besides, the time you spend waiting for a reply to this post might be faster if you try it yourself.
If you don’t have an environment, you can set up a TiDB serverless cluster to try it out yourself, it’s free.

| username: TiDBer_tkwATiUa | Original post link

I tried it myself and just wanted to confirm by posting. Thanks~

| username: TIDB-Learner | Original post link

Take some time to study and upgrade. Test the 8.1 version. I’m quite curious about the new features.

| username: 呢莫不爱吃鱼 | Original post link

The tidb_last_query_info is a system variable in TiDB used to store execution information of the last query statement. It is typically used to view various information about the last executed SQL statement, including resource consumption.

This variable mainly records information for DML (Data Manipulation Language) statements, such as INSERT, UPDATE, DELETE, etc. These operations usually involve data modification and thus consume Resource Units (RU).

However, for DQL (Data Query Language) statements, such as SELECT, SHOW, etc., tidb_last_query_info may not record detailed RU consumption information. Since DQL statements typically do not involve data modification, they do not directly consume RUs. Nevertheless, some complex queries, especially those involving significant computation or resource-consuming operations (such as sorting, aggregation, etc.), might be recorded in tidb_last_query_info.

To view the information recorded in tidb_last_query_info, you can execute the following query in TiDB’s SQL command line interface:

SELECT * FROM information_schema.tidb_last_query_info;

The information in tidb_last_query_info is only valid for a certain period after the query execution, controlled by the system variable tidb_query_info_expire_seconds. By default, this period might be a few minutes, but you can adjust it as needed.

If you want to understand the RU consumption of a specific SQL statement, the best approach is to examine the execution plan of that statement and analyze the resource consumption of each operation in the plan. This can be achieved using TiDB’s EXPLAIN statement.