Skip to content

Conversation

ChangRui-Ryan
Copy link
Contributor

@ChangRui-Ryan ChangRui-Ryan commented Sep 3, 2025

What problem does this PR solve?

Issue Number: close #10399

Problem Summary:
When performing a LIKE operation on strings in SQL, the underlying code logic varies depending on the collation used, leading to significant performance differences.
Currently, the LIKE operation performs well with BIN-type collations, but its performance with CI-type collations is poor—with a substantial gap compared to BIN-type collations.

What is changed and how it works?

Key Optimizations

1. Prioritize ASCII_CI Comparison for LIKE Patterns

Check if the LIKE pattern consists of only ASCII characters first. If so, prioritize case-insensitive (CI) comparison using the small (128-entry) ASCII character table (avoiding expensive UTF-8 decoding initially). Fall back to UTF-8 decoding only when non-ASCII characters are detected during matching.

2. Optimize Backtracking Logic for Patterns Starting with "%"

When a pattern starts with %, restart backtracking directly from the characters after % (instead of restarting from the % itself). For scenarios where the first character after backtracking does not match, simplify the flow by immediately advancing the string pointer to skip redundant loops, significantly reducing total operations.

Benchmark

To eliminate interference and facilitate comparison, a single-node TiFlash was used, and single concurrency was configured with the following setting:
set tidb_max_tiflash_threads=1;
For the query (under TPCH 10G data):
SELECT COUNT(*) FROM orders WHERE o_comment LIKE ?;
The comparison of execution time before and after optimization is as follows (unit: seconds):

PATTERN Collation Before Optimization After Optimization Improvement Multiple
"%ZZZ%"; utf8mb4_general_ci 4.1 0.8 5.1
"%ZZZ%"; utf8mb4_0900_ai_ci 5.3 0.8 6.6
"%中国%"; utf8mb4_general_ci 4.0 1.1 3.6
"%中国%"; utf8mb4_0900_ai_ci 5.3 2.5 2.1
"%special%requests%" (From TPCH Q13) utf8mb4_general_ci 5.0 1.7 2.9
"%special%requests%" (From TPCH Q13) utf8mb4_0900_ai_ci 6.0 1.7 3.5

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 3, 2025
@ChangRui-Ryan ChangRui-Ryan force-pushed the like_perf branch 2 times, most recently from 984925f to 7c9ce8b Compare September 4, 2025 09:18
@ChangRui-Ryan
Copy link
Contributor Author

/retest

@ChangRui-Ryan
Copy link
Contributor Author

/retest

1 similar comment
@ChangRui-Ryan
Copy link
Contributor Author

/retest

@ChangRui-Ryan ChangRui-Ryan changed the title [WIP] Improve like perf for utf8 ci collations Improve like perf for utf8 ci collations Sep 9, 2025
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 9, 2025
}

template <typename Collator>
void Pattern<Collator>::try_compile_ascii_ci(const std::string & pattern, char escape)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use camelCase for function names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Comment on lines 184 to 188
auto ret = try_match_ascii_ci(s, length);
if (ret == 1)
{
return true;
}
else if (ret == 0)
{
return false;
}
// if ret == -1, means the string contains non-ASCII characters, continue to check
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can simplify to something like:

if (auto ret = try_match_ascii_ci(s, length); ret >= 0) {
  return ret;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

virtual ~IPattern() = default;

virtual void compile(const std::string & pattern, char escape) = 0;
virtual void try_compile_ascii_ci(const std::string & pattern, char escape) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase

else
{
if (s[str_idx] < 0)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

return -1;
}
if ((match_types[p_idx] == Match
&& weight_ascii_ci[s[str_idx]] == weight_ascii_ci[ascii_ci_pattern[p_idx]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just save the converted pattern in ascii_ci_pattern during try_compile_ascii, then during match, there is no need to lookup the weight_ascii_ci for pattern chars?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

@ChangRui-Ryan ChangRui-Ryan force-pushed the like_perf branch 4 times, most recently from 44cee58 to fe39c84 Compare September 17, 2025 02:39
const std::string & s = inner_c.first;
res[idx] = pattern->match(s.data(), s.length());
bool ans = std::get<Collator::collation_case>(inner_c.second);
std::cout << "Pattern case (" << p << ", " << inner_c.first << ", " << ans << ")" << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line should be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line of code is from the original line 352. To facilitate locating specific cases when errors occur, let's leave it unchanged this time?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@ChangRui-Ryan
Copy link
Contributor Author

/retest

Copy link
Contributor

@windtalker windtalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Sep 18, 2025
Copy link
Contributor

ti-chi-bot bot commented Sep 18, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: solotzg, windtalker

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 18, 2025
Copy link
Contributor

ti-chi-bot bot commented Sep 18, 2025

[LGTM Timeline notifier]

Timeline:

  • 2025-09-18 03:24:28.011135469 +0000 UTC m=+70229.489716115: ☑️ agreed by windtalker.
  • 2025-09-18 09:32:13.649191714 +0000 UTC m=+92295.127772382: ☑️ agreed by solotzg.

@ChangRui-Ryan
Copy link
Contributor Author

/retest

2 similar comments
@ChangRui-Ryan
Copy link
Contributor Author

/retest

@ChangRui-Ryan
Copy link
Contributor Author

/retest

@ti-chi-bot ti-chi-bot bot merged commit f455495 into pingcap:master Sep 19, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve like perf for utf8 CI collation
4 participants