简体   繁体   English

Mysql-创建一个存根表来保存另一个表中某些行的ID

[英]Mysql - Creating a stub table that holds the Id's of certain rows in another table

I'm working on a project where I need to have different kinds of users, victims and members. 我正在一个项目中,我需要拥有不同类型的用户,受害者和成员。 The database should be able to support large amounts of records for users, say, for example 500k, but it's very reasonable that it grows even larger. 数据库应该能够为用户支持大量记录,例如500k,但是它变得更大是非常合理的。

Currently I have all of these records stored in the same table, "users", with a role field that describes what type of user it is. 目前,所有这些记录都存储在同一表“用户”中,其中的角色字段描述了用户的类型。 Initially this table was split in two, one for victims and one for members, but having a single table simplifies a lot of other work on the project. 最初,此表分为两部分,一个用于受害者,一个用于成员,但是只有一个表可以简化项目中的许多其他工作。

For the most part, there will be a much higher number of victims than members, which makes querying for victims not that much of a deal, because most of the rows will be victims. 在大多数情况下,受害人的数量将比成员多得多,这使得向受害人查询不是一件容易的事,因为大多数行将是受害人。 However, when needing to query for members (especially things like counting the number of rows which are members) I would assume it would waste a lot of time looking at rows that end up not being the ones I want. 但是,当需要查询成员时(尤其是像计算成员的行数之类的事情)时,我认为查找最终不是我想要的行将浪费大量时间。

My solution to this was to create a stub "members" table, that all it does is hold the id's of the rows in the user's table which are infact members, consisting only of a primary key + foreign key unsigned integer. 我对此的解决方案是创建一个存根“成员”表,它所做的只是保存用户表中实际上是成员的行的ID,这些ID仅由主键+外键无符号整数组成。

Does this solution actually mitigate the amount of time it would take to query for members ? 此解决方案是否真正减轻了查询成员所需的时间? What would be the best solution ? 最好的解决方案是什么? Again, I'm assuming that splitting the table into two separate ones would be the best option, and if that's the only reasonable way I'll choose that one, but having all in the same table does fix a bunch of other problems. 再次,我假设将表拆分为两个单独的表是最好的选择,如果这是唯一合理的方法,我将选择一个表,但是将所有表都放在同一表中确实可以解决其他问题。

Remember the old days, when a search engine would say "Found 2,571,462 hits"? 还记得搜索引擎说“找到2,571,462次点击”的旧时光吗? Did you believe the number? 你相信这个数字吗? Probably the number changed while you were reading it. 阅读时数字可能已更改。

Then they got smart and said "About 2,570,000 hits". 然后他们变得聪明,并说“大约257万点击”。 Clearly an approximation. 显然是一个近似值。 Perhaps estimated by some algorithm or counted once a day. 也许通过某种算法估算或每天计算一次。

Then came "Over 2,000,000 hits". 然后出现了“超过2,000,000次点击”。

And now no mention. 现在不提了。

I suggest that you can count once a day, then say "About 1,234 members". 我建议您每天可以计数一次,然后说“大约1,234个成员”。

The lesson here... Not all technical problems can (or even should) be solved in code. 这里的课程...并不是所有的技术问题都可以(甚至应该)用代码解决。 Some should be solved by changing the user's expectations. 应该通过改变用户的期望来解决一些问题。

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

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