每日摘要的 SQL 架构

发布于 2024-12-12 21:08:23 字数 470 浏览 2 评论 0原文

我有一个数据库,它以以下格式存储来自不同位置的 ping:

|    Date    | Source | Language |   OS   | Version | more...
| 2011-10-30 | App1   | en       | XP     | 1.0     | ...
| 2011-10-30 | App2   | de       | 10.7.1 | 1.3     | ...

它非常适合提取快照信息。我希望能够存储从上表生成的每日摘要,以便我可以获得信息如何随时间变化的图表。

示例:

  • 显示日期与版本使用情况的图表
  • 显示日期与操作系统版本的图表

问题是上表中的几个列(语言、操作系统、版本)可能具有可变数量的值。我没有受过太多数据库设计方面的教育,无法理解如何存储这些信息以便于检索。

有人可以提出任何建议吗?

I've got a database which stores pings from various places in the following format:

|    Date    | Source | Language |   OS   | Version | more...
| 2011-10-30 | App1   | en       | XP     | 1.0     | ...
| 2011-10-30 | App2   | de       | 10.7.1 | 1.3     | ...

It works just fine for extracting snapshot information. I would like to be able to store daily summaries generated from the above table so that I can get graphs of how the information changes over time.

Examples:

  • Graph showing date against version usage
  • Graph showing date against OS version

The problem is that several of the columns in the table above (language, os, version) can have a variable number of values. I've not had much of an education in database design and can't get my head around how to store this information for easy retrieval.

Can anyone make any suggestions?

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

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

发布评论

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

评论(2

月下伊人醉 2024-12-19 21:08:36

这里使用数据库规范化。确保您的数据库采用3-NF

例如,在您的情况下,

创建一个表 OS_TYPE 说它有值

Id Value
1  Win XP
2  Win 7
3  Ubuntu Linux

等。

当您需要插入事务表而不是插入操作系统名称时,查找表并获取 Id,然后将其与其他数据一起插入。在 OS_TYPE 表和 DATA 表之间保留外键引用。因此,对于 Win XP version-1 或 Win XP Service pack 2,您将在 DATA 表中具有相同的条目 ID 1

这样您将获得可分析和处理的一致数据。获得正确的数据后,请在 SQL 中使用GROUP BY、ORDER BY 和 HAVING 子句来处理数据。

Here comes use of database normalization. Make sure your DB is in 3-NF.

For example In your case,

Create a table OS_TYPE say it has values

Id Value
1  Win XP
2  Win 7
3  Ubuntu Linux

etc..

When you need to insert into your transaction table instead of inserting the OS Name,look up table and get the Id, then insert that with other data. Keep a foriegn key reference between OS_TYPE table and DATA table. So for Win XP version-1 or Win XP Service pack 2 you will have the same entry the ID 1 in the DATA table.

This way you will have consistent data that can be analysed and processed. Once you have the proper data, use GROUP BY, ORDER BY and HAVING clauses in SQL to process the data.

夜灵血窟げ 2024-12-19 21:08:33

您可以很容易地从当前表结构中获取这些统计信息。 SQL 关键字 GROUP BY 应该是您的朋友。

要获取您键入的每个日期的版本使用情况

SELECT Date, Version, count(*) as NumberOfEntries
FROM YourTable
GROUP BY Date, Version
ORDER BY Date, Version

ORDER BY Date, Version 只是为了获得良好的排序输出)

如果您想获得每个源和版本组合的统计信息,请将问题修改为

SELECT Date, Source, Version, count(*) as NumberOfEntries
FROM YourTable
GROUP BY Date, Source, Version
ORDER BY Date, Source, Version

If您希望获得您键入的每个日期的操作系统版本

SELECT Date, OS, count(*) as NumberOfEntries
FROM YourTable
GROUP BY Date, OS
ORDER BY Date, OS

如果您没有大量数据(例如几百万行),那么使用合适的索引不会出现任何性能问题。

You can quite easily get those statistics from the current table structure. The SQL keyword GROUP BY should be your friend.

To get version usage per date you type

SELECT Date, Version, count(*) as NumberOfEntries
FROM YourTable
GROUP BY Date, Version
ORDER BY Date, Version

(The ORDER BY Date, Version is just to get a nice sorted output)

If you want to have statistics per each combination of Source and Version you modify the question to

SELECT Date, Source, Version, count(*) as NumberOfEntries
FROM YourTable
GROUP BY Date, Source, Version
ORDER BY Date, Source, Version

If you want to get OS Version per date you type

SELECT Date, OS, count(*) as NumberOfEntries
FROM YourTable
GROUP BY Date, OS
ORDER BY Date, OS

If you don't have huge amounts of data (like several million rows) you won't have any performance problems with suitable indexes.

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