vacpartswarehouse
asked on
Better/Quicker Way to Join on LIKE in SQL Server?
Hello,
I have two sets of data I'm trying to join, but the query is so slow it times out even with very small sections of the data.
The first set of data is an email address and a comma-separated list of categories.
The second set of data is a list of SKUs which have one category ID associated with them on each row:
I've tried to create join the two result sets on a LIKE condition
but either there's too much data, the query is too slow, or both. Is there a better or more efficient way to do this sort of query in SQL Server?
I have two sets of data I'm trying to join, but the query is so slow it times out even with very small sections of the data.
The first set of data is an email address and a comma-separated list of categories.
Email_Address Customer_Categories
bob1@bob.com 592,593
bob2@bob.com 592,593,597
bob3@bob.com 602
The second set of data is a list of SKUs which have one category ID associated with them on each row:
productcode categoryid
2036814 592
2102602 593
2031181 597
2031212 597
2102602 599
2102602 602
2102602 608
2102602 610
2102602 611
I've tried to create join the two result sets on a LIKE condition
SELECT
CustomerTable.EmailAddress,
ProductTable.ProductCode
FROM
(SELECT EmailAddress, Customer_Categories FROM Customers) AS CustomerTable
JOIN
(SELECT ProductCode, CategoryID FROM Products) AS ProductTable
ON
CustomerTable.Customer_Categories LIKE ProductTable.CategoryID
but either there's too much data, the query is too slow, or both. Is there a better or more efficient way to do this sort of query in SQL Server?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can't understand why do you have 2 sub-queries. You can use a single query for that:
SELECT
Customers.EmailAddress,
Products.ProductCode
FROM Customers, Products
WHERE Customers.Customer_Categories LIKE Products.CategoryID
CustomerID CategoryID
1 592
1 593
2 592
2 593
2 597
3 602
and you should have a Customers table which contains the CustomerID and email address, along with other customer specific fields.