I'm not sure if the subject really makes sense, but I was not sure how to say it. Here's the setting: I have an Item that has many ItemLogic , each of which has one Field . Each Item has, say, 25 ItemLogic entities. The logic determines whether this Item matches the given input from the form. For example, Field X has a value greater than A and Field Y has a value equal to B , etc. For each of the 25 fields.
In the current version of the application, all related objects are queried and looped, returning the first matching element, where all ItemLogic were true . It's a little expensive, but simple code, and it has never had such items. Still.
The application should now filter 3000 items to find a match. The previous query had at least two connections and took about 45 seconds on our SQL instance. This is too long.
The stored procedure seems natural, but here's the catch: the data is dynamic for each set of elements, it comes in as a string value and often needs to be distinguished as another type (DateTime or int most often) to perform actual comparisons, and some logic is ignored, not compares. This is a lot of extra overhead in a stored procedure, at least on how it affects me.
Alternatively, I could trim the data, but it will not save for the weak guy trying to match the last item in the collection.
What are some approaches that could be taken to expedite the match?
Scheme and some examples of data:
CREATE TABLE [dbo].[Items]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](255) NOT NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] CREATE TABLE [dbo].[ItemLogic]( [Id] [int] IDENTITY(1,1) NOT NULL, [ItemId] [int] NOT NULL, [FieldId] [int] NOT NULL, [Value] [nvarchar](max) NULL, [Comparison] [int] NOT NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] CREATE TABLE [dbo].[Fields]( [Id] [int] IDENTITY(1,1) NOT NULL, [Value] [nvarchar](max) NOT NULL, [Type] [int] NOT NULL, ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] INSERT INTO [dbo].[Fields] (Value, Type) VALUES ('abc', 0), ('def', 0), ('123', 1) INSERT INTO [dbo].[Items] (Name) VALUES ('Item 1'), ('Item 2'), ('Item 3') INSERT INTO [dbo].[ItemLogic] (ItemId, FieldId, Value, Comparison) VALUES (1, 1, 'xyz', 1), (1, 2, 'qrs', 1), (1, 3, '200', 0), (2, 1, 'abc', 1), (2, 2, 'xyz', 1), (2, 3, '123', 2), (3, 1, 'abc', 1), (3, 2, 'def', 1), (3, 2, '100', 0)
In the Comparison field, this is an enumeration match: 0 = Greater Than, 1 = Equal, 2 = Ignore. For the Type field, this is an enumeration match: 0 = string, 1 = int.
The expected result of the above match should be to return Item 3 .