简体   繁体   English

MySql数据库设计-哪种表结构更好?

[英]MySql database design - which table structure would be better?

I am working on a mysql database and would like to know which of these two table structures is better in terms of performance. 我正在使用mysql数据库,想知道这两个表结构中哪个在性能方面更好。

Structure 1; 结构1; two tables with lots of common fields 有很多共同领域的两个表

table1(file)   : id, userid, parentid, name, desc, tags, public, dateadded, datemodified, linkid, domain, url, snapshot
table2(folder): id, userid, parentid, name, desc, tags, public, dateadded, datemodified, hasSubFolders, noOfItems, noOfViews

Structure 2; 结构2; three tables with all the common fields put in a different table 三个表,所有公共字段放在不同的表中

table1(main)   : id, userid, parentid, objectid, objecttype(file or folder), name, desc, tags, public, dateadded, datemodified
table2(file)   : id (objectid), linkid, domain, url, snapshot
table3(folder) : id (objectid), hasSubFolders, noOfItems, noOfViews

I would use neither of them but something similar to the first example. 我不会使用它们,而是使用类似于第一个示例的东西。 There are a lot of common columns in the two tables. 在两个表中有很多公共列。 Therefore, two tables would have to be accessed just for a listing command ( ls in UNIX, dir ind Windows). 因此,仅需使用清单命令即可访问两个表(在UNIX中为ls在Windows中为dir ind)。 Also, there are hints to the data-types in the column-names which is bad practice: 另外,在列名中也有对数据类型的提示,这是一种不好的做法:

Instead, use one table: fsys_entries: 而是使用一个表:fsys_entries:

id, is_directory, parentid, name, desc, tags, public, added, modified, linkid, url, snapshot

With this structure it would be possible to create files and directories within files but with the correct restrictions from the code that is not a problem. 通过这种结构,可以在文件中创建文件和目录,但是不受代码问题的正确限制。

EDIT: In a real file-system a directory is a file with a directory-flag to it. 编辑:在真实的文件系统中,目录带有目录标志的文件。 So from that perspective its not too bad and it allows a more similar treatment as compared to a real file-system. 因此,从这个角度来看,它还不错,并且与真实文件系统相比,它允许更相似的处理。

PS: As an advanced feature i would store the sharing-information (is the file/directory public? what link refers to it?) in a different table (which is just best practice). PS:作为一项高级功能,我会将共享信息(文件/目录是否公开?链接指向的是什么?)存储在另一张表中(这只是最佳做法)。

If it performs comparably to the flat structure1, I'd go for Structure2 after renaming "main" to "FileSystemObject" or something more telegraphic. 如果它的性能与扁平的structure1相当,则在将“ main”重命名为“ FileSystemObject”或更具有电报性之后,我会去使用Structure2。

The disadvantage is that you need joins for accessing the full information on any given FSO and this makes indexing more difficult (not sure if mysql supports indexed views nowadays). 缺点是您需要使用联接来访问任何给定FSO上的全部信息,这使索引编制变得更加困难(不确定mysql现在是否支持索引视图)。

In case of doubt, populate sample tables and benchmark. 如有疑问,请填写样本表和基准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM