TDB subqueries are much slower than on MySQL

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

Original topic: TDB 子查询 比在MySQL 上慢很多

| username: TiDBer_Z93z8JhD

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

【Overview】 Scenario + Problem Overview
Importing MySQL table into TiDB, TiDB query results are much slower than MySQL
【Background】 Actions taken
Tried adjusting parameters like tidb_distsql_scan_concurrency, tidb_executor_concurrency, tidb_index_join_batch_size, etc., but to no avail
【Phenomenon】 Business and database phenomena

【Problem】 Current issue encountered
SQL execution time is much slower on TiDB compared to MySQL
【Business Impact】

【TiDB Version】
6.1
【Application Software and Version】

【Attachments】 Relevant logs and configuration information

SQL statement:
SELECT t.blockcode, t.blockname, t.blocktype, MIN(t.tradday) tradday, t.cnt, t.stocks
FROM (
SELECT a.* FROM block_stocks_info a WHERE 2 >= (
SELECT COUNT(*)
FROM block_stocks_info b
WHERE a.blockcode = b.blockcode AND a.tradday <= b.tradday
) ORDER BY a.blockcode, a.tradday
) t GROUP BY blockcode;

Execution time on MySQL: 14s
Execution time on TiDB: 30s



| username: h5n1 | Original post link

  1. There is a significant difference between actrows and estrows evaluations. Manually collect the table’s statistics first and then take a look.
  2. Upload a more comprehensive execution plan to see if there are many historical version reads related to tikv_task statistics.
  3. This SQL looks awkward; it has an order by after count. It is recommended to rewrite it.
| username: TiDBer_Z93z8JhD | Original post link

image


New Text Document (3).txt (13.8 KB)

| username: buddyyuan | Original post link

  1. Try enabling this parameter.

  2. See if you can rewrite the subquery as an inner join.

| username: TiDBer_Z93z8JhD | Original post link

Effective, reduced from 30 seconds to 7 seconds.
Is there still room to reduce the time further?

| username: Tank001 | Original post link

:+1: We’re exploring, and we’ve directly opened it.

| username: TiDBer_Z93z8JhD | Original post link

After adjustment, the return time further accelerated from 7 seconds to 3 seconds.

| username: system | Original post link

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