我正在使用InnoDB设计论坛数据库,并且提出了一些问题,有没有人可以帮我澄清一下?
帖子和评论下面的表格设计
CREATE TABLE `my_posts` (
`forum_id` mediumint(8) unsigned NOT NULL,
`post_id` int(10) unsigned NOT NULL, // not an auto increment
subject varchar(200) NOT NULL,
`details` text NOT NULL,
`access` tinyint(3) unsigned NOT NULL, //private,friends,public
`created_by` int(10) unsigned NOT NULL,
`created_on` int(10) unsigned NOT NULL,
`updated_on` int(10) unsigned NOT NULL,
`comment_count` smallint(5) unsigned NOT NULL DEFAULT '0',
`ip_address` int(10) unsigned NOT NULL,
sphinx_unique_id int(10) unsigned NOT NULL, // not an auto increment
PRIMARY KEY (`forum_id`,`post_id`,created_by)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `my_posts_comments` (
`forum_id` mediumint(8) unsigned NOT NULL,
`post_id` int(10) unsigned NOT NULL, // not an auto increment
`comment_id` int(10) unsigned NOT NULL,
`details` text NOT NULL,
`created_by` int(10) unsigned NOT NULL,
`created_on` int(10) unsigned NOT NULL,
`ip_address` int(10) unsigned NOT NULL,
PRIMARY KEY (`forum_id`,`post_id`,comment_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我根据 forum_id,post_id,created_by 进行查询,并对 updated_on,comment_count
进行排序SELECT * FROM my_posts WHERE access=public ORDER BY updated_on DESC //OR ASC
SELECT * FROM my_posts WHERE access=public ORDER BY comment_count DESC //OR ASC
SELECT * FROM my_posts WHERE forum_id=? and access=public ORDER BY updated_on DESC //OR ASC
SELECT * FROM my_posts WHERE forum_id=? and access=public ORDER BY comment_count DESC //OR ASC
SELECT * FROM my_posts WHERE created_by=? ORDER BY updated_on DESC //OR ASC
SELECT * FROM my_posts WHERE created_by=(Friends_ids) AND access=(public,friends) ORDER BY updated_on DESC //OR ASC
SELECT * FROM my_posts WHERE forum_id=? AND post_id=?
SELECT * FROM my_posts_comments WHERE forum_id=? AND post_id=?
1)由于 forum_id,post_id,created_by 上的复合主键,我发现访问它的问题只是在 created_by 上进行调整。在 created_by 上查询的最佳设计是什么?我可以从复合键中删除 created_by ,只需为其添加索引即可。我将在这里放松群集性能..
2)在论坛级别增加post_id的最佳方法是什么?或者我可以在表级递增。例如
示例数据(论坛级别)
FORUM_ID POST_ID
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
2 5
或表级
FORUM_ID POST_ID
1 1
1 2
1 3
1 4
2 5
2 6
2 7
2 8
2 9
如果我可以使用上面的表级增量,我可以删除sphinx_unique_id列,因为我只使用它进行sphinx搜索。同样在评论表中,我可以删除forum_id列。
3)我在 updated_on 和 comment_count 上排序的所有查询。这两个不是PK,所以前两个查询看起来像一个主要问题..
感谢大家的帮助