如何设置 MySQL 表以随时间推移跟踪变量?

发布于 2024-10-16 22:47:05 字数 1411 浏览 1 评论 0原文

假设我的网站有几个注册用户。

用户保存在单个表“users”中,该表为每个用户分配一个唯一的 ID。

我想让我的用户跟踪他们的费用、行驶里程、温度等。

我无法确定每个用户在登录时总是会输入所有可跟踪变量的值 - 例如可能发生的情况是:

'example data'
user     date         amount    miles     temp    etc
1        3/1/2010     $10.00    5         54
2        3/1/2010     $20.00    15          
1        3/12/2010              5         55
1        3/15/2010    $10.00    25        51
3        3/20/2010                        45
3        4/12/2010    $20.00    10        54

针对这种情况设置桌子的最佳方法是什么?

我应该在每个用户注册时创建一个专属于他们的表吗? (最终可能会出现数千个用户专用表)

'user-1 table'
date         amount    miles     temp    etc
3/1/2010     $10.00    5         54
3/12/2010              5         55
3/15/2010    $10.00    25        51

'user-3 table'
date         amount    miles     temp    etc
3/20/2010                        45
4/12/2010    $20.00    10        54

and so on...

我是否应该创建一个与上面的示例数据基本相同的单个表? (最终可能会得到一个巨大的表,需要对其进行梳理以查找具有请求的用户 ID 的行)。

'user data table'
user     date         amount    miles     temp    etc
1        3/1/2010     $10.00    5         54
2        3/1/2010     $20.00    15          
1        3/12/2010              5         55
1        3/15/2010    $10.00    25        51
3        3/20/2010                        45
3        4/12/2010    $20.00    10        54

有什么建议吗?

Say I have several registered users in my website.

Users are saved on a single table 'users' that assigns a unique id for each one of them.

I want to allow my users to track their expenses, miles driven, temperature, etc.

I can't be sure each user will always enter a value for all trackable variables when they login -- so an example of what could happen would be:

'example data'
user     date         amount    miles     temp    etc
1        3/1/2010     $10.00    5         54
2        3/1/2010     $20.00    15          
1        3/12/2010              5         55
1        3/15/2010    $10.00    25        51
3        3/20/2010                        45
3        4/12/2010    $20.00    10        54

What is the best way to set up my tables for this situation?

Should I create a table exclusive to each user when they register? (could end up with thousands of user-exclusive tables)

'user-1 table'
date         amount    miles     temp    etc
3/1/2010     $10.00    5         54
3/12/2010              5         55
3/15/2010    $10.00    25        51

'user-3 table'
date         amount    miles     temp    etc
3/20/2010                        45
4/12/2010    $20.00    10        54

and so on...

Should I create a single table that is essentially the same as the example data above? (could end up with a gigantic table that needs to be combed to find rows with requested user id's).

'user data table'
user     date         amount    miles     temp    etc
1        3/1/2010     $10.00    5         54
2        3/1/2010     $20.00    15          
1        3/12/2010              5         55
1        3/15/2010    $10.00    25        51
3        3/20/2010                        45
3        4/12/2010    $20.00    10        54

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅渡 2024-10-23 22:47:05

数据库的构建是为了将相似的数据作为一个集合来处理。

您想要的是单个用户数据表,同一个表中的多个用户按 user_id 分割。不过,您可能希望进一步规范化,以便它存储:

user     date         type      units
1        3/1/2010     dollars   10.00
1        3/1/2010     miles     5
1        3/1/2010     temp      54
2        3/1/2010     dollars   20.00
2        3/1/2010     miles     15          
1        3/12/2010    miles     5
1        3/12/2010    temp      55
etc

或者更进一步,如果用户+日期进行特定旅行

trip-table
tripid    user     date
========= ======== =========
1         1        3/1/2010

type-table
typeid    description
========= ============
1         dollars
2         miles
etc

trip-data
tripid     type      units
=========  ========  =======
1          1         10.00
1          2         5
etc

但是,如果您总是(或几乎总是)在表单中显示数据输入时,数据基于所有输入列(如电子表格),那么为了简洁性、可编程性和性能,您最好坚持使用非标准化形式。

最终可能会得到一个巨大的表,需要对其进行梳理以查找具有请求的用户 ID 的行

假设您正确且明智地使用索引,现代 RDBMS 旨在处理大量数据。索引允许查询仅查找所需的数据,因此将所有数据保存在一张表中通常不会造成什么损失。

Databases are built to handle similar data as a set together.

What you want is a single user-data-table, with multiple users in the same table split by user_id. You might want to further normalize that though, so that it stores:

user     date         type      units
1        3/1/2010     dollars   10.00
1        3/1/2010     miles     5
1        3/1/2010     temp      54
2        3/1/2010     dollars   20.00
2        3/1/2010     miles     15          
1        3/12/2010    miles     5
1        3/12/2010    temp      55
etc

Or even further if the user+date makes a specific trip

trip-table
tripid    user     date
========= ======== =========
1         1        3/1/2010

type-table
typeid    description
========= ============
1         dollars
2         miles
etc

trip-data
tripid     type      units
=========  ========  =======
1          1         10.00
1          2         5
etc

However, if you will always (or almost always) show your data in the form as entered, with the data pivoted on all the input columns (like a spreadsheet), then you would be better off sticking to the un-normalised form for brevity, programmability and performance.

could end up with a gigantic table that needs to be combed to find rows with requested user id's

Assuming you employ indexes properly and judiciously, modern RDBMS are built to handle gigantic amounts of data. The indexes allow the queries to seek only the data it needs, so there is normally little penalty in keeping it all in one table.

童话里做英雄 2024-10-23 22:47:05

不,只需创建一张包含所有可能的可空字段的表。如果用户没有填写该参数 - 那么只需保留 NULL 值即可。

最终可能会得到一个巨大的表,需要对其进行梳理以查找具有请求的用户 ID 的行

,并且如果您为 user_id 字段指定索引(对于诸如 <代码>WHERE user_id = 42)

No, just create one table with all possible nullable fields. If user hasn't filled that parameter - then just keep NULL value there.

could end up with a gigantic table that needs to be combed to find rows with requested user id's

Yes, and the query will be fast enough if you'll specify an index for user_id field (for queries like WHERE user_id = 42)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文