Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: LIKE and ILIKE with exact match strangely slow #91887

Closed
michae2 opened this issue Nov 15, 2022 · 2 comments · Fixed by #91895
Closed

sql: LIKE and ILIKE with exact match strangely slow #91887

michae2 opened this issue Nov 15, 2022 · 2 comments · Fixed by #91895
Assignees
Labels
A-sql-execution Relating to SQL execution. C-performance Perf of queries or internals. Solution not expected to change functional behavior. E-quick-win Likely to be a quick win for someone experienced. T-sql-queries SQL Queries Team

Comments

@michae2
Copy link
Collaborator

michae2 commented Nov 15, 2022

The LIKE and ILIKE operators are strangely very slow when using an exact match. Even slower than with the same pattern wrapped with wildcards. Here's a demonstration using some different text operators:

SELECT count(i::text = ('%' || i::text || '%')) FROM generate_series(0, 999999) s(i);
SELECT count(i::text < ('%' || i::text || '%')) FROM generate_series(0, 999999) s(i);
SELECT count(i::text || ('%' || i::text || '%')) FROM generate_series(0, 999999) s(i);
SELECT count(position(i::text IN ('%' || i::text || '%'))) FROM generate_series(0, 999999) s(i);
SELECT count(trim(i::text FROM ('%' || i::text || '%'))) FROM generate_series(0, 999999) s(i);
SELECT count(i::text LIKE ('%' || i::text || '%')) FROM generate_series(0, 999999) s(i);
SELECT count(i::text ILIKE ('%' || i::text || '%')) FROM generate_series(0, 999999) s(i);
SELECT count(i::text ~ ('%' || i::text || '%')) FROM generate_series(0, 999999) s(i);
SELECT count(i::text ~* ('%' || i::text || '%')) FROM generate_series(0, 999999) s(i);

-- the strange cases
SELECT count(i::text LIKE i::text) FROM generate_series(0, 999999) s(i);
SELECT count(i::text ILIKE i::text) FROM generate_series(0, 999999) s(i);

Below is an unscientific best-of-five-runs comparison of PostgreSQL 14.6 and single-node CockroachDB v22.2.0 on the same hardware. Everything looks reasonable except for the last two cases:

operator PG 14.6 CRDB v22.2.0
= 290 ms 277 ms
< 826 ms 275 ms
|| 299 ms 319 ms
position 308 ms 374 ms
trim 408 ms 427 ms
LIKE with wildcards 320 ms 516 ms
ILIKE with wildcards 549 ms 535 ms
~ 7770 ms 2326 ms
~* 7959 ms 2665 ms
LIKE no wildcards 260 ms 4177 ms ???
ILIKE no wildcards 495 ms 4182 ms ???

Jira issue: CRDB-21469

@michae2 michae2 added C-performance Perf of queries or internals. Solution not expected to change functional behavior. A-sql-execution Relating to SQL execution. T-sql-queries SQL Queries Team labels Nov 15, 2022
@michae2
Copy link
Collaborator Author

michae2 commented Nov 15, 2022

I wonder if this is the same underlying problem as #89749?

@michae2
Copy link
Collaborator Author

michae2 commented Nov 15, 2022

According to profiling we're not using eval.optimizedLikeFunc for some reason. 🤔

michae2 added a commit to michae2/cockroach that referenced this issue Nov 15, 2022
`eval.optimizedLikeFunc` has special cases for LIKE patterns that start and
end with wildcards `%` or `_` which can be evaluated without regular
expressions. Add support for patterns with no wildcards at all.

Fixes: cockroachdb#91887
Assists: cockroachdb#89749

Epic: None

Release note (performance improvement): Performance of the `LIKE` and
`ILIKE` operators using patterns without any wildcards has been
improved.
@michae2 michae2 self-assigned this Nov 15, 2022
@michae2 michae2 added the E-quick-win Likely to be a quick win for someone experienced. label Nov 15, 2022
craig bot pushed a commit that referenced this issue Nov 16, 2022
91895: eval: add LIKE patterns without wildcards to optimizedLikeFunc r=DrewKimball,yuzefovich a=michae2

`eval.optimizedLikeFunc` has special cases for LIKE patterns that start and end with wildcards `%` or `_` which can be evaluated without regular expressions. Add support for patterns with no wildcards at all.

Fixes: #91887
Assists: #89749

Epic: None

Release note (performance improvement): Performance of the `LIKE` and `ILIKE` operators using patterns without any wildcards has been improved.

Co-authored-by: Michael Erickson <[email protected]>
@craig craig bot closed this as completed in 984f72b Nov 16, 2022
msirek pushed a commit to msirek/cockroach that referenced this issue Jun 30, 2023
`eval.optimizedLikeFunc` has special cases for LIKE patterns that start and
end with wildcards `%` or `_` which can be evaluated without regular
expressions. Add support for patterns with no wildcards at all.

Fixes: cockroachdb#91887
Assists: cockroachdb#89749

Epic: None

Release note (performance improvement): Performance of the `LIKE` and
`ILIKE` operators using patterns without any wildcards has been
improved.
@mgartner mgartner moved this to Done in SQL Queries Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-execution Relating to SQL execution. C-performance Perf of queries or internals. Solution not expected to change functional behavior. E-quick-win Likely to be a quick win for someone experienced. T-sql-queries SQL Queries Team
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant