0

我有两张桌子:

++++++++++++++++++++++++++++++++++++
|              Games               |
++++++++++++++++++++++++++++++++++++
| ID |  Name  |    Description     |
++++++++++++++++++++++++++++++++++++
| 1  | Game 1 | A game description |
| 2  | Game 2 | And another        |
| 3  | Game 3 | And another        |
| .. |  ...   |       ...          |
++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++++++
|             GameReviews             |
+++++++++++++++++++++++++++++++++++++++
| ID |GameID|          Review         |
+++++++++++++++++++++++++++++++++++++++
| 1  |  1   |Review for game 1        |
| 2  |  1   |Another review for game 1|
| 3  |  1   |And another              |
| .. | ...  |         ...             |
+++++++++++++++++++++++++++++++++++++++

选项1:

SELECT 
    Games.ID, 
    Games.Name,
    Games.Description, 
    GameReviews.ID, 
    GameReviews.Review 
FROM 
    GameReviews
LEFT JOIN
    Games
ON
    Games.ID = GameReviews.GameID
WHERE
    Games.ID=?

选项 2:

SELECT
    ID,
    Name,
    Description
FROM
    Games
WHERE
    ID=?

然后 SELECT ID, Review FROM GameReviews WHERE GameID=?

显然,查询 1 会“更简单”,编写的代码更少,而另一个在逻辑上似乎在数据库上“更容易”,因为它只查询Games表一次。问题是当它真正归结为它时,性能和效率真的有区别吗?

4

2 回答 2

1

选择选项 1,这正是 RDBMS 的优化目标。
并且从客户端访问一次数据库总是比多次重复访问要好。

我不相信你会有这么多游戏和评论,选择选项 2 是有意义的。

于 2013-03-09T20:33:10.320 回答
1

The vast majority of the time option 1 would be the way to go. The performance difference between the two would not be measurable until you have a lot of data. Keep it simple.

Your example is also fairly basic. At scale, performance issues can start revealing themselves based on what fields are being filtered, joined and pulled. The ideal scenario is to only pull data that exists in indexes (particularly with InnoDB). That usually is not possible, but a strategy is to pull the actual data you need at the last possible moment. Which is sort of what option 2 would be doing.

At extreme scale, you don't want to do any joins in the database at all. Your "joins" would happen in code, minimizing data sent over the network. Go with option 1 until you start having performance issues, which may never happen.

于 2013-03-09T20:45:19.900 回答