Solved

Better/Quicker Way to Join on LIKE in SQL Server?

Posted on 2015-02-20
3
69 Views
Last Modified: 2015-03-03
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.

Email_Address	Customer_Categories
bob1@bob.com	592,593
bob2@bob.com	592,593,597
bob3@bob.com	602

Open in new window


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

Open in new window


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

Open in new window


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?
0
Comment
Question by:vacpartswarehouse
3 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
Sure is there a quicker solution: normalize your data. That's what gives you the best performance.

You need a split function when you want to do it inline, e.g. like here.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Have to agree with ste5an.  Your CustomerCategories column is not normalized and the Like argument in your SQL will take forever with large datasets.  Your customer Categories table should look like:

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.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now