English 中文(简体)
一种外国关键关系是否正常化?
原标题:Is it ever OK to denormalize a foreign key relation?

我有:

companies
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

users
+----------------------+--------------+------+-----+---------+----------------+
| Field                | Type         | Null | Key | Default | Extra          |
+----------------------+--------------+------+-----+---------+----------------+
| id                   | int(11)      | NO   | PRI | NULL    | auto_increment |
| email                | varchar(255) | NO   |     |         |                |
| username             | varchar(255) | NO   | UNI | NULL    |                |
| company_id           | int(11)      | YES  |     | NULL    |                |
| first_name           | varchar(255) | YES  |     | NULL    |                |
| last_name            | varchar(255) | YES  |     | NULL    |                |
| title                | varchar(255) | YES  |     | NULL    |                |
+----------------------+--------------+------+-----+---------+----------------+

notes
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id         | int(11)      | YES  |     | NULL    |                |
| title           | varchar(255) | YES  |     | NULL    |                |
| body            | text         | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

一家公司有许多用户,用户有许多说明。

My question is, is it bad to denormalize a little by adding a company_id foreign key to the notes table? The reason is that then I can get all the notes for a company with one fewer join than in the current schema.

Or to put the question another way: What are the pros and cons of adding a redundant company_id foreign key to notes?

最佳回答

Don t do it. MySQL is very smart about joins. What seems like a lot of work in theory often ends up being done very efficiently. Use explain if you aren t sure about how much work a query will generate.

避免在打字时加入,是失去数据基的极差的原因。

问题回答

脱热的缺点(涉及公司——见附注表):

  • The data can get out of sync
  • It uses a little bit more disk space

Advantages:

  • Joins are simpler. Sometimes this is a good reason, for example: you could normalize an address table by moving the city name to a cities table. But I wouldn t do it, because it will complicate a lot of queries, plus it will complicate inserts and updates, and there is little benefit. There is a way to reduce this added complexity problem somewhat: using a view.
  • For some cases it might be faster (those cases are rare).
  • For very large databases, it might be easier to shard the data.




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签