Here is my take at it.
DECLARE @Keyword NVARCHAR(1000) = N'wwAMxxAMyyAMzz',
@Source NVARCHAR(1000) = N'AM',
@Synonym NVARCHAR(1000) = N'AN';
DECLARE @Permutations INT...
Slightly less cost. Reduce the WHERE clause like this
WITH C AS
SELECT col1, LEAD(col1, 1, 0) OVER(ORDER BY col1) AS nxt
SELECT TOP (1)...
Thank you Itzik for this excellent walk-through!
See my set-based solution in second part of this article.
1.5 seconds and 14 000 reads.
The problem is just another Relational Division; Ordered Relational Division. My solution finishes in just 1.5 seconds and uses about 14000 reads.
DECLARE @Items INT = (...