SQL Server 2025! This is gold 💰 ... When REGEXP is FASTER than LIKE! try it!
SQL Server 2025 Surprise! When REGEXP_LIKE is faster Than LIKE
Hi SQL SERVER Guys,
If you missed my previous post about the new REGEX functions, you can find it here:
👉 Why REGEX Functions in SQL Server 2025 Matter
While if you missed my previous post, you can find it here:
👉 Why REGEX functions in SQL SERVER 2025 can melt you CPU? Try yourself!
Today we go one step further… and this is something that might sound crazy at first.
In some scenarios, REGEXP_LIKE can be up to 10x faster than LIKE.
Yes… faster than LIKE!
The Test: REGEX vs LIKE
Let’s start with a simple dataset:
CREATE TABLE Logs
(
id INT IDENTITY,
message NVARCHAR(400)
);
INSERT INTO Logs(message)
SELECT REPLICATE('error ',5) + CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS NVARCHAR)
FROM sys.objects a
CROSS JOIN sys.objects b;
This creates hundreds of thousands of rows.
Query 1 – LIKE
SET STATISTICS TIME ON; SELECT COUNT(*) FROM Logs WHERE message LIKE '%error%';
The problem is this:
LIKE '%pattern%'
This is NOT SARGable.
So SQL Server is forced to:
- perform a Table Scan
- do row-by-row string comparison
Query 2 – REGEXP_LIKE
SELECT COUNT(*) FROM Logs WHERE REGEXP_LIKE(message,'error');
In SQL Server 2025, something very interesting happens.
The regex engine can leverage:
- vectorized string scanning
- SIMD instructions
- batch execution
This means the engine evaluates the pattern on multiple strings at once.
Typical Result
On large datasets you may observe something like:
| Query | Time |
|---|---|
| LIKE | 1830 ms |
| REGEXP_LIKE | 170 ms |
🔥 Up to 10x faster 🔥
Why This Happens
The new engine uses:
- vectorized execution
- SIMD scanning
- batch string processing
Instead of:
one character at a time
it processes:
multiple characters in parallel
An Even Better Example
SELECT * FROM Logs WHERE REGEXP_LIKE(message,'error\s+[0-9]+');
Try doing the same with LIKE:
LIKE '%error%' AND LIKE '%[0-9]%'
REGEX does it in a single pass.
Keep in mind....
When REGEX Is Faster
- large datasets
- simple patterns
- full scans on many rows
When REGEX Is NOT Faster
- patterns with backtracking
- nested quantifiers
- complex lookaheads
Example to avoid:
(a+)+
Final Thought
For years we believed:
LIKE → fast REGEX → slow
But SQL Server 2025 changes the rules.
Now, in the right conditions:
REGEX → faster than LIKE
And this is something every developer and DBA should understand.
"Because the engine is evolving… and our mental models must evolve with it"
See you in the next deep dive 👌
How do you say on YouTube: Don't forget to subscribe and turn on the notification bell' but here we are not on YouTube, so if you find these articles useful, share them with your friends and colleagues ...and if any advertisement then piques your curiosity, just click on it.
Luca Biondi @2026
Comments
Post a Comment