标签:
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