什么是在MySQL数据库中保存数据的最有效方法

[英]What's the most efficient way to save data in MySQL Database


Hi I have data that I need to store in my database. My website is about tv-shows, and the data I'm talking about is basically seasons and episodes. My concern is about whether I should use two tables or one. I'll make myself more clear:

嗨,我有需要存储在我的数据库中的数据。我的网站是关于电视节目,我所谈论的数据基本上是季节和剧集。我担心的是我应该使用两张桌子还是一张桌子。我会让自己更清楚:

Option 1:

选项1:

seasons_table

seasons_table

  • post_id
  • POST_ID
  • season_number
  • season_number
  • season_title
  • SEASON_TITLE
  • language
  • 语言
  • subtitles
  • 字幕
  • item_date (when it was created)
  • item_date(创建时)
  • item_modified (when it was last modified)
  • item_modified(上次修改时)

episodes_table

episodes_table

  • post_id
  • POST_ID
  • season_id
  • season_id
  • episode_number
  • EPISODE_NUMBER
  • episode_title
  • EPISODE_TITLE
  • item_date
  • item_date
  • item_modified
  • item_modified

Option 2:

选项2:

unique table

独特的桌子

  • post_id
  • POST_ID
  • item_type (season or episode)
  • item_type(季节或剧集)
  • season_number
  • season_number
  • season_title
  • SEASON_TITLE
  • language
  • 语言
  • subtitles
  • 字幕
  • season_id
  • season_id
  • episode_number
  • EPISODE_NUMBER
  • episode_title
  • EPISODE_TITLE
  • item_date
  • item_date
  • item_modified
  • item_modified

I can already see for myself that with Option 1 there's gonna be a lot of common fields between the two tables, while with Option 2 there's gonna be a lot of fields that are never gonna be used (e.g. an episode will never have a value in the field season_title since it just needs a value for season_id to be linked to that season).

我已经可以看到,对于自己,选项1会在两个表之间存在很多共同的领域,而对于选项2,会有很多领域永远不会被使用(例如,一集将永远不会有价值字段season_title因为它只需要将season_id的值与该季节相关联)。

So which one is the best option? I'm willing to choose option 2, but I'm worried that those empty fields are gonna waste memory or loading time or whatever while processing any data in that table. Is that true? Thanks in advance to everyone, I hope I made myself clear.

那么哪一个是最佳选择?我愿意选择选项2,但是我担心那些空字段在处理该表中的任何数据时会浪费内存或加载时间等等。真的吗?在此先感谢大家,我希望自己能够清楚明白。

By the way my website is wordpress based and I'm gonna use a custom table, but I think i'm gonna use some wordpress functions to process data like $wpdb->insert and so on...

顺便说一句,我的网站是基于wordpress的,我将使用自定义表,但我想我会使用一些wordpress函数来处理数据,如$ wpdb-> insert等等...

1 个解决方案

#1


0  

Two tables is the best approach here, that more closely adheres to the rules of database normalization. If there's concern about duplication you need to better evaluate where you're storing data.

这里有两个表是最好的方法,它更紧密地遵循数据库规范化的规则。如果担心重复,您需要更好地评估存储数据的位置。

item_date and item_modified are probably unique for each entry even if they are "duplicated" in terms of schema. Don't worry about this.

item_date和item_modified对于每个条目可能都是唯一的,即使它们在模式方面是“重复的”。别担心这个。

Whatever post_id is, you'll have to evaluate if you have a direct relationship between post and these two tables, or from post to seasons to episodes.

无论post_id是什么,您都必须评估帖子和这两个表之间是否有直接关系,或者从帖子到季节到剧集。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/05/06/76f5a290ac0e4f6db73689f6c43499ad.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告