0

我将所有视频和每个视频的详细信息存储在数据库中。我有一些桌子:

  • 视频(有关视频的详细信息、名称等)
  • video_file(每个视频以不同的比特率和不同的屏幕尺寸呈现)
  • 播放列表(将视频存储在预定义的播放列表中)

每个视频都有针对 1080 和 720 编码的文件,并且每个文件都再次使用三种不同的比特率进行编码,并且这些比特率中的每一个都再次使用 WebM 和 MP4 进行编码。因此,每个视频条目的 video_file 中有六个条目。所以我的查询每个视频返回 12 行。

我看到的问题是,对于每一行,都包含所有信息,而不仅仅是“neccecary”信息。例子:

| id |   name   |    playlist    |   file   | quality | size | type |
---------------------------------------------------------------------
  1     video1      funny videos     file1      3        720    MP4
  1     video1      funny videos     file2      2        720    MP4
  1     video1      funny videos     file3      1        720    MP4
  1     video1      funny videos     file4      3        1080   MP4
  1     video1      funny videos     file5      2        1080   MP4
  1     video1      funny videos     file6      1        1080   MP4
  1     video1      funny videos     file7      3        1080   WebM
  1     video1      funny videos     file8      2        1080   WebM
  1     video1      funny videos     file9      1        1080   WebM
  1     video1      funny videos     file10     3        720    WebM
  1     video1      funny videos     file11     2        720    WebM
  1     video1      funny videos     file12     1        720    WebM

这是一个示例输出。我认为更有效的是对结果进行分组,这样您就不会逐行获得冗余信息,毕竟,您将在 PHP 或您使用的语言中对这些信息进行分组以便能够使用它一点也不。

我认为更有效的例子:

| id |   name   |    playlist    |   file   | quality | size | type |
---------------------------------------------------------------------
  1     video1      funny videos     file1       3      1080    WebM
                                     file2                      MP4
                                     file3              720     WebM
                                     file4                      MP4
                                     file5       2      1080    WebM
                                     file6                      MP4
                                     file7              720     WebM
                                     file8                      MP4
                                     file9       1      1080    WebM
                                     file10                     MP4
                                     file11             720     WebM
                                     file12                     MP4

这样,您可以轻松设置与列名对应的一些变量:

id, name, playlist, file, quality, size, type

while (record = fetchRecord)
    id = record.id or id
    name = record.name or name
    playlist = record.playlist or playlist
    file = record.file or file
    quality = record.quality or quality
    size = record.size or size
    type = record.type or type

    // Now you have filled the variables with the exact information you had in the first recordset example, but without wasting so much unnecessary bandwidth and processing power. 

现在,我在另一个表中添加了在视频播放期间弹出的注释,因此如果视频上有 x 个注释,则每个视频将返回 12x 行。

疯了吧!我错过了什么吗?我在这里“解释”的事情实际上可以做到吗?还是像今天一样让一个查询返回每个视频 12 行,然后遍历每个视频以获取注释,或者什么更好?

我的问题到此结束,非常感谢您一直阅读本文!

4

2 回答 2

1

An alternative way to do something like this would be to use the GROUP_CONCAT function across the columns that differ, and group on the column(s) that repeat(s). This will create a delimited string column in the output that you will need to parse in PHP; however, it will reduce your row count to only the number of unique combinations of the non-grouped columns. For example:

select name, playlist, GROUP_CONCAT(file, '|', quality, '|', size, '|', type SEPARATOR '/') AS delimited_pairs
from video
group by name, playlist;

In the above example, the resulting delimited_pairs column will delimit each unique column value per name/playlist (the grouping columns) using the pipe character, and each of these unique combinations will be delimited by forward slash.

于 2013-05-31T15:44:04.367 回答
0

我认为您的数据布局有问题。您的“冗余数据”不应该进入video_file表格,而是在video表格中,因为它是指视频的信息,而不是文件!这种数据库方案在一致性方面也很糟糕,例如。将更新视频所属的播放列表的信息,您必须为所有文件执行此操作...

于 2013-05-31T11:33:56.847 回答