简体   繁体   English

每个用户无限项的MySQL数据库结构

[英]MySQL database structure for infinite items per user

I have a MySQL database with a growing number of users and each user has a list of items they want and of items they have - and each user has a specific ID 我有一个拥有越来越多用户的MySQL数据库,每个用户都有他们想要的项目列表和他们拥有的项目 - 每个用户都有一个特定的ID

The current database was created some time ago and it currently has each users with a specific row in a WANT or HAVE table with 50 columns per row with the user id as the primary key and each item WANT or HAVE has a specific id number. 当前数据库是在不久前创建的,它当前每个用户在WANT或HAVE表中都有一个特定的行,每行有50列,用户ID为主键,每个项目WANT或HAVE都有一个特定的id号。

this currently limits the addition of 50 items per user and greatly complicates searches and other functions with the databases 这目前限制每个用户增加50个项目,并使数据库的搜索和其他功能大大复杂化

When redoing the database - would it be viable to instead simply create a 2 column WANT and HAVE table with each row having the user ID and the Item ID. 重做数据库时 - 只需创建一个2列WANT和HAVE表,每行包含用户ID和项ID,是否可行。 That way there is no 'theoretical' limit to items per user. 这样,每个用户的项目没有“理论”限制。

Each time a member loads the profile page - a list of their want and have items will then be compiled using a simple SELECT WHERE ID = ##### statement from the have or want table 每次成员加载配置文件页面时,将使用来自have或want表的简单SELECT WHERE ID = #####语句编译其所需项目和项目列表

Furthermore i would need to make comparisons of user to user item lists, most common items, user with most items, complete user searches for items that one user wants and the other user has... - blah blah 此外,我需要将用户与用户项目列表,大多数常见项目,具有大多数项目的用户,完全用户搜索一个用户想要的项目以及另一个用户具有...的比较进行比较... - blah blah

The amount of users will range from 5000 - 20000 用户数量范围为5000 - 20000

and each user averages about 15 - 20 items 每个用户平均约15-20个项目

will this be a viable MySQL structure or do i have to rethink my strategy? 这将是一个可行的MySQL结构还是我必须重新考虑我的策略?

Thanks alot for your help! 非常感谢你的帮助!

This will certainly be a viable structure in mysql. 这肯定是mysql中一个可行的结构。 It can handle very large amounts of data. 它可以处理非常大量的数据。 When you build it though, make sure that you put proper indexes on the user/item IDs so that the queries will return nice and quick. 在构建它时,请确保在用户/项目ID上放置正确的索引,以便查询返回良好和快速。

This is called a one to many relationship in database terms. 这在数据库术语中称为一对多关系。

Table1 holds:
 userName | ID

Table2 holds:
userID | ItemID

You simply put as many rows into the second table as you want. 您只需将任意数量的行放入第二个表中即可。

In your case, I would probably structure the tables as this: 在你的情况下,我可能会将表格结构如下:

users
id | userName | otherFieldsAsNeeded

items
userID | itemID | needWantID

This way, you can either have a simple lookup for needWantID - for example 1 for Need, 2 for Want. 这样,您可以对needWantID进行简单查找 - 例如1表示Need,2表示Want。 But later down the track, you can add 3 for wishlist for example. 但是后来在赛道上,你可以为wishlist添加3个。

Edit: just make sure that you aren't storing your item information in table items just store the user relationship to the item. 编辑:只需确保您没有将项目信息存储在表items只需将用户关系存储到项目中即可。 Have all the item information in a table (itemDetails for example) which holds your descriptions, prices and whatever else you want. 将所有项目信息放在一个表格中(例如itemDetails),其中包含您的描述,价格和您想要的任何其他内容。

I would recommend 2 tables, a Wants table and a Have table. 我会推荐2个表,一个Wants表和一个Have表。 Each table would have a user_id and product_id. 每个表都有一个user_id和product_id。 I think this is the most normalized and gives you "unlimited" items per user. 我认为这是最正常化的,并为每个用户提供“无限”项目。

Or, you could have one table with a user_id, product_id, and type ('WANT' or 'HAVE'). 或者,您可以拥有一个包含user_id,product_id和type('WANT'或'HAVE')的表。 I would probably go with option 1. 我可能会选择1。

What you're theorizing is a very legitimate database structure. 你理论化的是一个非常合法的数据库结构。 For a many to many relationship (which is what you want), the only way I've seen this done is to, like you say, have a relationships table with user_id and item_it as the columns. 对于many to many关系(这是你想要的),我看到这样做的唯一方法就是像你说的那样,有一个关系表,其中user_id和item_it作为列。 You could expand on it, but that's the basic idea. 你可以扩展它,但这是基本的想法。

This design is much more flexible and allows for the infinite items per user that you want. 这种设计更加灵活,可以为每个用户提供所需的无限项目。

In order to handle wants and have, you could create two tables or you could just use one and have a third column which would hold just one byte, indicating whether the user/item match is a want or a need. 为了处理想要和拥有,你可以创建两个表,或者你可以只使用一个表,并有一个第三列,它只能容纳一个字节,表明用户/项匹配是想要还是需要。 Depending on the specifics of your projects, either would be a viable option. 根据项目的具体情况,要么是可行的选择。

So, what you would end up with is at least the following tables: 那么,你最终会得到的是至少下面的表格:

Table: users
Cols:
  user_id
  any other user info

Table: relationships
Cols:
  user_id
  item_id
  type (1 byte/boolean)

Table: items
Cols:
  item_id
  any other item info

Hope that helps! 希望有所帮助!

As you mentioned in your question, yes, it would make much more sense to have a separate tables for WANTs and HAVEs. 正如你在问题中提到的那样,为WANT和HAVE设置一个单独的表会更有意义。 These tables could have an Id column which would relate the row to the user, and a column that actually dictates what the WANT or HAVE item is. 这些表可以有一个Id列,它将行与用户相关联,以及一个实际指示WANT或HAVE项目的列。 This method would allow for much more room to expand. 这种方法可以提供更大的扩展空间。

It should be noted that if you have a lot of of these rows, you may need to increase the capacity of your server in order to maintain quick queries. 应该注意的是,如果您有很多这些行,您可能需要增加服务器的容量以保持快速查询。 If you have millions of rows, they will have a great deal of strain on the server (depending on your setup). 如果你有数百万行,他们将在服务器上承受很大的压力(取决于你的设置)。

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

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