TiDB SQL Execution is Slow

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

Original topic: TIDB 执行SQL 慢

| username: 等一分钟

To improve efficiency, please provide the following information. A clear problem description will help resolve the issue faster:

【TiDB Usage Environment】
Production Environment

【Overview】 Scenario + Problem Overview
Multi-table join query, the amount of data queried is very small, but the query is very slow compared to MySQL

【Background】 Actions taken
Updated statistics

【Phenomenon】 Business and database phenomena

【Problem】 Current issue encountered

【Business Impact】

【TiDB Version】
v5.2.3

【Application Software and Version】
Using Navicat

【Attachments】 Relevant logs and configuration information
Query result


Execution plan

Comparison with MySQL result

| username: 等一分钟 | Original post link

Compared to MySQL query results, it is much slower.
It has a lot to do with expectations.

| username: xfworld | Original post link

Then just use MySQL~ :cowboy_hat_face:

| username: 人如其名 | Original post link

The personid in table A probably doesn’t have an index and needs to be added. Additionally, if table B’s b.scheduledate and b.personid don’t have indexes, add them. If they do, then gather statistics for table A.
Could you please share the MySQL execution plan?

| username: zhouzeru | Original post link

There are many reasons for this. Can you check the execution plan?

| username: 等一分钟 | Original post link

The execution plan is in the above diagram.

| username: 等一分钟 | Original post link

:bomb:

| username: xfworld | Original post link

To be honest, if the scale is not large enough, TiDB’s efficiency is not as good as MySQL’s… :custard:

| username: 等一分钟 | Original post link

The personid in table A has an index, and the scheduledate and personid in table B also have indexes. Statistics information was updated before execution in TiDB.

Execution plan in MySQL:

| username: 等一分钟 | Original post link

Large tables typically contain tens of millions of rows of data.

| username: 等一分钟 | Original post link

The scenarios provided by the user are exactly where TiDB needs to improve, or does TiDB not focus on these points…

| username: buddyyuan | Original post link

The execution plan is not fully displayed. Please upload a sanitized text version.

| username: xfworld | Original post link

Looking at the execution plan you provided, the data volume is not too large… it’s still a full table scan.

| username: 等一分钟 | Original post link

Sorry, I can’t assist with that.

| username: 等一分钟 | Original post link

TiDB did not choose the optimal execution plan for this SQL.

| username: buddyyuan | Original post link

Please provide the result of EXPLAIN ANALYZE, mainly to check actRows and execution info.
Note: EXPLAIN ANALYZE will execute the query for real.

| username: 等一分钟 | Original post link

Alright, please wait a moment.

| username: 等一分钟 | Original post link

explain2.xlsx (12.1 KB)

| username: 等一分钟 | Original post link

Specifically added a TiFlash node, but the effect is still not significant.

| username: buddyyuan | Original post link

It’s slow on Table A, and the estimated data doesn’t seem accurate. Check the statistics of this table.
SHOW STATS_HEALTHY A