码迷,mamicode.com
首页 > 数据库 > 详细

工作笔记 - 微博数据库设计

时间:2015-02-16 18:08:52      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

users表设计
CREATE TABLE `users` (
`UID` int(20) NOT NULL AUTO_INCREMENT,
`Username` varchar(20) DEFAULT NULL,
`Password` varchar(20) DEFAULT NULL,
PRIMARY KEY (`UID`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

fans表设计
CREATE TABLE `fans` (
`FansID` int(20) NOT NULL AUTO_INCREMENT,
`UID` int(20) DEFAULT NULL,
`FID` int(20) DEFAULT NULL,
PRIMARY KEY (`FansID`),
KEY `UID` (`UID`),
KEY `FID` (`FID`),
CONSTRAINT `FID` FOREIGN KEY (`FID`) REFERENCES `users` (`UID`),
CONSTRAINT `UID` FOREIGN KEY (`UID`) REFERENCES `users` (`UID`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

tmp表设计
CREATE TABLE `tmp` (
`ID` int(20) NOT NULL AUTO_INCREMENT,
`UID` int(20) DEFAULT NULL,
`Message` varchar(20) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `UID2` (`UID`),
CONSTRAINT `UID2` FOREIGN KEY (`UID`) REFERENCES `users` (`UID`)
) ENGINE=InnoDB AUTO_INCREMENT=138353 DEFAULT CHARSET=latin1;

message表设计
CREATE TABLE `message` (
`MSGID` int(20) NOT NULL AUTO_INCREMENT,
`OID` int(20) DEFAULT NULL,
`UID` int(20) DEFAULT NULL,
`Message` varchar(100) DEFAULT NULL,
`State` bit(1) DEFAULT b‘0‘,
PRIMARY KEY (`MSGID`),
KEY `OID` (`OID`),
KEY `UID1` (`UID`),
CONSTRAINT `OID` FOREIGN KEY (`OID`) REFERENCES `users` (`UID`),
CONSTRAINT `UID1` FOREIGN KEY (`UID`) REFERENCES `users` (`UID`)
) ENGINE=InnoDB AUTO_INCREMENT=2949116 DEFAULT CHARSET=latin1;

方案1:
查询
select f.fid,t.uid,t.message from fans f left join tmp t on f.uid = t.uid

插入到接收表:(耗时 28.656ms秒)
insert into message(oid,uid,message) select f.fid,t.uid,t.message from fans f left join tmp t on f.uid = t.uid

建立存储过程:
CREATE PROCEDURE myProc()
begin
DECLARE i int;
SET i=1;
loop1: WHILE i<=99999 DO
insert into tmp (uid,message)values(1,"abc");
SET i=i+1;
END WHILE loop1;
end;

调用存储过程:
call myProc();

分批插入:(耗时两批每次21秒)
1、统计总数
select count(f.uid) from fans f left join tmp t on f.uid = t.uid

2、除以分批次数而后插入
begin
DECLARE i int;
select count( f.uid)/2 into i from fans f left join tmp t on f.uid = t.uid;
insert into message(oid,uid,message) select f.fid,t.uid,t.message from fans f left join tmp t on f.uid = t.uid limit 0,i;
end

3、查询message表数据(耗时0.468秒)message表70w记录2条state=1
select * from message where oid =2 and state =1

4、注意事项
3.1、在执行上面操作前,不允许加入新的粉丝和发表新的微薄内容。添加一个状态,判断是否正在处理中。如果是,需要使用队列对新加入粉丝和微薄进行缓存。

方案2:(耗时1.140秒)tmp表40w记录 fans表10w记录
关联fans表和tmp表。fans表保存粉丝关系,tmp表保存微薄消息
select * from fans f left join tmp t on f.uid = t.uid where f.fid =2 and state =1

工作笔记 - 微博数据库设计

标签:

原文地址:http://www.cnblogs.com/changzhoudajiake/p/4294413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!