Pros and Cons of autoincrement keys on “every table”

前端 未结 9 740
误落风尘
误落风尘 2020-12-09 12:54

We are having a rather long discussion in our company about whether or not to put an autoincrement key on EVERY table in our database.

I can understand putting one o

相关标签:
9条回答
  • 2020-12-09 13:08

    I'm not a fan of auto-increment primary keys on every table. The ideas that these give you fast joins and fast row inserts are really not true. My company calls this meatloaf thinking after the story about the woman who always cut the ends off her meatloaf just because her mother always did it. Her mother only did it because the pan was too short--the tradition keeps going even though the reason no longer exists.

    • When the driving table in a join has an auto-increment key, the joined table frequently shouldn't because it must have the FK to the driving table. It's the same column type, but not auto-increment. You can use the FK as the PK or part of a composite PK.

    • Adding an auto-increment key to a table with a naturally unique key will not always speed things up--how can it? You are adding more work by maintaining an extra index. If you never use the auto-increment key, this is completely wasted effort.

    • It's very difficult to predict optimizer performance--and impossible to predict future performance. On some databases, compressed or clustered indexes will decrease the costs of naturally unique PKs. On some parallel databases, auto-increment keys are negotiated between nodes and that increases the cost of auto-increment. You can only find out by profiling, and it really sucks to have to change Company Policy just to change how you create a table.

    0 讨论(0)
  • 2020-12-09 13:08

    Having autoincrementing primary keys may make it easier for you to switch ORM layers in the future, and doesn't cost much (assuming you retain your logical unique keys).

    0 讨论(0)
  • 2020-12-09 13:14

    If you use small keys like this for Clustered Indexes, then there's quite significant advantages.

    Like:

    Inserts will always go at the end of pages.

    Non-Clustered Indexes (which need a reference to the CIX key(s)) won't have long row addresses to consider.

    And more... Kimberly Tripp's stuff is the best resource for this. Google her...

    Also - if you have nothing else ensuring uniqueness, you have a hook into each row that you wouldn't otherwise have. You should still put unique indexes on fields that should be unique, and use FKs onto appropriate fields.

    But... please consider the overhead of creating such things on existing tables. It could be quite scary. You can put unique indexes on tables without needing to create extra fields. Those unique indexes can then be used for FKs.

    0 讨论(0)
  • 2020-12-09 13:14

    You add surrogate auto increment primary keys as part of the implementation after logical design to respect the physical, on-disk architecture of the db engine.

    That is, they have physcial properties (narrow, numeric, strictly monotonically increasing) that suit use as clustered keys, in joins etc.

    Example: If you're modelling your data, then "product SKU" is your key. "product ID" is added afterwards, (with a unique constraint on "product SKU") when writing your "CREATE TABLE" statements because you know SQL Server.

    This is the main reason.

    The other reason a brain dead ORM that can't work without one...

    0 讨论(0)
  • 2020-12-09 13:20

    The simplest approach is to always use surrogate keys that are either auto-incremented by the db or via an orm. And on every table. This is because they are the generally fasted method for joins and also they make learning the database extremely simple, i.e. none of this whats my key for a table nonsense as they all use the same kind of key. Yes they can be slower but in truth the most important part of design is something that wont break over time. This is proven for surrogate keys. Remember, maintenance of the system happens a lot longer than development. Plan for a system that can be maintained. Also, with current hardware the potential performance loss is really negligable.

    0 讨论(0)
  • 2020-12-09 13:25

    I'm assuming that almost all tables will have a primary key - and it's just a question of whether that key consists of one or more natural keys or a single auto-incrementing surrogate key. If you aren't using primary keys then you will generally get a lot of advantages of using them on almost all tables.

    So, here are some pros & cons of surrogate keys. First off, the pros:

    • Most importantly: they allow the natural keys to change. Trivial example, a table of persons should have a primary key of person_id rather than last_name, first_name.
    • Read performance - very small indexes are faster to scan. However, this is only helpful if you're actually constraining your query by the surrogate key. So, good for lookup tables, not so good for primary tables.
    • Simplicity - if named appropriately, it makes the database easy to learn & use.
    • Capacity - if you're designing something like a data warehouse fact table - surrogate keys on your dimensions allow you to keep a very narrow fact table - which results in huge capacity improvements.

    And cons:

    • They don't prevent duplicates of the natural values. So, you'll still usually want a unique constraint (index) on the logical key.
    • Write performance. With an extra index you're going to slow down inserts, updates and deletes that much more.
    • Simplicity - for small tables of data that almost never changes they are unnecessary. For example, if you need a list of countries you can use the ISO list of countries. It includes meaningful abbreviations. This is better than a surrogate key because it's both small and useful.

    In general, surrogate keys are useful, just keep in mind the cons and don't hesitate to use natural keys when appropriate.

    0 讨论(0)
提交回复
热议问题