简体   繁体   English

关系数据库结构逻辑配置

[英]Relational database structure logic configuration

I'm going to develop a relational database on server side with MySql RDBMS. 我将使用MySql RDBMS在服务器端开发一个关系数据库。

Since this database will contain many informations and there will be many simultaneous queries, i'd like to setup an optimized structure to avoid slow processes when retrieving datas. 由于此数据库将包含许多信息,并且将有许多同时查询,因此我想设置一种优化的结构,以避免在检索数据时过程缓慢。

My main table called "account" will contain information about name, surname ecc.. of users: 我的主表称为“帐户”,将包含有关用户的名称,姓氏等信息:

 TABLE 'account'

 ID - NAME - SURNAME - AGE

 Primary key -> 'ID'

The second table will be called "file" and has an id (foreign key referring account.id), a filename and a column called 'count': 第二个表将被称为“文件”,并具有一个ID(引用account.id的外键),一个文件名和一个名为“ count”的列:

 TABLE 'file'

 ID - FILENAME - COUNT

 Primary key -> {'ID','FILENAME'}

 Foreign key -> 'ID' -> 'account.ID'

My last table is called 'visit' and has this structure: 我的最后一个表称为“访问”,其结构如下:

 TABLE 'visit'

 ID - FILENAME - OPTION

 Primary key -> {'ID','FILENAME'}

 Foreign key -> 'ID' -> 'account.ID'
 Foreign key -> 'FILENAME' -> 'file.FILENAME'

While first table won't contain many rows, second table could have something like thousands of rows and third table hundreds of thousands rows. 虽然第一个表不会包含很多行,但是第二个表可能有几千行,第三个表有数十万行。

Maybe i'm wrong (i'm not an expert about db system management and optimization :D), but if i make simultaneous queries on 'visit' table with so many rows and joined with other tables, the process could become slow. 也许我错了(我不是数据库系统管理和优化方面的专家:D),但是如果我同时对具有这么多行并与其他表连接的'visit'表进行查询,则过程可能会变慢。

Is this structure good or can be improved? 这个结构好还是可以改善?

Last question, can i update automatically 'file' -> 'count' column with the count of rows contained in 'visit' where 'file.filename' = 'visit.filename' when 'visit' table is updated ? 最后一个问题,当“访问”表更新时,我是否可以使用“访问”中包含的行数自动更新“文件”->“计数”列,其中“ file.filename” =“ visit.filename”?

Or it's better to make a query like 'SELECT count(*) from ecc..' every time? 还是每次都进行类似“ SELECT ecc .. SELECT count(*)”的查询更好?

Hope i explained myself, thanks. 希望我自己解释一下,谢谢。

According to what you comment, your idea sounds good, but everything depends on the business, ie business logic — if you are considering make daily reports, weeks, monthly visits; 根据您的评论,您的想法听上去不错,但是一切都取决于业务,即业务逻辑-如果您考虑进行每日报告,几周,每月访问; if the business is interested in knowing the history of visits in the last 10, 20 or 30 years. 如果企业有兴趣了解过去10、20或30年的访问历史。 Depending on that, you could create from time to time historical data tables with a period of time (it may be a year). 以此为基础,您可以不定期创建具有一定时间段(可能是一年)的历史数据表。

As you can see there are many ways, and you should also take into account the part of business the technical side, such as disk space, backups, etc, how important is to keep historical data and for how long. 如您所见,有很多方法,并且还应考虑技术方面的业务部分(例如磁盘空间,备份等),保留历史数据有多重要以及保留多长时间。

EDIT : 编辑

Regarding your second question, yes you can, if you implement a trigger. 关于第二个问题,是的,如果您实现触发器,则可以。 But you can check this nice link to ensure if you're making a good decision to create a trigger. 但是您可以检查此链接,以确保您是否在决定创建触发器方面做出了正确的决定。 (I don't like copy and paste so I prefer share the link, if I'm breaking the rules , please do me know) (我不喜欢复制和粘贴,所以我更喜欢共享链接,如果我违反规则,请告诉我)

https://softwareengineering.stackexchange.com/questions/123074/sql-triggers-and-when-or-when-not-to-use-them https://softwareengineering.stackexchange.com/questions/123074/sql-triggers-and-when-or-when-not-to-use-them

UPDATE V.2.0: 更新V.2.0:

Suggestions: 意见建议:

  1. Try to not use restricted names on your column names (like count,option), I don't exactly if there are restricted on mysql but in SQL SERVER yes , so for portability is better not use names like this. 尝试不要在列名(例如count,option)上使用受限名称, 我不完全 了解mysql是否受限制, 但在SQL SERVER中是yes ,因此 为了可移植性 最好不要使用这样的名称。

  2. Like Jonathan Leffler says, its not a good idea save the age, you can prefer save a birthdate. 就像乔纳森·莱夫勒(Jonathan Leffler)所说的那样,保存年龄并不是一个好主意,您更喜欢保存生日。

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

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