Mybatis Query Causes TiDB Index Failure

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

Original topic: Mybatis查询导致TiDB索引失效

| username: TiDBer_mGBoAnW9

To improve efficiency, please provide the following information. Clear problem descriptions can be resolved faster:
[TiDB Usage Environment] TiDB 6.1

[Overview]
[SQL]
SELECT
a.*
FROM
records a
LEFT JOIN company b ON b.eid = a.eid
WHERE
b.id = ?
ORDER BY
a.change_date DESC
Query business records under the company by the company ID in table b. The id in table b is the primary key, eid is the index key, table a has no primary key, and the unique key is composed of eid and u_id, with eid being the associated field.

Navicat query time

[Problem] Data migrated from MySQL to TiDB, the original table’s auto-increment primary key was discarded, and a multi-field composite unique key was adopted. The original business query SQL in Navicat has no issues and takes 0.3s, but in the program, Mybatis queries are very slow, taking tens of seconds or even minutes. The database connection pool used is Hikari.


If the question is related to performance optimization or fault troubleshooting, please download the script and run it. Please select all and copy-paste the terminal output results for uploading.

| username: Icemap | Original post link

Should the LEFT JOIN company a here be LEFT JOIN company b?
I see that your screenshot has the SQL printed by MyBatis running, could you copy it?

| username: Icemap | Original post link

Also, please provide the table structure. I will try to reproduce it locally.

| username: TiDBer_mGBoAnW9 | Original post link

I just made a mistake, the company table should be table b.

| username: 边城元元 | Original post link

Could you please share the execution plan?

| username: 边城元元 | Original post link

You can view detailed information on the SQL analysis in the dashboard.

| username: Tank001 | Original post link

Send the table structure, I’ll give it a try. If it doesn’t work, we can create a composite index.

| username: Hacker007 | Original post link

Confirm where the issue is. You can check the execution time of the SQL in the dashboard’s slow SQL query. If it is very slow, then check the resource usage of TiDB.

| username: Z六月星星 | Original post link

Check the execution plan of the SQL.

| username: ddhe9527 | Original post link

You still need to specifically look at the execution plan of your SQL. First of all, your SQL is a fake outer join, and the optimizer will directly rewrite it into the following inner join:

SELECT a.*
  FROM records a, company b 
 WHERE a.eid = b.eid
   AND b.id = ?
 ORDER BY a.change_date DESC

Then, according to your description, it is estimated that the JOIN method between table a and table b has changed:

  1. If table a drives table b, then table a will be a full table scan, and table b will use the eid index (IndexJoin) or the id primary key (HashJoin).
  2. If table b drives table a, then table b will use the id primary key, and table a will use the (eid, u_id) index (IndexJoin) or a full table scan.
    So there are roughly at least the above 4 JOIN methods. Which one is better depends on the scale of the number of rows in table a and the filtering ability of the a.eid field. Therefore, you need to compare the execution plans of SQL under Mybatis and Navicat.
| username: xuexiaogang | Original post link

b.eid = a.eid

b.id =
Check these columns, do they all have indexes?

| username: ti-tiger | Original post link

You can check the execution plan to see if there is a full table scan.

| username: system | Original post link

This topic will be automatically closed 60 days after the last reply. No new replies are allowed.