0

我正在寻找有关多区域网站数据库结构的指导。

我正在建立一个类似于 craigslist.com 的网站,允许用户在他们的城市发布添加内容。我正在使用 MySQL 数据库。

我希望将区域作为链接到子域的子文件夹,例如 ca.mysite.com 转到 mysite.com/ca。

我的问题是当我想向我的网站添加另一个城市时如何管理数据库。

如果我为每个城市使用不同的数据库,那么用户将无法登录到其他城市,因为他们的登录详细信息存储在他们在其数据库的用户表中注册的城市中。

但这可能不是问题,因为内容是特定城市的,例如 craigslist。但是,如果他们想联系其他城市的用户,他们就做不到了。

此外,总体上会有重复的用户名和电子邮件地址,因为用户可以使用相同的电子邮件和用户名在所有城市注册。

如果我创建一个中央数据库,例如,一个用户表和一个消息表,然后为每个城市创建一个单独的数据库,其中包含所有城市的“帖子”,那么在显示基本信息时,我需要咨询城市特定的数据库以及存储用户信息的中央数据库。

或者,我可以将所有内容存储在一个数据库中,并将登录用户的位置存储在 cookie 中,将其传递给会话变量,并在显示搜索结果等时将该位置用作数据库查询的一部分。

但是,这不会给每个查询增加不必要的开销吗?

例如,每次搜索都必须添加“AND location = $user_location”。

我真的不知道这里最好的方法是什么。

提前感谢您对此的任何帮助。

4

2 回答 2

0

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.

When a user posts an ad on a city, insert a new row to the Adverts table. The userid and cityid of the new row are the ids of the corresponding user and city.

When a user sends a message to another user in the system, add a row to the Messages table. The senderid and the receiverid of the new row are the ids of the corresponding users.

Query all ads in a city: SELECT * FROM Adverts WHERE cityid = $cityid

Query all ads of a user: 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

Hope this helps.

于 2014-11-06T01:03:41.313 回答
0

看来你还不清楚你要构建的系统。在开始为此系统设计数据库之前,请确保您拥有系统要求的完整描述。一些可以帮助您澄清的示例问题是:

  • 您的网站提供哪些功能?
  • 使用您的系统的用户可以执行哪些操作?对于每个动作,是否有任何限制?

与系统性能有关的其他一些问题: - 您希望有多少用户使用您的系统?- 每个动作的执行速度和正确性如何?哪些动作经常使用?

如果不仔细了解系统需求,设计系统是没有意义的。

于 2014-11-05T21:16:42.053 回答