scorona85
scorona85

Reputation: 121

FirstOrDefault in DbSet slow using Linq

I have this Linq expression (FirstOrDefault) and it's very slow. Options is an DbSet

 DbOption dbo = ctx.Options.FirstOrDefault(o =>
                        o.SynN == oLoaded.SynN &&
                        o.OwnerId == oLoaded.OwnerId &&
                        o.Context == oLoaded.Context);

There is a way faster to find my options? I see with "Expression", but how can I convert this Linq code in Expression? I have never used it.

Thanks for the support

Upvotes: -2

Views: 112

Answers (1)

fubo
fubo

Reputation: 45947

In case of too many records, you can create a index to improve the performance of your query against the database

CREATE NONCLUSTERED INDEX [IX_Options_SynN_OwnerId_Context] ON [dbo].[Options]
(
    [SynN] ASC,
    [OwnerId] ASC,
    [Context] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO

Upvotes: 1

Related Questions