Skip to content

[6.x] Work the pattern for a like where out once - #15895

Merged
jasonvarga merged 3 commits into
statamic:6.xfrom
DanielDarrenJones:perf/6x-like-where-pattern
Nov 13, 2026
Merged

jasonvarga merged 3 commits into
statamic:6.xfrom
DanielDarrenJones:perf/6x-like-where-pattern

Conversation

@DanielDarrenJones

Copy link
Copy Markdown
Contributor

A like where works out the same regular expression again for every item it's tested against.

filterTestLike() turns the SQL-style pattern into a regex with Pattern::sqlLikeToRegex() each time it's called, and it's called once per item. The pattern belongs to the where, so it's the same every time.

It's now worked out the first time and remembered on the query builder, by the string being looked for. It's kept on the instance rather than statically, so a long-running worker doesn't collect every search anyone has made. not like goes through the same method, and a value that isn't a string is converted each time as before.

This is the path behind where('title', 'like', ...) in a Control Panel listing search when there's no search index, the contains, starts_with and ends_with conditions on tags, and user and form submission searches.

Numbers

Converting the pattern for each of 2,000 items takes 6.7 ms; doing it once and matching 2,000 items takes 0.5 ms.

Queries on a collection of 2,000 entries, average of 20 runs:

before after
where('title', 'like', '%amber%') 7.0 ms 0.7 ms
two likes, one an orWhere 13.7 ms 1.1 ms
where('title', 'not like', '%a%') across every collection 9.8 ms 0.95 ms

Each finds the same entries as before.

Tests

The pattern is asked for once for a query that tests several entries (fails without the change), and a query with four different likes still applies each of them.

@jasonvarga
jasonvarga merged commit f051cbc into statamic:6.x Nov 13, 2026
61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants