Can foreign keys hurt query performance

前端 未结 11 1800
温柔的废话
温柔的废话 2021-02-02 10:55

As I understood from this post, there are some scenarios where foreign keys can improve query performance.

I\'ve heard the opposite claim though, that because of refere

相关标签:
11条回答
  • 2021-02-02 11:14

    I believe the noted post pointed out that putting an index on FK fields improved performance, not simply that a FK relationship improved performance. The existence of a FK on a table should not have any effect on a SELECT query, unless JOIN operations are being done, at which point, the FK relationship AND index on FK fields would improve performance.

    0 讨论(0)
  • 2021-02-02 11:15

    Foreign keys slow down insertions and alterations, because each foreign key reference must be verified. Foreign keys can either not affect a selection, or make it go faster, depending on if the DBMS uses foreign key indexing.

    Foreign keys have a complex effect on deletion. If you're deleting the thing that refers to the foreign key, it won't affect anything, but if what you're deleting is referenced by a foreign key in another row/table, then it will generally cause problems.

    Foreign keys can cause a minor performance degradation in table creations and alterations.

    Of course, this all assumes foreign key verification is in use.

    0 讨论(0)
  • 2021-02-02 11:16

    In theory, yes: data writes need to validate the constraints.

    In practice, seldom: unless measured and proved otherwise, you can assume there is no performance impact. Overwhelmingly, performance problems occur due to other problems:

    • bad schema design (missing indexes, bad clustered index choice)
    • contention (blocking), again due to bad schema design (table scans guarantee lock conflicts)
    • bad query design

    On a well designed schema and good queries the cost of constraints will start to show up at very high throughput. When this happens, there are preventive measures.

    My 2c: Never sacrifice correctness constraints for some elusive performance goals. In the very rare case when the constraints are indeed the problem there are measurements to show that's the case, and as the saying goes: if you have to ask how much it costs, you can't afford it. If you have to ask if constraints can be a problem, you can't remove them (no offence intended).

    0 讨论(0)
  • 2021-02-02 11:18

    If foreign keys had any impact in that way, it would be on INSERTS. The database does the referential checking on foreign keys when records are created/modified, not SELECTed.

    0 讨论(0)
  • 2021-02-02 11:27

    Foreign keys will not adversley affect query performance in most cases, and are strongly recommended. By aiding normalization, you will eliminate redundant data, and if you follow up by adding underlying indexes (for the appropriate foreign key) you will get good performance on your queries.

    Foreign-keys can help the query optimizer get the best query plans for a given query.

    Foreign-key checking is a factor when you update your data (which is a separate consideration - I assume here your concern is query - unless by the word query you imply both).

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