I am creating a database with one table for a special scenario for which I need to implement a solution. After a short time, the table will contain several hundred million rows, but each row will be quite compact. Even when there are a lot of rows, I need to insert, update and choose the speed to be beautiful and fast, so I need to choose the best indexes for the job.
My table looks like this:
create table dbo.Domain ( Name varchar(255) not null, MetricType smallint not null, -- very small range of values, maybe 10-20 at most Priority smallint not null, -- extremely small range of values, generally 1-4 DateToProcess datetime not null, DateProcessed datetime null, primary key(Name, MetricType) );
The selection request will look like this:
select Name from Domain where MetricType = @metricType and DateProcessed is null and DateToProcess < GETUTCDATE() order by Priority desc, DateToProcess asc
The first type of update will look like this:
merge into Domain as target using @myTablePrm as source on source.Name = target.Name and source.MetricType = target.MetricType when matched then update set DateToProcess = source.DateToProcess, Priority = source.Priority, DateProcessed = case -- set to null if DateToProcess is in the future when DateToProcess < DateProcessed then DateProcessed else null end when not matched then insert (Name, MetricType, Priority, DateToProcess) values (source.Name, source.MetricType, source.Priority, source.DateToProcess);
The second type of update will look like this:
update Domain set DateProcessed = source.DateProcessed from @myTablePrm source where Name = source.Name and MetricType = @metricType
Are they the best indexes for optimal insertion, updating and speed selection?
-- for the order by clause in the select query create index IX_Domain_PriorityQueue on Domain(Priority desc, DateToProcess asc) where DateProcessed is null; -- for the where clause in the select query create index IX_Domain_MetricType on Domain(MetricType asc);