Abnormal SQL Execution Time

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

Original topic: SQL执行耗时异常

| username: CHENGX

[TiDB Usage Environment] Production Environment
[TiDB Version] v6.5.2
[Reproduction Path] Operations performed that led to the issue
[Encountered Issue: Problem Phenomenon and Impact] The execution time of the same type of SQL varies greatly, sometimes taking a few hundred milliseconds and sometimes a few minutes. An example is shown in the image below.

[Resource Configuration] Go to TiDB Dashboard - Cluster Info - Hosts and take a screenshot of this page

[Attachments: Screenshots/Logs/Monitoring]

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

Try executing the two SQL statements manually on the client. Is there a significant difference in speed?

| username: TiDBer_小阿飞 | Original post link

How do you determine the same type of SQL?

| username: Fly-bird | Original post link

We have encountered this as well. Manual execution should be very fast. This is influenced by the actual execution of other SQL in the environment. What can be done is to optimize this statement as much as possible.

| username: WalterWj | Original post link

The same type, but different query conditions, right? Could it be because querying one day’s data versus one year’s data is causing the difference?

| username: CHENGX | Original post link

It’s the same SQL with different conditions.

| username: CHENGX | Original post link

The query conditions are all primary keys.

| username: yeminhua | Original post link

This depends on the execution plan. You need to manually execute it with different conditions to see. It seems that the two execution plans might differ significantly, and the amount of data scanned under different conditions could vary greatly.

| username: CHENGX | Original post link

It seems that the optimization for joins is not very reliable with cost model 2. I used STRAIGHT_JOIN to replace JOIN, and the SQL execution performance improved significantly. I also tried using cost model 1, and the SQL execution efficiency was better than with cost model 2.

| username: andone | Original post link

Update the statistics.

| username: Kongdom | Original post link

It would be best to send the complete execution plan. The information in the screenshot is not very comprehensive, and some information is not visible.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.