CREATE TABLE [dbo].[ATTrackingRecord] (
    [Id]                    INT              NOT NULL IDENTITY(1,1),
    [TrackingDate]          DATETIME         NOT NULL,
    [WorkItemId]            INT              NOT NULL,
    [UserName]              NVARCHAR (255)   NULL,
    [TrackingDefinitionId]  INT              NULL,
    [LastActionId]          INT              NULL,
    [Message]               NVARCHAR (MAX)   NULL,
    CONSTRAINT [PK_ATTrackingRecord] PRIMARY KEY NONCLUSTERED ([Id] ASC),
    CONSTRAINT [FK_ATTrackingRecord_ATWorkItem] FOREIGN KEY ([WorkItemId]) REFERENCES [dbo].[ATWorkItem] ([WorkItemId]),
    CONSTRAINT [FK_ATTrackingRecord_ATUser] FOREIGN KEY ([UserName]) REFERENCES [dbo].[ATUser] ([UserName]),
    CONSTRAINT [FK_ATTrackingRecord_ATButton] FOREIGN KEY ([LastActionId]) REFERENCES [dbo].[ATButton] ([Id]),
    CONSTRAINT [FK_ATTrackingRecord_ATTrackingDefinition] FOREIGN KEY ([TrackingDefinitionId]) REFERENCES [dbo].[ATTrackingDefinition] ([Id])
);


GO
CREATE NONCLUSTERED INDEX [ATTrackingRecord_Composite1]
    ON [dbo].[ATTrackingRecord]([UserName] ASC, [TrackingDate] ASC);


GO
CREATE NONCLUSTERED INDEX [ATTrackingRecord_Composite2]
    ON [dbo].[ATTrackingRecord]([LastActionId] ASC, [TrackingDate] ASC);

GO
CREATE NONCLUSTERED INDEX [SYSIDGUIDOrderd]
    ON [dbo].[ATTrackingRecord]([Id] ASC, [WorkItemId] ASC);


GO
CREATE NONCLUSTERED INDEX [WorkitemIDandDate]
    ON [dbo].[ATTrackingRecord]([TrackingDate] ASC, [WorkItemId] ASC);

