Answer the question
In order to leave comments, you need to log in
What is the best database structure for getting new records from the database?
The question arose of how best to organize the structure of the database / table for the fastest receipt of new records. There is a table in which new records are constantly added. In addition to the main data, the table has 2 fields:
Processed and RecordType (nullable). A non-unique index has been created for these fields.
There are several performers who are raking out new entries from the table.
SELECT ...
FROM [Table] AS [a]
WHERE ([a].[Processed] = 0) AND ([a].[RecordType] IS NULL OR ([a].[RecordType] = @__Type_0))
SELECT ...
FROM [Table] AS [a]
WHERE (([a].[Processed] = 0) AND [a].[RecordType] IS NOT NULL) AND ([a].[RecordType] = @__Type_0)
Answer the question
In order to leave comments, you need to log in
It looks like the Rubber Duck Programming methodology has worked again.
In my case, the index filter helped
CREATE NONCLUSTERED INDEX [IX_...]
ON [dbo].[Table]([Processed] ASC, [RecordType] ASC);
CREATE NONCLUSTERED INDEX [IX_...]
ON [dbo].[Table]([Processed] ASC, [RecordType] ASC) WHERE ([Processed]=(0));
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Table>()
.HasIndex(x => new { x.Processed, x.RecordType})
.HasFilter("Processed= 0")//NEW!!!
.IsUnique(false);
}
Because the optimizer includes a query by index. Look at the execution plan.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question