SQL Server 2025 finally gave us native regex, and everyone is swapping their old LIKE patterns for the shiny new REGEXP_LIKE. Microsoft documents anchored REGEXP_LIKE patterns as SARGable, meaning they can potentially benefit from index access. I anchored mine with '^', but SQL Server scanned a million rows anyway.
March 2025, Brent said don't get too excited.... Here is the measurement that suggests things remain the same 18 months later.
The Setup
One table, one million rows, one index on the column we search. Only 100 of those million rows start with 'Smith' -- that matters, because a rare value is exactly the case an index seek is built to win. Compatibility level 170 is required. REGEXP_LIKE does not exist below that CL.
IF DB_ID('RegexDemo') IS NULL
CREATE DATABASE RegexDemo;
GO
USE RegexDemo;
ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 170;
DROP TABLE IF EXISTS dbo.Customers;
GO
CREATE TABLE dbo.Customers
(
CustomerID INT IDENTITY PRIMARY KEY,
LastName VARCHAR(50) NOT NULL
);
/* 1,000,000 rows. Only 100 start with 'Smith' -- that is the point.
Everybody else starts with 'Acct'. Hard cap in TOP. */
;WITH Tally AS
(
SELECT TOP (1000000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n
FROM sys.all_columns a CROSS JOIN sys.all_columns b
)
INSERT dbo.Customers (LastName)
SELECT CASE WHEN n % 10000 = 0
THEN 'Smith' + CAST(n AS VARCHAR(10)) /* 100 rows */
ELSE 'Acct' + CAST(n AS VARCHAR(10)) /* the other 999,900 */
END
FROM Tally;
CREATE NONCLUSTERED INDEX idx_Customers_LastName
ON dbo.Customers (LastName);
The SELECT below returns only CustomerID and LastName. CustomerID is the clustered key, so it already rides inside the nonclustered index -- the index covers the query, gets rid of any key lookups, and the logical reads measure one thing cleanly: did we seek, or did we scan?
The Baseline: LIKE Seeks
Find every customer whose last name starts with Smith, the old way. STATISTICS IO on, actual execution plan on:
SET STATISTICS IO ON; GO SELECT CustomerID, LastName FROM dbo.Customers WHERE LastName LIKE 'Smith%';
Index Seek. Three logical reads to pull 100 rows out of a million. That's a number to remember.
The Regex The Docs say Should Have a Chance to Seek
Now the regex version. Microsoft's documentation identifies anchored patterns as SARGable, so I used ^Smith to express the same prefix search as LIKE 'Smith%'. The question is whether SQL Server will now take advantage of the existing index.
SELECT CustomerID, LastName FROM dbo.Customers WHERE REGEXP_LIKE(LastName, '^Smith');
Index Scan. 2,979 logical reads for the same 100 rows. The anchor is right there at the front of the pattern, the documentation says that makes it SARGable, and the optimizer scanned the entire index anyway.
The Scoreboard
| Query | Operator | Logical reads |
|---|---|---|
LIKE 'Smith%' |
Index Seek | 3 |
REGEXP_LIKE(LastName, '^Smith') |
Index Scan | 2,979 |
Same 100 rows. Same indexed column. The anchored regex cost roughly a thousand times the reads of the LIKE it was meant to replace.
Why 'SARGable' Didn't Save Me
SARGable means the optimizer is allowed to use a seek. It does not mean that it will. That choice comes down to cost, and cost comes down to how many rows the optimizer thinks it will find.
That is where things get interesting. SQL Server chose an index scan even though the pattern was anchored and the nonclustered index covered everything the query needed. Microsoft's documentation includes two query hints, ASSUME_FIXED_MIN_SELECTIVITY_FOR_REGEXP and ASSUME_FIXED_MAX_SELECTIVITY_FOR_REGEXP, specifically to adjust the default selectivity assumptions for regex predicates. That tells us selectivity estimation is an important consideration, but it doesn't prove that cardinality estimation caused this particular scan. What the execution plan does prove is that the optimizer didn't turn this anchored regex into the index seek we saw with LIKE.
The Takeaway
Do not swap LIKE for REGEXP_LIKE on a prefix search assuming the index will behave the same way. On my v2025 instance, the anchored regex scanned the index and required 2,979 logical reads to return the same 100 rows that the LIKE retrieved with just 3. That doesn't mean every anchored regex will scan, but it clearly demonstrates that SARGability alone is no guarantee of an efficient execution plan. REGEXP_LIKE earns its place where LIKE genuinely cannot do the job -- character classes, quantifiers, real pattern matching. For 'starts with Smith', the traditional LIKE 'Smith%' remains the better choice in this test.
Test it yourself before you trust it, and watch the reads. Rowcount is the same either way. Only the reads tell us what it costs.
/* Cleanup */ USE master; DROP DATABASE IF EXISTS RegexDemo;
More to read
MSFT -- REGEXP_LIKE (Transact-SQL)
Brent Ozar -- T-SQL Has Regex in SQL Server 2025. Don't Get Too Excited.




No comments:
Post a Comment