I have two tables in database Products and Filters.
The schema:
I have created a query that find all the records from the filters table, loop with each record and call a procedure that set the category id for Products table.
Filter table data will be as follow.
The filter selection query is as follow..
DECLARE @TotalRecords INT, @Start INT, @Limit INT, @CatId INT, @Merchants NVARCHAR(max), @NotMatch NVARCHAR(max), @WillMatch NVARCHAR(max);
SELECT @TotalRecords = COUNT(*) FROM filters;
SET @Limit = 1;
SET @Start = 0;
WHILE(@TotalRecords > 0)
BEGIN
SELECT @CatId = category_id, @Merchants = merchant_name, @NotMatch = not_match, @WillMatch = will_match FROM
(
SELECT TOP (@Start + @Limit) *, ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS rnum
FROM filters
) a
WHERE rnum > @Start;
-- call filter procedure.
exec procSetProductCategory @CatId = @CatId, @Merchants = @Merchants, @WillMatch = @WillMatch, @NotMatch = @NotMatch;
SET @Start += 1;
SET @TotalRecords -= 1;
END
And the procSetProductCategory as follow..
CREATE PROC [dbo].[procSetProductCategory]
(
@CatId INT = NULL,
@Merchants NVARCHAR(max),
@NotMatch NVARCHAR(max),
@WillMatch NVARCHAR(max)
)
AS
BEGIN
SET NOCOUNT ON
declare @query nvarchar(max), @orToken nvarchar(max), @andToken nvarchar(max);
set @query = 'UPDATE Products SET category_id = '+ convert(nvarchar(20), @CatId) + ' WHERE category_id IS NULL AND merchant_name IN(' + @Merchants + ')';
if(@WillMatch is not null AND LTRIM(RTRIM(@WillMatch)) != '')
BEGIN
set @andToken = '%'' AND product_name LIKE ''%';
set @WillMatch = REPLACE(@WillMatch, '+', @andToken);
set @orToken = '%'') OR (product_name LIKE ''%';
set @query = @query + ' AND ((product_name LIKE '''+ '%' + REPLACE(@WillMatch, ',', @orToken) + '%''))';
END
if(@NotMatch is not null AND LTRIM(RTRIM(@NotMatch)) != '')
BEGIN
set @andToken = '%'' AND product_name NOT LIKE ''%';
set @NotMatch = REPLACE(@NotMatch, '+', @andToken);
set @orToken = '%'') OR (product_name NOT LIKE ''%';
set @query = @query + ' AND ((product_name NOT LIKE '''+ '%' + REPLACE(@NotMatch, ',', @orToken) + '%''))';
END
EXECUTE sp_executesql @query;
END
It generates the sql query like following...
Query #1
-------------------------------------------------------------------------------------------------------
UPDATE Products SET category_id = 101 WHERE merchant_name IN('merchant 1','merchant 4','merchant 3') AND
(
(product_name LIKE '%abcd%' AND product_name LIKE '%efhg%')
) AND (
(product_name NOT LIKE '%3258%')
OR (product_name NOT LIKE '%yxzs%')
)
Query #2
-------------------------------------------------------------------------------------------------------
UPDATE Products SET category_id = 102 WHERE merchant_name IN('merchant 3', 'merchant 4') AND
(
(product_name LIKE '%1258%') OR (product_name LIKE '%abcd%')
)
Note there are some trick used here.
[,] is used to differentiate match phrases. [+] in match fields used for two match phrases with AND conditions.
These query doing the same what I needed..
Issue is that when I run this query with 500 000 products its using about 100% CPU.
How can we optimize the query that doesn't take impact on result but can reduce the CPU usage?