以前做过一个小规模的代购类电商网站,每天网站会增加1000左右的新商品,在商品数增加到65万左右时,由于服务器配置不是很高,而且是基于zend开发,只要访问与商品有关的页面,就会mysql数据库卡死,并报 sort aborted 错误,让人很是困扰。网上有人提供的一些解决sort aborted的办法,但是由于服务器配置限制,所以没什么用。之前开发时水平有限,就担心过出现此问题。今天决定彻底处理下。
分析了一下,本代购类电商网站的特点,由于产品更新比较快,所以以前的产品很快就被淘汰,只要保留10万左右最新商品就足够了,之前的商品有销售的,也要保留下来。
直接删除不符合规则的数据
商品表
商品订单表
delete product from product,orderproduct where (product.id <> orderproduct.productid) or (product.id < 550000 and product.id <> orderproduct.productid);
但是mysql数据库报超时错误。应该是数据库配置不够执行上面的命令。
间接提取合并数据
有销售的商品记录放在pa表,
create table pa (select product.id, product.name, product.note, product.content, product.menuaid, product.menubid, product.buywayid, product.stallid, product.issingleton, product.notsingleintro, product.valid, product.quantity, product.minquantity, product.cost, product.hits, product.sort, product.isshow, product.authorid, product.added, product.sizeid, product.pidstr, product.productdesc, product.stallname, product.sales from product,orderproduct where product.id = orderproduct.productid);
最近的10万左右商品记录放在pb表,
create table pb (select product.id, product.name, product.note, product.content, product.menuaid, product.menubid, product.buywayid, product.stallid, product.issingleton, product.notsingleintro, product.valid, product.quantity, product.minquantity, product.cost, product.hits, product.sort, product.isshow, product.authorid, product.added, product.sizeid, product.pidstr, product.productdesc, product.stallname, product.sales from product where product.id > 550000);
把pa表的数据放入新建的pc表,
create table pc select * from pa;
把pb表的数据放入pc表,
insert into pc select * from pb;
去掉pc中重复记录放入新建的pd表,
create table pd (select distinct id, name, note, content, menuaid, menubid, buywayid, stallid, issingleton, notsingleintro, valid, quantity, minquantity, cost, hits, sort, isshow, authorid, added, sizeid, pidstr, productdesc, stallname, sales from pc);
pd表中数据就是符合要求的数据,
备份原来product表中的数据,
alter table product rename pe;
把表pd重命名为product表,
alter table pd rename product;
把product表的id改为自动编号,
alter table product modify id bigint(20) primary key auto_increment;
最后测试,问题解决。