表A储存帖子,主键`ID`,字段`LAST_REPLIES`,字段`CONTENT`(`LAST_REPLIES`存储最近20条回复)
表B储存回复,主键`ID`,降序索引(`PARENT`,`TIME`),字段`CONTENT`(`PARENT`是被回复帖的ID)
LAST_REPLIES
,一共600个,然后SELECT * FROM `B` WHERE `ID` IN (####一共六百条ID####)
THREAD_ID
,一共30个,然后SELECT * FROM `B` WHERE `PARENT` = :ID01 ORDER BY `TIME` DESC LIMIT 0,20
UNION ALL
SELECT * FROM `B` WHERE `PARENT` = :ID02 ORDER BY `TIME` DESC LIMIT 0,20
UNION ALL
SELECT * FROM `B` WHERE `PARENT` = :ID03 ORDER BY `TIME` DESC LIMIT 0,20
UNION ALL
...... ......
SELECT * FROM `B` WHERE `PARENT` = :ID30 ORDER BY `TIME` DESC LIMIT 0,20
THREAD_ID
,语句是 [注意降序索引(PARENT
,TIME
)]SET @NUM := 0, @PARENT := 0;
SELECT * ,
@NUM := if(@PARENT = `PARENT`, @NUM + 1, 1) as "COUNT",
@PARENT = `PARENT` AS "DUMMY"
FROM `B` FORCE INDEX(`PARENT`)
GROUP BY `PARENT`
HAVING @NUM <= 20
WHERE `PARENT` IN (####一共三十条ID####)
1
schezuk OP 唔……
|
2
juxingzhutou 2015-03-06 22:04:22 +08:00
不是很懂数据库,提个设想,请指正:每个帖子的回复单独建一张表?
|
3
juxingzhutou 2015-03-06 22:06:07 +08:00
是不是数据库的索引其实已经有我上面说的这个效果了?
|