Non-clustered indexes for columns defined within a composite Primary KeyParent-Child Tree Hierarchical...

Is divide-by-zero a security vulnerability?

A bug in Excel? Conditional formatting for marking duplicates also highlights unique value

Citing contemporaneous (interlaced?) preprints

Where is the line between being obedient and getting bullied by a boss?

Test pad's ESD protection

If nine coins are tossed, what is the probability that the number of heads is even?

Adding days to the Date portion of DateTime throws off the Time portion

Is there a frame of reference in which I was born before I was conceived?

Misplaced tyre lever - alternatives?

Why do members of Congress in committee hearings ask witnesses the same question multiple times?

What are the issues with an additional (limited) concentration slot instead of Bladesong?

What is this waxed root vegetable?

If a set is open, does that imply that it has no boundary points?

A right or the right?

Real life puzzle: Unknown alphabet or shorthand

Achieving MPPT of a solar panel with LM2596

Inverse of the covariance matrix of a multivariate normal distribution

Reason why dimensional travelling would be restricted

In iTunes 12 on macOS, how can I reset the skip count of a song?

Why are special aircraft used for the carriers in the united states navy?

Where is the fallacy here?

Forward slip vs side slip

Dredging in a fantasy setting

Plagiarism of code by other PhD student



Non-clustered indexes for columns defined within a composite Primary Key


Parent-Child Tree Hierarchical ORDEROracle GoldenGate add trandata errorsSHOWPLAN does not display a warning but “Include Execution Plan” does for the same queryMysql might have too many indexesShould I mark a composite index as unique if it contains the primary key?SQL Server equivalent of Oracle USING INDEX clauseExcluding clustered key columns from non-clustered indexes definitionsdeteriorating stored procedure running timesShould I remove this clustered index?Single or multiple non-clustered indexes when pairing with a composite primary key













0















I have a multi-tenant database where I am utilizing an AccountId column within all tables as a part of the composite Primary Key for tenant isolation. Is it beneficial to create additional non-clustered indexes on each column that is part of the composite Primary Key to assist SQL Server with maintaining accurate statistics and improving query performance when joining to lookup tables?



For instance, within an associative table that defines one-to-many relationships between an Account and the American State in which they have offices, in theory which of the two options is preferable given the following structure and sample query?



Create the Account and State tables and populate with sample data.



DROP TABLE IF EXISTS [dbo].[Account];
DROP TABLE IF EXISTS [dbo].[State];

-- [Account] table and sample values.
IF OBJECT_ID('[dbo].[Account]', 'U') IS NULL
BEGIN
CREATE TABLE [dbo].[Account] (
[AccountId] [int] IDENTITY(1,1) NOT NULL
,[AccountAlias] [varchar](3) NOT NULL
,[AccountName] [varchar](128) NOT NULL
,CONSTRAINT [PK_Account] PRIMARY KEY CLUSTERED ([AccountId] ASC)
,CONSTRAINT [UQ_Account_Alias] UNIQUE NONCLUSTERED ([AccountAlias] ASC)
,CONSTRAINT [UQ_Account_Name] UNIQUE NONCLUSTERED ([AccountName] ASC)
);

SET IDENTITY_INSERT [dbo].[Account] ON;

INSERT INTO [dbo].[Account] ([AccountId], [AccountAlias], [AccountName])
VALUES (1, 'SA1', 'Sample Account 1'), (2, 'SA2', 'Sample Account 2'), (3, 'SA3', 'Sample Account 3')

SET IDENTITY_INSERT [dbo].[Account] OFF;
END;
GO

-- [State] table and sample values.
IF OBJECT_ID('[dbo].[State]', 'U') IS NULL
BEGIN
CREATE TABLE [dbo].[State] (
[StateId] [tinyint] IDENTITY(1,1) NOT NULL
,[StateCode] [varchar](2) NOT NULL
,[StateName] [varchar](32) NOT NULL
,CONSTRAINT [PK_State] PRIMARY KEY CLUSTERED ([StateId] ASC)
,CONSTRAINT [UQ_State_Code] UNIQUE NONCLUSTERED ([StateCode] ASC)
,CONSTRAINT [UQ_State_Name] UNIQUE NONCLUSTERED ([StateName] ASC)
);

SET IDENTITY_INSERT [dbo].[State] ON;

INSERT INTO [dbo].[State] ([StateId], [StateCode], [StateName])
VALUES (1, 'AL', 'Alabama'), (2, 'AK', 'Alaska'), (3, 'AZ', 'Arizona'), (4, 'AR', 'Arkansas'), (5, 'CA', 'California')

SET IDENTITY_INSERT [dbo].[State] OFF;
END;
GO


Create AccountState OPTION 1 - composite Primary Key only



DROP TABLE IF EXISTS [dbo].[AccountState];

IF OBJECT_ID('[dbo].[AccountState]', 'U') IS NULL
BEGIN
CREATE TABLE [dbo].[AccountState] (
[AccountId] [int] NOT NULL
,[StateId] [tinyint] NOT NULL
,CONSTRAINT [PK_AccountState] PRIMARY KEY CLUSTERED ([AccountId] ASC, [StateId] ASC)
,CONSTRAINT [FK_AccountState_Account] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account]([AccountId])
,CONSTRAINT [FK_AccountState_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State]([StateId])
);

INSERT INTO [dbo].[AccountState] ([AccountId], [StateId])
SELECT A.[AccountId], S.[StateId]
FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
WHERE A.[AccountId] = 1 AND S.[StateId] IN (1, 2, 3)
UNION
SELECT A.[AccountId], S.[StateId]
FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
WHERE A.[AccountId] = 2 AND S.[StateId] IN (3, 4, 5)
UNION
SELECT A.[AccountId], S.[StateId]
FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
WHERE A.[AccountId] = 3 AND S.[StateId] IN (1, 3, 5)
END;
GO


Create AccountState OPTION 2 - composite Primary Key + non-clustered indexes



DROP TABLE IF EXISTS [dbo].[AccountState];

IF OBJECT_ID('[dbo].[AccountState]', 'U') IS NULL
BEGIN
CREATE TABLE [dbo].[AccountState] (
[AccountId] [int] NOT NULL
,[StateId] [tinyint] NOT NULL
,CONSTRAINT [PK_AccountState] PRIMARY KEY CLUSTERED ([AccountId] ASC, [StateId] ASC)
,CONSTRAINT [FK_AccountState_Account] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account]([AccountId])
,CONSTRAINT [FK_AccountState_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State]([StateId])
,INDEX [IX_AccountState_Account] NONCLUSTERED ([AccountId])
,INDEX [IX_AccountState_State] NONCLUSTERED ([StateId])
);

INSERT INTO [dbo].[AccountState] ([AccountId], [StateId])
SELECT A.[AccountId], S.[StateId]
FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
WHERE A.[AccountId] = 1 AND S.[StateId] IN (1, 2, 3)
UNION
SELECT A.[AccountId], S.[StateId]
FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
WHERE A.[AccountId] = 2 AND S.[StateId] IN (3, 4, 5)
UNION
SELECT A.[AccountId], S.[StateId]
FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
WHERE A.[AccountId] = 3 AND S.[StateId] IN (1, 3, 5)
END;
GO


Sample query



SELECT
A.[AccountName]
,S.[StateName]
FROM
[dbo].[AccountState] A_S
JOIN [dbo].[Account] A
ON A_S.[AccountId] = A.[AccountId]
JOIN [dbo].[State] S
ON A_S.[StateId] = S.[StateId]
WHERE
S.[StateCode] = 'CA'


Of the two options, what type of index combination would be seen as most suitable to scale? Composite primary key only or composite primary key plus additional non-clustered indexes? Or is there another option that is more viable?









share



























    0















    I have a multi-tenant database where I am utilizing an AccountId column within all tables as a part of the composite Primary Key for tenant isolation. Is it beneficial to create additional non-clustered indexes on each column that is part of the composite Primary Key to assist SQL Server with maintaining accurate statistics and improving query performance when joining to lookup tables?



    For instance, within an associative table that defines one-to-many relationships between an Account and the American State in which they have offices, in theory which of the two options is preferable given the following structure and sample query?



    Create the Account and State tables and populate with sample data.



    DROP TABLE IF EXISTS [dbo].[Account];
    DROP TABLE IF EXISTS [dbo].[State];

    -- [Account] table and sample values.
    IF OBJECT_ID('[dbo].[Account]', 'U') IS NULL
    BEGIN
    CREATE TABLE [dbo].[Account] (
    [AccountId] [int] IDENTITY(1,1) NOT NULL
    ,[AccountAlias] [varchar](3) NOT NULL
    ,[AccountName] [varchar](128) NOT NULL
    ,CONSTRAINT [PK_Account] PRIMARY KEY CLUSTERED ([AccountId] ASC)
    ,CONSTRAINT [UQ_Account_Alias] UNIQUE NONCLUSTERED ([AccountAlias] ASC)
    ,CONSTRAINT [UQ_Account_Name] UNIQUE NONCLUSTERED ([AccountName] ASC)
    );

    SET IDENTITY_INSERT [dbo].[Account] ON;

    INSERT INTO [dbo].[Account] ([AccountId], [AccountAlias], [AccountName])
    VALUES (1, 'SA1', 'Sample Account 1'), (2, 'SA2', 'Sample Account 2'), (3, 'SA3', 'Sample Account 3')

    SET IDENTITY_INSERT [dbo].[Account] OFF;
    END;
    GO

    -- [State] table and sample values.
    IF OBJECT_ID('[dbo].[State]', 'U') IS NULL
    BEGIN
    CREATE TABLE [dbo].[State] (
    [StateId] [tinyint] IDENTITY(1,1) NOT NULL
    ,[StateCode] [varchar](2) NOT NULL
    ,[StateName] [varchar](32) NOT NULL
    ,CONSTRAINT [PK_State] PRIMARY KEY CLUSTERED ([StateId] ASC)
    ,CONSTRAINT [UQ_State_Code] UNIQUE NONCLUSTERED ([StateCode] ASC)
    ,CONSTRAINT [UQ_State_Name] UNIQUE NONCLUSTERED ([StateName] ASC)
    );

    SET IDENTITY_INSERT [dbo].[State] ON;

    INSERT INTO [dbo].[State] ([StateId], [StateCode], [StateName])
    VALUES (1, 'AL', 'Alabama'), (2, 'AK', 'Alaska'), (3, 'AZ', 'Arizona'), (4, 'AR', 'Arkansas'), (5, 'CA', 'California')

    SET IDENTITY_INSERT [dbo].[State] OFF;
    END;
    GO


    Create AccountState OPTION 1 - composite Primary Key only



    DROP TABLE IF EXISTS [dbo].[AccountState];

    IF OBJECT_ID('[dbo].[AccountState]', 'U') IS NULL
    BEGIN
    CREATE TABLE [dbo].[AccountState] (
    [AccountId] [int] NOT NULL
    ,[StateId] [tinyint] NOT NULL
    ,CONSTRAINT [PK_AccountState] PRIMARY KEY CLUSTERED ([AccountId] ASC, [StateId] ASC)
    ,CONSTRAINT [FK_AccountState_Account] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account]([AccountId])
    ,CONSTRAINT [FK_AccountState_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State]([StateId])
    );

    INSERT INTO [dbo].[AccountState] ([AccountId], [StateId])
    SELECT A.[AccountId], S.[StateId]
    FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
    WHERE A.[AccountId] = 1 AND S.[StateId] IN (1, 2, 3)
    UNION
    SELECT A.[AccountId], S.[StateId]
    FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
    WHERE A.[AccountId] = 2 AND S.[StateId] IN (3, 4, 5)
    UNION
    SELECT A.[AccountId], S.[StateId]
    FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
    WHERE A.[AccountId] = 3 AND S.[StateId] IN (1, 3, 5)
    END;
    GO


    Create AccountState OPTION 2 - composite Primary Key + non-clustered indexes



    DROP TABLE IF EXISTS [dbo].[AccountState];

    IF OBJECT_ID('[dbo].[AccountState]', 'U') IS NULL
    BEGIN
    CREATE TABLE [dbo].[AccountState] (
    [AccountId] [int] NOT NULL
    ,[StateId] [tinyint] NOT NULL
    ,CONSTRAINT [PK_AccountState] PRIMARY KEY CLUSTERED ([AccountId] ASC, [StateId] ASC)
    ,CONSTRAINT [FK_AccountState_Account] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account]([AccountId])
    ,CONSTRAINT [FK_AccountState_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State]([StateId])
    ,INDEX [IX_AccountState_Account] NONCLUSTERED ([AccountId])
    ,INDEX [IX_AccountState_State] NONCLUSTERED ([StateId])
    );

    INSERT INTO [dbo].[AccountState] ([AccountId], [StateId])
    SELECT A.[AccountId], S.[StateId]
    FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
    WHERE A.[AccountId] = 1 AND S.[StateId] IN (1, 2, 3)
    UNION
    SELECT A.[AccountId], S.[StateId]
    FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
    WHERE A.[AccountId] = 2 AND S.[StateId] IN (3, 4, 5)
    UNION
    SELECT A.[AccountId], S.[StateId]
    FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
    WHERE A.[AccountId] = 3 AND S.[StateId] IN (1, 3, 5)
    END;
    GO


    Sample query



    SELECT
    A.[AccountName]
    ,S.[StateName]
    FROM
    [dbo].[AccountState] A_S
    JOIN [dbo].[Account] A
    ON A_S.[AccountId] = A.[AccountId]
    JOIN [dbo].[State] S
    ON A_S.[StateId] = S.[StateId]
    WHERE
    S.[StateCode] = 'CA'


    Of the two options, what type of index combination would be seen as most suitable to scale? Composite primary key only or composite primary key plus additional non-clustered indexes? Or is there another option that is more viable?









    share

























      0












      0








      0








      I have a multi-tenant database where I am utilizing an AccountId column within all tables as a part of the composite Primary Key for tenant isolation. Is it beneficial to create additional non-clustered indexes on each column that is part of the composite Primary Key to assist SQL Server with maintaining accurate statistics and improving query performance when joining to lookup tables?



      For instance, within an associative table that defines one-to-many relationships between an Account and the American State in which they have offices, in theory which of the two options is preferable given the following structure and sample query?



      Create the Account and State tables and populate with sample data.



      DROP TABLE IF EXISTS [dbo].[Account];
      DROP TABLE IF EXISTS [dbo].[State];

      -- [Account] table and sample values.
      IF OBJECT_ID('[dbo].[Account]', 'U') IS NULL
      BEGIN
      CREATE TABLE [dbo].[Account] (
      [AccountId] [int] IDENTITY(1,1) NOT NULL
      ,[AccountAlias] [varchar](3) NOT NULL
      ,[AccountName] [varchar](128) NOT NULL
      ,CONSTRAINT [PK_Account] PRIMARY KEY CLUSTERED ([AccountId] ASC)
      ,CONSTRAINT [UQ_Account_Alias] UNIQUE NONCLUSTERED ([AccountAlias] ASC)
      ,CONSTRAINT [UQ_Account_Name] UNIQUE NONCLUSTERED ([AccountName] ASC)
      );

      SET IDENTITY_INSERT [dbo].[Account] ON;

      INSERT INTO [dbo].[Account] ([AccountId], [AccountAlias], [AccountName])
      VALUES (1, 'SA1', 'Sample Account 1'), (2, 'SA2', 'Sample Account 2'), (3, 'SA3', 'Sample Account 3')

      SET IDENTITY_INSERT [dbo].[Account] OFF;
      END;
      GO

      -- [State] table and sample values.
      IF OBJECT_ID('[dbo].[State]', 'U') IS NULL
      BEGIN
      CREATE TABLE [dbo].[State] (
      [StateId] [tinyint] IDENTITY(1,1) NOT NULL
      ,[StateCode] [varchar](2) NOT NULL
      ,[StateName] [varchar](32) NOT NULL
      ,CONSTRAINT [PK_State] PRIMARY KEY CLUSTERED ([StateId] ASC)
      ,CONSTRAINT [UQ_State_Code] UNIQUE NONCLUSTERED ([StateCode] ASC)
      ,CONSTRAINT [UQ_State_Name] UNIQUE NONCLUSTERED ([StateName] ASC)
      );

      SET IDENTITY_INSERT [dbo].[State] ON;

      INSERT INTO [dbo].[State] ([StateId], [StateCode], [StateName])
      VALUES (1, 'AL', 'Alabama'), (2, 'AK', 'Alaska'), (3, 'AZ', 'Arizona'), (4, 'AR', 'Arkansas'), (5, 'CA', 'California')

      SET IDENTITY_INSERT [dbo].[State] OFF;
      END;
      GO


      Create AccountState OPTION 1 - composite Primary Key only



      DROP TABLE IF EXISTS [dbo].[AccountState];

      IF OBJECT_ID('[dbo].[AccountState]', 'U') IS NULL
      BEGIN
      CREATE TABLE [dbo].[AccountState] (
      [AccountId] [int] NOT NULL
      ,[StateId] [tinyint] NOT NULL
      ,CONSTRAINT [PK_AccountState] PRIMARY KEY CLUSTERED ([AccountId] ASC, [StateId] ASC)
      ,CONSTRAINT [FK_AccountState_Account] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account]([AccountId])
      ,CONSTRAINT [FK_AccountState_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State]([StateId])
      );

      INSERT INTO [dbo].[AccountState] ([AccountId], [StateId])
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 1 AND S.[StateId] IN (1, 2, 3)
      UNION
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 2 AND S.[StateId] IN (3, 4, 5)
      UNION
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 3 AND S.[StateId] IN (1, 3, 5)
      END;
      GO


      Create AccountState OPTION 2 - composite Primary Key + non-clustered indexes



      DROP TABLE IF EXISTS [dbo].[AccountState];

      IF OBJECT_ID('[dbo].[AccountState]', 'U') IS NULL
      BEGIN
      CREATE TABLE [dbo].[AccountState] (
      [AccountId] [int] NOT NULL
      ,[StateId] [tinyint] NOT NULL
      ,CONSTRAINT [PK_AccountState] PRIMARY KEY CLUSTERED ([AccountId] ASC, [StateId] ASC)
      ,CONSTRAINT [FK_AccountState_Account] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account]([AccountId])
      ,CONSTRAINT [FK_AccountState_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State]([StateId])
      ,INDEX [IX_AccountState_Account] NONCLUSTERED ([AccountId])
      ,INDEX [IX_AccountState_State] NONCLUSTERED ([StateId])
      );

      INSERT INTO [dbo].[AccountState] ([AccountId], [StateId])
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 1 AND S.[StateId] IN (1, 2, 3)
      UNION
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 2 AND S.[StateId] IN (3, 4, 5)
      UNION
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 3 AND S.[StateId] IN (1, 3, 5)
      END;
      GO


      Sample query



      SELECT
      A.[AccountName]
      ,S.[StateName]
      FROM
      [dbo].[AccountState] A_S
      JOIN [dbo].[Account] A
      ON A_S.[AccountId] = A.[AccountId]
      JOIN [dbo].[State] S
      ON A_S.[StateId] = S.[StateId]
      WHERE
      S.[StateCode] = 'CA'


      Of the two options, what type of index combination would be seen as most suitable to scale? Composite primary key only or composite primary key plus additional non-clustered indexes? Or is there another option that is more viable?









      share














      I have a multi-tenant database where I am utilizing an AccountId column within all tables as a part of the composite Primary Key for tenant isolation. Is it beneficial to create additional non-clustered indexes on each column that is part of the composite Primary Key to assist SQL Server with maintaining accurate statistics and improving query performance when joining to lookup tables?



      For instance, within an associative table that defines one-to-many relationships between an Account and the American State in which they have offices, in theory which of the two options is preferable given the following structure and sample query?



      Create the Account and State tables and populate with sample data.



      DROP TABLE IF EXISTS [dbo].[Account];
      DROP TABLE IF EXISTS [dbo].[State];

      -- [Account] table and sample values.
      IF OBJECT_ID('[dbo].[Account]', 'U') IS NULL
      BEGIN
      CREATE TABLE [dbo].[Account] (
      [AccountId] [int] IDENTITY(1,1) NOT NULL
      ,[AccountAlias] [varchar](3) NOT NULL
      ,[AccountName] [varchar](128) NOT NULL
      ,CONSTRAINT [PK_Account] PRIMARY KEY CLUSTERED ([AccountId] ASC)
      ,CONSTRAINT [UQ_Account_Alias] UNIQUE NONCLUSTERED ([AccountAlias] ASC)
      ,CONSTRAINT [UQ_Account_Name] UNIQUE NONCLUSTERED ([AccountName] ASC)
      );

      SET IDENTITY_INSERT [dbo].[Account] ON;

      INSERT INTO [dbo].[Account] ([AccountId], [AccountAlias], [AccountName])
      VALUES (1, 'SA1', 'Sample Account 1'), (2, 'SA2', 'Sample Account 2'), (3, 'SA3', 'Sample Account 3')

      SET IDENTITY_INSERT [dbo].[Account] OFF;
      END;
      GO

      -- [State] table and sample values.
      IF OBJECT_ID('[dbo].[State]', 'U') IS NULL
      BEGIN
      CREATE TABLE [dbo].[State] (
      [StateId] [tinyint] IDENTITY(1,1) NOT NULL
      ,[StateCode] [varchar](2) NOT NULL
      ,[StateName] [varchar](32) NOT NULL
      ,CONSTRAINT [PK_State] PRIMARY KEY CLUSTERED ([StateId] ASC)
      ,CONSTRAINT [UQ_State_Code] UNIQUE NONCLUSTERED ([StateCode] ASC)
      ,CONSTRAINT [UQ_State_Name] UNIQUE NONCLUSTERED ([StateName] ASC)
      );

      SET IDENTITY_INSERT [dbo].[State] ON;

      INSERT INTO [dbo].[State] ([StateId], [StateCode], [StateName])
      VALUES (1, 'AL', 'Alabama'), (2, 'AK', 'Alaska'), (3, 'AZ', 'Arizona'), (4, 'AR', 'Arkansas'), (5, 'CA', 'California')

      SET IDENTITY_INSERT [dbo].[State] OFF;
      END;
      GO


      Create AccountState OPTION 1 - composite Primary Key only



      DROP TABLE IF EXISTS [dbo].[AccountState];

      IF OBJECT_ID('[dbo].[AccountState]', 'U') IS NULL
      BEGIN
      CREATE TABLE [dbo].[AccountState] (
      [AccountId] [int] NOT NULL
      ,[StateId] [tinyint] NOT NULL
      ,CONSTRAINT [PK_AccountState] PRIMARY KEY CLUSTERED ([AccountId] ASC, [StateId] ASC)
      ,CONSTRAINT [FK_AccountState_Account] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account]([AccountId])
      ,CONSTRAINT [FK_AccountState_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State]([StateId])
      );

      INSERT INTO [dbo].[AccountState] ([AccountId], [StateId])
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 1 AND S.[StateId] IN (1, 2, 3)
      UNION
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 2 AND S.[StateId] IN (3, 4, 5)
      UNION
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 3 AND S.[StateId] IN (1, 3, 5)
      END;
      GO


      Create AccountState OPTION 2 - composite Primary Key + non-clustered indexes



      DROP TABLE IF EXISTS [dbo].[AccountState];

      IF OBJECT_ID('[dbo].[AccountState]', 'U') IS NULL
      BEGIN
      CREATE TABLE [dbo].[AccountState] (
      [AccountId] [int] NOT NULL
      ,[StateId] [tinyint] NOT NULL
      ,CONSTRAINT [PK_AccountState] PRIMARY KEY CLUSTERED ([AccountId] ASC, [StateId] ASC)
      ,CONSTRAINT [FK_AccountState_Account] FOREIGN KEY ([AccountId]) REFERENCES [dbo].[Account]([AccountId])
      ,CONSTRAINT [FK_AccountState_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State]([StateId])
      ,INDEX [IX_AccountState_Account] NONCLUSTERED ([AccountId])
      ,INDEX [IX_AccountState_State] NONCLUSTERED ([StateId])
      );

      INSERT INTO [dbo].[AccountState] ([AccountId], [StateId])
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 1 AND S.[StateId] IN (1, 2, 3)
      UNION
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 2 AND S.[StateId] IN (3, 4, 5)
      UNION
      SELECT A.[AccountId], S.[StateId]
      FROM [dbo].[Account] A CROSS JOIN [dbo].[State] S
      WHERE A.[AccountId] = 3 AND S.[StateId] IN (1, 3, 5)
      END;
      GO


      Sample query



      SELECT
      A.[AccountName]
      ,S.[StateName]
      FROM
      [dbo].[AccountState] A_S
      JOIN [dbo].[Account] A
      ON A_S.[AccountId] = A.[AccountId]
      JOIN [dbo].[State] S
      ON A_S.[StateId] = S.[StateId]
      WHERE
      S.[StateCode] = 'CA'


      Of the two options, what type of index combination would be seen as most suitable to scale? Composite primary key only or composite primary key plus additional non-clustered indexes? Or is there another option that is more viable?







      sql-server database-design index query-performance





      share












      share










      share



      share










      asked 3 mins ago









      PicoDeGalloPicoDeGallo

      7921819




      7921819






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f231407%2fnon-clustered-indexes-for-columns-defined-within-a-composite-primary-key%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f231407%2fnon-clustered-indexes-for-columns-defined-within-a-composite-primary-key%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Armoriale delle famiglie italiane (Car) Indice Armi | Bibliografia | Menu di navigazioneBlasone...

          Why does this relation fail symmetry and transitivity properties?Properties of Relations. Reflexive,...

          why typing a variable (or expression) prints the value to stdout?Calling a function of a module by using its...