I am trying to understand the use of Indexes to improve query performance (a broad task, I know). To pose my question, consider 2 simple tables: [Alarms] which has an AlarmID as its PK and [Events] which has AlarmID as a FK and EventID which represents an ordered sequence of Events (1,2,3,...) for each AlarmID. (The TSQL code for the schema is included at the bottom).
In the [Events] table, I have defined the PK as (AlarmID,EventID). If I do a lot of queries like
SELECT * FROM Alarms,Events WHERE (Alarms.AlarmID = Events.AlarmID) AND (AlarmID = 12)
would I benefit from creating a separate Index on [Events].AlarmID? As a usage issue, Events are rarely looked at on their own. Nearly 100% of the time, they are always viewed in the context of an Alarm object.
As a related question, does the order of the way I specify the PK for [Events] matter? For a clustered index, I think I would want to keep all the Events associated with an AlarmID near each other. If the answer to this question is YES, which should be specified first: AlarmID or EventID?
Any other suggestions/mods are most welcome.
Thnx,
Pete
CREATE TABLE [dbo].[Alarms] (
[AlarmID] [int] NOT NULL ,
[MoreStuff] [varchar] (50) COLLATE Latin1_General_BIN NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Events] (
[AlarmID] [int] NOT NULL ,
[EventID] [int] NOT NULL ,
[MoreStuff] [varchar] (50) COLLATE Latin1_General_BIN NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Alarms] WITH NOCHECK ADD
CONSTRAINT [PK_Alarms] PRIMARY KEY CLUSTERED
(
[AlarmID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Events] WITH NOCHECK ADD
CONSTRAINT [PK_Events] PRIMARY KEY CLUSTERED
(
[AlarmID],
[EventID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Events] ADD
CONSTRAINT [FK_Events_Alarms] FOREIGN KEY
(
[AlarmID]
) REFERENCES [dbo].[Alarms] (
[AlarmID]
) ON DELETE CASCADE NOT FOR REPLICATION
GO
In the [Events] table, I have defined the PK as (AlarmID,EventID). If I do a lot of queries like
SELECT * FROM Alarms,Events WHERE (Alarms.AlarmID = Events.AlarmID) AND (AlarmID = 12)
would I benefit from creating a separate Index on [Events].AlarmID? As a usage issue, Events are rarely looked at on their own. Nearly 100% of the time, they are always viewed in the context of an Alarm object.
As a related question, does the order of the way I specify the PK for [Events] matter? For a clustered index, I think I would want to keep all the Events associated with an AlarmID near each other. If the answer to this question is YES, which should be specified first: AlarmID or EventID?
Any other suggestions/mods are most welcome.
Thnx,
Pete
CREATE TABLE [dbo].[Alarms] (
[AlarmID] [int] NOT NULL ,
[MoreStuff] [varchar] (50) COLLATE Latin1_General_BIN NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Events] (
[AlarmID] [int] NOT NULL ,
[EventID] [int] NOT NULL ,
[MoreStuff] [varchar] (50) COLLATE Latin1_General_BIN NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Alarms] WITH NOCHECK ADD
CONSTRAINT [PK_Alarms] PRIMARY KEY CLUSTERED
(
[AlarmID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Events] WITH NOCHECK ADD
CONSTRAINT [PK_Events] PRIMARY KEY CLUSTERED
(
[AlarmID],
[EventID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Events] ADD
CONSTRAINT [FK_Events_Alarms] FOREIGN KEY
(
[AlarmID]
) REFERENCES [dbo].[Alarms] (
[AlarmID]
) ON DELETE CASCADE NOT FOR REPLICATION
GO