How to Query Historical SQL

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

Original topic: 如何查询历史sql

| username: 路在何chu

[TiDB Usage Environment] Production Environment 4013
Which view does the SQL statement analysis section of the TiDB dashboard query? It only shows the most recent one. How can I view all historical SQL?
[Encountered Problem: Problem Phenomenon and Impact]
The system tables like events_statements_history_long are also unusable.

| username: 啦啦啦啦啦 | Original post link

Refer to this:

| username: 像风一样的男子 | Original post link

To view all historical SQL, you need to use auditing, which is available in the enterprise edition.

| username: xfworld | Original post link

You can collect it yourself, and the system’s historical statistics also have a time range.

| username: tidb菜鸟一只 | Original post link

The main ones are these two tables, statements_summary_history and cluster_statements_summary_history.