简体   繁体   English

插入由特定用户保存到数据库中的多个URL的最佳方法是什么?

[英]what is the best approach to insert multple urls which is saved by a perticular user to the database?

i am making a bookmarking site where user saves his urls. 我正在建立一个书签站点,用户可以在其中保存他的网址。 so what is the best approach to insert all urls of a user into a database(mysql). 那么将用户的所有URL插入数据库(mysql)的最佳方法是什么。

1)create individual table for individual user. 1)为个人用户创建个人表。 in each table create 1 column and put 1 url per row(per cell). 在每个表格中创建1列,然后每行(每个单元格)放置1个url。

2) create 1 table and insert all urls into a single cell according to the row of the user. 2)创建1个表,然后根据用户的行将所有url插入单个单元格中。

if 2nd option is best then can anyone explain me how to do it? 如果第二个选项是最好的,那么谁能解释我该怎么做? i can insert one url in one cell but how to insert all urls into a single cell and again retrieve it to display in a proper manner. 我可以在一个单元格中插入一个URL,但是如何将所有URL插入单个单元格,然后再次检索它以适当的方式显示。 Thank you for taking time and reading my question. 感谢您抽出宝贵时间阅读我的问题。 I tried hard to do this myself but failed. 我自己努力做到这一点,但失败了。 that's why asking here. 这就是为什么在这里问。

Create a bookmark table with the following columns: (id, fk_user_id, url) . 用以下几列创建一个bookmark表: (id, fk_user_id, url) Each entry would have its own id, and the foreign-key would be the id of the user. 每个条目将具有其自己的ID,外键将是用户的ID。

You don't want to create an individual table per user. 您不想为每个用户创建一个单独的表。 That won't scale! 那不会扩展! Imagine you have a thousand users - that's at least a thousand tables that you have! 假设您有一千个用户-至少有一千个表!

The second approach could work, but has the following problems: 第二种方法可以工作,但有以下问题:

  • To add a URL, you have to read in the existing value, append the new value, and then write it back. 要添加URL,您必须读入现有值,附加新值,然后将其写回。 That's two database operations you are doing just to add a new value. 这就是您要添加新值的两个数据库操作。 If you take the foreign-key approach, you simply have to perform one write. 如果采用外键方法,则只需执行一次写入。
  • While perhaps not as likely, you could have a case where the user is using two clients and you end up getting a request to add a bookmark from both of them at close to the same time. 尽管可能不太可能,但您可能遇到这样的情况:用户正在使用两个客户端,并且最终您几乎同时从两个客户端收到添加书签的请求。 This can lead to consistency issues. 这可能导致一致性问题。 For example, assume you already had http://google.com in the column, and Client #1 wants to add http://reddit.com and Client #2 wants to add http://stackoverflow.com . 例如,假设您已经在该栏中添加了http://google.com ,并且客户端#1要添加http://reddit.com ,客户端#2要添加http://stackoverflow.com If Client #2 reads in the value before Client #1's data has been persisted, you will either end up with http://google.com, http://reddit.com or http://google.com, http://stackoverflow.com depending on who writes to the table last. 如果客户端#2在持久保存客户端#1的数据之前读入该值,则您将以http://google.com, http://stackoverflow.com http://google.com, http://reddit.comhttp://google.com, http://stackoverflow.com结尾http://google.com, http://stackoverflow.com取决于谁最后写入表。 This could even happen if you get multiple add requests from the same client at close to the same time. 如果您几乎同时从同一客户端收到多个添加请求,甚至可能会发生这种情况。
  • Add deleting bookmarks to the mix, and your consistency issues could get even worse. 将删除书签添加到组合中,您的一致性问题可能会变得更糟。 Deleting also becomes much more difficult, because instead of simply deleting by bookmark id, you now have to pull in the data, split it, and then perform string comparisons on the URLs to figure out what you need to delete, and then write the modified data back. 删除也变得更加困难,因为现在您不仅需要按书签ID进行删除,还必须提取数据,分割数据,然后对URL进行字符串比较以找出需要删除的内容,然后编写修改后的内容。数据返回。 What if you end up getting multiple delete requests close to the same time? 如果您最终几乎同时收到多个删除请求怎么办? You run into the same problem that you did with the case where you were adding new URLs. 您遇到了与添加新URL时相同的问题。

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

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