简体   繁体   English

制作此MySQL数据库的最有效方法是什么?

[英]What is the most efficient way to make this MySQL database?

Ok, I have a database with with a table for storing classified posts, each post belongs to a different city. 好吧,我有一个数据库,带有一个用于存储分类帖子的表,每个帖子属于不同的城市。 For the purpose of this example we will call this table posts . 就本示例而言,我们将此表称为posts This table has columns: 此表包含以下列:

id        (INT, +AI),
cityid    (TEXT),
postcat   (TEXT), 
user      (TEXT), 
datatime  (DATETIME), 
title     (TEXT), 
desc      (TEXT), 
location  (TEXT)

an example of that data would be: 该数据的一个例子是:

'12039',
'fayetteville-nc',
'user@gmail.com',
'December 28th, 2010 - 11:55 PM',
'post title',
'post description',
'spring lake'

id is auto incremented, cityid is in text format (this is where I think i will be losing performance once the database is large)... id是自动递增的, cityid是文本格式(这是我认为一旦数据库很大我将失去性能)...

Originally I planned on having a different table for each city and now since a user has to have the option of searching and posting through multiple cities, I think I need them all in one table. 最初,我计划为每个城市使用不同的表格,现在由于用户不得不选择在多个城市中进行搜索和发布,因此我认为我需要将它们全部放在一个表格中。 Everything was perfect when I had one city per table, where I could: 当我每张桌子有一个城市时,一切都可以做到:

SELECT       *
FROM         `posts`
WHERE        MATCH (`title`, `desc`, `location`)
    AGAINST ('searchtext' IN BOOLEAN MODE)
  AND        `postcat` LIKE 'searchcatagory'

But then I ran into problems when trying to search multiple cities at one time, or listing all of a users posts for them to delete or edit. 但是,当我尝试一次搜索多个城市时,或者列出所有用户帖子以供删除或编辑时,我遇到了问题。

So looks like I have to have one table with all the posts, and also match another FULLTEXT field: cityid . 所以看起来我必须有一个包含所有帖子的表,并且还匹配另一个FULLTEXT字段: cityid I am guessing I need full-text because if a user chooses an entire state, and my cityid is "fayetteville-nc" I would need to match cityid against "-nc" this is only an assumption and I would love another way. 我猜我需要全文,因为如果用户选择整个州,而我的cityid是“fayetteville-nc”,我需要将cityid与“-nc”匹配,这只是一个假设,我会喜欢另一种方式。 This database could easily reach over a million rows within 6 months, and a fulltext search against 4 columns is probably going to be slow. 这个数据库可以在6个月内轻松达到一百多万行,而对四列的全文搜索可能会很慢。

My question is, is there a better way to do this more efficiently? 我的问题是,是否有更好的方法可以更有效地执行此操作? The database has nothing in it now, except for some test posts made by me. 除了我自己做的一些测试文章外,数据库现在什么都没有了。 So I can completely redesign the table structure if necessary. 因此,如有必要,我可以完全重新设计表结构。 I am open to any and all suggestions, even if it is just a more efficient way to perform my query. 我对任何和所有建议持开放态度,即使它只是一种更有效的方式来执行我的查询。

Yes, one table for all posts sounds sensible. 是的,所有职位一张桌子听起来很明智。 It would also be normal design for the posts table to have a city_id, referring to the id in a city table. 帖子表具有city_id也是正常的设计,它引用了city表中的id。 Each city would also have a state_id, referring to the id in a state table, and similarly each state would have a country_id referring to the id in a country table. 每个城市也有一个state_id,引用状态表中的id,类似地,每个州都有一个country_id引用国家表中的id。 So you could write: 所以你可以写:

SELECT $columns
FROM posts JOIN city ON city.id = posts.city_id
WHERE city.tag = 'fayetteville-nc'

Once you've brought the cities into a separate table, it might make more sense for you to do the city-to-city_id resolving up front. 一旦你把城市带到一个单独的桌子上,你可能更有意义地做到city-to-city_id预先解决。 This fairly naturally happens if you have a city chose from a dropdown, for instance. 例如,如果你有一个城市从下拉列表中选择,那么这种情况自然会发生。 But if you're entering free text into a search field, you may want to do it differently. 但是,如果您在搜索字段中输入自由文本,则可能需要采用不同的方式。

You can also search for all posts in a given state (or set of states) as: 您还可以搜索给定状态(或状态集)中的所有帖子:

SELECT $columns
FROM posts
     JOIN city ON city.id = posts.city_id
     JOIN state ON state.id = city.state_id
WHERE state.tag = 'NC'

If you're going to go more fancy or international, you may need a more flexible way of arranging locations into a hierarchy (eg you may want city districts, counties, multinational regions, intranational regions (Midwest, East Coast etc)) but stay easy for now :) 如果您想要更加花哨或国际化,您可能需要一种更灵活的方式将地点安排到一个层次结构中(例如,您可能需要城区,县,跨国地区,国内地区(中西部,东海岸等))但是留下来现在很容易:)

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

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