I am having a performance issue. I am trying to select from a table based on a very long list of parameters
Currently am using this stored proc
CREATE PROC [dbo].[GetFileContentsFromTitles]
@MyTitles varchar(max)
AS
SELECT [Title], [Sequence] From [dbo].[MasterSequence]
WHERE charindex(',' + Title + ',', ',' + @MyTitles + ',') > 0;
Where @MyTitles can be a very big number (currently doing a string with 4000 entries seperated by commas). Any suggestions? Thanks
OK, if you want performance for something like this, then you need to use the best stuff out there. First, create this function for splitting strings (which I got from Jeff Moden about two weeks ago):
Yes, it long, but that's mostly comments to explain it and its history. Don't worry, its the fastest thing available in T-SQL (AFAIK only SQLCLR is faster, and that's not T-SQL).
Note that it only supports up to VARCHAR(8000). If your really need VARCHAR(MAX), then it can be easily changed to that, but VARCHAR(MAX)'s are about twice as slow.
Now you can implement your procedure like this:
I cannot test this for you without your DDL and some data, but it should be much faster. If not, then we may need to throw an index onto the [item] column in the temp table.
Here's another version of the split function that uses VARCHAR(MAX):
Be forewarned, however, that I only set it up to count up to 100,000,000 characters. Also, I have not had a chance to test it yet, you should be sure to test it yourself.