I'm using these two tables in a SQL Server 2014 database:
CREATE TABLE #FORMULAS
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[DataSourceAddress] [nvarchar](255) NULL,
[Description] [nvarchar](255) NULL
)
CREATE INDEX [IX_FORMULAS]
ON #FORMULAS ([DataSourceAddress] ASC)
CREATE TABLE #PARAMETERS
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Datetime] DateTime NULL,
[ParameterName] [nvarchar](255) NULL,
[Value] float NULL
)
CREATE INDEX [IX_PARAMETERS]
ON #PARAMETERS([ParameterName] ASC)
INSERT INTO #FORMULAS ([DataSourceAddress], [Description])
VALUES ('(parameter1+parameter2+parameter3)/3', 'Formula description 1'),
('parameter4', 'Formula description 2'),
('(parameter5*1000)', 'Formula description 3');
INSERT INTO #PARAMETERS ([Datetime], [ParameterName], [Value])
VALUES ('2016-01-26 11:40:00.000', 'parameter1', 1),
('2016-01-26 11:40:00.000', 'parameter2', 2),
('2016-01-26 11:40:00.000', 'parameter3', 3),
('2016-01-26 11:40:00.000', 'parameter4', 4),
('2016-01-26 11:40:00.000', 'parameter5', 5),
('2016-01-26 11:40:00.000', 'parameter6', 6),
('2016-01-26 11:44:00.000', 'parameter1', 1),
('2016-01-26 11:44:00.000', 'parameter2', 2),
('2016-01-26 11:44:00.000', 'parameter3', 3),
('2016-01-26 11:44:00.000', 'parameter4', 4),
('2016-01-26 11:44:00.000', 'parameter5', 5),
('2016-01-26 11:44:00.000', 'parameter6', 6);
I want to select the parameters of the table #FORMULAS
that match with the column [ParameterName]
of the table #PARAMETERS
.
For example, take the value '(parameter1+parameter2+parameter3)/3' of the table #FORMULAS
and receive all the rows of the table #PARAMETERS
that contains 'parameter1','parameter2','parameter3'
.
I'm using the following select:
SELECT
A.[Datetime],
A.[ParameterName],
B.[Description],
A.[Value]
FROM
#PARAMETERS A
JOIN
#FORMULAS B ON CHARINDEX(A.[ParameterName], B.[DataSourceAddress]) > 0
The #PARAMETERS
table has more than 160000 rows (with 1174 unique ParameterNames
), #FORMULAS
has 3283 rows. The query takes a lot of time to finish, more than 10 minutes.
Is there a faster way to do the query?