What is the most effective declaration for the PRIMARY KEY CLUSTERED in SQL table

Andreas ss 726 Reputation points
2020-10-18T22:27:24.803+00:00

Hello,

I wonder something about the PRIMARY KEY CLUSTERED for a ceratain scenario.
I am not sure what would be the Fastest way to return the information I want.

I will describe what I want to return from a table (See Attached image of table)

  1. The table exist of 3000 rows. The table only exists of Dates within 1 month. So it is 30 or 31 days. DateTimes like: 2010-01-03 17:45:00
  2. _DayNr column tells which day it is in the date. 3 for 2010-01-03 17:45:00
  3. It exists 1570 _featureNbr for EACH _DateTime. So for example:
    2010-01-03 17:45:00 have 1570 _FeatureNbr and so on.

Now is my task this. I want to return ALL _FeatureNbr for each of the 3000
DateTimes like: 2010-01-03 17:45:00.

So I wonder what would be the most effective declaration for the PRIMARY KEY CLUSTERED?

Shouldn't I just use _DateTime as the first one and _FeatureNbr as the second one?
CONSTRAINT [PK_table20101] PRIMARY KEY CLUSTERED ([_DateTime] ASC, [_FeatureNbr]),

Or is it faster to use _DayNr first like this?
CONSTRAINT [PK_table20101] PRIMARY KEY CLUSTERED ([_DayNr] ASC, [_DateTime] ASC, [_FeatureNbr]),

Or should I do this in a different way. I am not 100% sure in what way I should think when declaring
those most effectively?

Thank you!

33070-image1.png

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,670 questions
{count} votes

Accepted answer
  1. Ben Miller (DBAduck) 956 Reputation points
    2020-10-19T14:44:44.22+00:00

    I would recommend that you put in place and test the clustered index (_DayNr, _DateTime) and preferably it be Unique (could be made the primary key).

    Not necessary to put the _FeatureNbr in the index as 3 keys deep will probably not be helpful in this case.

    Ensure that you TEST this to ensure that you get what you expect for speed and results. If not then you can change things pretty simply as you have pretty small tables (relatively).


2 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,731 Reputation points
    2020-10-19T12:28:22.267+00:00

    Do not confuse the primary key with the clustered index, they can be different. SQL Server as a default makes the PK clustered, however there is no requirement for it to be that way.

    The PK should be the way you uniquely identify the records in your table.
    The clustered index should be the most common way you search for records in the table.

    1 person found this answer helpful.
    0 comments No comments

  2. Ben Miller (DBAduck) 956 Reputation points
    2020-10-19T02:49:14.543+00:00

    Then you can make the Clustered Primary Key on the _DateTime column so when you query for the datetime value and want to get all the _FeatureNbr, it will be able to since you are seeking on the Clustered Key.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.