简体   繁体   English

使用MySQL进行多区域市场数据库设计

[英]Multi regional marketplace database design with MySQL

I'm looking for guidance on the database structure for a multi-regional website. 我正在寻找有关多区域网站的数据库结构的指南。

I am setting up a website, similar to craigslist.com, that will allow users to post adds in their city. 我正在建立一个类似于craigslist.com的网站,该网站将允许用户在其所在的城市中发布添加的内容。 I am using a MySQL database. 我正在使用MySQL数据库。

I want to have regions as subfolders linked to subdomains eg ca.mysite.com goes to mysite.com/ca. 我想将区域作为子文件夹链接到子域,例如ca.mysite.com转到mysite.com/ca。

My question is how do I manage the database(s) when I want to add another city to my website. 我的问题是,当我想向我的网站添加另一个城市时,如何管理数据库。

If I use a different database for each city, then users wont be able to login to other cities as their login details are stored in the city they registered at in the users table of it's db. 如果我为每个城市使用不同的数据库,则用户将无法登录其他城市,因为其登录详细信息存储在他们在数据库db的用户表中注册的城市中。

This may not be an issue though, as the content is city specific, like craigslist. 不过,这可能不是问题,因为内容是特定于城市的,例如craigslist。 But, should they wish to contact users in other cities, they wont be able to. 但是,如果他们希望与其他城市的用户联系,则将无法联系。

Also, there would be duplicate username's and email addresses overall, as users could register in all of the cities using the same email and username. 此外,由于用户可以使用相同的电子邮件和用户名在所有城市进行注册,因此总体上将有重复的用户名和电子邮件地址。

If I create a central database with for instance, a users table, and a messages table, and then a separate db for each city with all that cities 'posts' then when displaying basic information I will need to consult the city specific db plus the central db that stores user info. 如果我创建一个中央数据库,例如,一个用户表和一个消息表,然后为每个城市创建一个单独的数据库,其中包含所有城市的“职位”,那么在显示基本信息时,我将需要咨询特定于城市的数据库以及存储用户信息的中央数据库。

Alternatively I could store everything on one database, and store the logged-in user's location in a cookie, pass that to a session variable, and use that location as part of the database query when displaying search results etc. 或者,我可以将所有内容存储在一个数据库中,并将登录用户的位置存储在cookie中,将其传递给会话变量,并在显示搜索结果等时将该位置用作数据库查询的一部分。

However, would this not add an unnecessary overhead to each query? 但是,这不会为每个查询增加不必要的开销吗?

eg every search would have to have ' AND location = $user_location ' added in. 例如,每个搜索都必须添加'AND location = $ user_location'。

I really have no idea what would be the best method here. 我真的不知道什么是最好的方法。

Thanks in advance for any help on this. 在此先感谢您的任何帮助。

It seems that you still do not know clearly the system you want to build. 似乎您仍然不清楚要构建的系统。 Before starting designing a database for this system, please make sure that you have the full description of the system requirements. 在开始为此系统设计数据库之前,请确保您具有系统要求的完整描述。 Some sample questions that help you clarify are: 可以帮助您弄清一些示例问题:

  • What features does your website offer? 您的网站提供什么功能?
  • What are all the actions that a user using your system can do? 用户使用您的系统可以执行的所有操作是什么? With each action, is there any restrictions? 每个动作都有限制吗?

Some other questions relating to the system performance: - How many users do you expect to use your system? 与系统性能有关的其他一些问题:-您希望使用系统的用户数量是多少? - How fast and correct each action should be served? -每个动作应多快正确地执行? Which actions are used frequently? 经常使用哪些动作?

It makes no sense to design a system without a careful understanding about the system requirements. 如果不仔细了解系统要求,则设计系统是没有意义的。

So here is the sample database design and how it can support your requirements. 因此,这里是示例数据库设计以及它如何满足您的需求。

Cities(cityid, cname)
Users(userid, fname, lname, cityid, currcityid)
Messages(mid, senderid, receiverid, content)
Adverts(aid, title, content, userid, cityid)

When a user switches the city, update the currcityid field in its row in the Users table. 用户切换城市时,请更新“ Users表中其行中的currcityid字段。

When a user posts an ad on a city, insert a new row to the Adverts table. 当用户在城市上发布广告时,请在“ Adverts表中插入新行。 The userid and cityid of the new row are the ids of the corresponding user and city. useridcityid新行的是相应的用户和城市的ID。

When a user sends a message to another user in the system, add a row to the Messages table. 当用户向系统中的另一个用户发送消息时,请在“ Messages表中添加一行。 The senderid and the receiverid of the new row are the ids of the corresponding users. 新行的senderidreceiverid是相应用户的ID。

Query all ads in a city: SELECT * FROM Adverts WHERE cityid = $cityid 查询城市中的所有广告: SELECT * FROM Adverts WHERE cityid = $cityid

Query all ads of a user: SELECT * FROM Adverts WHERE userid = $userid 查询某个用户的所有广告: SELECT * FROM Adverts WHERE userid = $userid

Query all ads of a user in a specific city: SELECT * FROM Adverts WHERE cityid = $cityid AND userid = $userid 查询特定城市中用户的所有广告: SELECT * FROM Adverts WHERE cityid = $cityid AND userid = $userid在以下广告中: SELECT * FROM Adverts WHERE cityid = $cityid AND userid = $userid

Hope this helps. 希望这可以帮助。

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

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