TiDB does not use index for 'ABC%'

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

Original topic: TIDB like ‘ABC%’ 不走索引

| username: lemonade010

[TiDB Usage Environment] Production Environment / Testing / PoC
[TiDB Version]
[Reproduction Path] What operations were performed when the issue occurred
[Encountered Issue: Problem Phenomenon and Impact]
Found that in the database execution plan, like ‘ABC%’ % cannot use the index and can only perform a full table scan. Please help analyze the reason.
[Resource Configuration] Go to TiDB Dashboard - Cluster Info - Hosts and take a screenshot of this page
[Attachments: Screenshots/Logs/Monitoring]

| username: Miracle | Original post link

Post the table structure and execution plan.

| username: TiDBer_QYr0vohO | Original post link

Please share the table structure for review.

| username: zhanggame1 | Original post link

Also, please provide the data volume. If the number of data rows is too small, the index will not be used.

| username: zhaokede | Original post link

Inaccurate statistics, table size and data distribution, index selectivity, query complexity, and other factors all have an impact, leading to not using the index. Check the execution plan for analysis.

| username: DBAER | Original post link

Check if the statistics are accurate, or if there are too many selected fields, causing the query index to go back to the table. It might be faster to directly scan the entire table.

| username: TIDB-Learner | Original post link

Explain and explain analyze analysis

| username: linnana | Original post link

Check if the index is effective, review the table’s metadata and health status using show status_meta and show status_healthy, and see if the statistics need to be updated.

| username: Kongdom | Original post link

:thinking: How about running explain analyze?

| username: lemonade010 | Original post link

Accuracy of statistical information is 100%.

| username: lemonade010 | Original post link

The image is not visible. Please provide the text you need translated.

| username: 我是人间不清醒 | Original post link

Try binding to force a specific index and see the effect.

EXPLAIN ANALYZE SELECT /*+ use_index('alias', 'index_name') */ * FROM t2 as alias WHERE a > 1 AND b = 1;

– Create binding

– Effective usage of Hint

CREATE GLOBAL BINDING for SELECT * FROM t2 as alias WHERE a > 1 AND b = 1
USING SELECT /*+ use_index('alias', 'index_name') */ * FROM t2 as alias WHERE a > 1 AND b = 1;

– Query binding

SHOW GLOBAL BINDINGS;

– Delete binding

DROP GLOBAL BINDING FOR SELECT * FROM t2 WHERE a > 1 AND b = 1;
| username: paulli | Original post link

This is a slow table lookup. It is recommended to use a composite index to filter most of the data.

| username: Kongdom | Original post link

:flushed: Can you also post the sentence? It looks like another index was chosen, and it seems like a SQL query can only use one index.

| username: caiyfc | Original post link

Will using LIKE filter more data than using start_time? If not, then using start_time is fine for now.
start_time is a range value, and even if it is a composite index with the LIKE field, it cannot use this index.

If using LIKE is more optimal, then use a hint to bind the index of the LIKE field and force it to use LIKE.

| username: caiyfc | Original post link

Moreover, considering the entire SQL, using an index join for table l and table d should be better, right?

| username: TIDB-Learner | Original post link

It seems to be using an index (range). Is it a partitioned table, crossing partitions, and then accessing the table again?

| username: tony5413 | Original post link

What is the specific SQL, the table data volume, and the result set data volume?

| username: 有猫万事足 | Original post link

It is best to try tiflash + mpp.

When the root operator is hashagg, the improvement will be significant.

| username: 健康的腰间盘 | Original post link

First, try to force it with a HINT. If it works, reanalyze the table statistics.