Guardian
Guardian

Reputation: 109

String as Primary Key

I know this has been discussed a lot, and my final choice is: I will use string as primary key, mainly for two reasons:

  1. I won't have more than 100k rows
  2. It will simplify a lot

Is there anything I can do to improve performance, like creating a index?

I'm using Entity Framework code-first.

Upvotes: 0

Views: 550

Answers (2)

TelJanini
TelJanini

Reputation: 835

Creating an index is unnecessary, as the PRIMARY KEY provides a UNIQUE, CLUSTERED index.

Also note, most RDBMS have can more efficiently index fixed-length fields, such as CHAR, vs. variable length fields like VARCHAR.

One other possibility to consider: An integer (INT) primary key, with a unique index on the string value that you were originally going to use as the PRIMARY KEY. This provides the ease of JOINing on an integer field (not to mention space savings), while enforcing your original requirement.

Upvotes: 4

Rafa Paez
Rafa Paez

Reputation: 4860

Reduce the length of the VARCHAR as much as possible.

Indexes are automatically created for PRIMARY KEYS, so you don't have to create any index for this column.

Upvotes: 1

Related Questions