Я проектирую базу данных с одной таблицей для специального сценария, для которого мне нужно реализовать решение. Через короткое время таблица будет иметь несколько сотен миллионов строк, но каждая строка будет довольно компактной. Даже когда строк много, мне нужно, чтобы скорости вставки, обновления и выбора были хорошими и быстрыми, поэтому мне нужно выбрать лучшие индексы для работы.
Мой стол выглядит так:
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)
);
Запрос выбора будет выглядеть так:
select Name from Domain
where MetricType = @metricType
and DateProcessed is null
and DateToProcess < GETUTCDATE()
order by Priority desc, DateToProcess asc
Первый тип обновления будет выглядеть так:
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);
Второй тип обновления будет выглядеть так:
update Domain
set DateProcessed = source.DateProcessed
from @myTablePrm source
where Name = source.Name and MetricType = @metricType
Это лучшие показатели для оптимальной вставки, обновления и выбора скорости?
-- 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);