交易记录首先存在一个临时表中,定期把这些数据插入到正式销售记录表中(这个表很大)。
同时把这个临时表中的数据插入到last表中,用于查询最后100笔销售记录,要求last表对于某个代理商来说
最多只能保存100条最新(按交易时间)记录,这样这个last表将会很小,而且不占用太多空间,查询效率会高。
如何实现写last表这个功能?有什么好的办法,谢谢大家了。作者: zhangshebao 时间: 2007-09-11 13:10
不是精确100条,是保持100条或略多一点,因为在边界时间点,可能正好有多条销售时间相同的记录.
临时表为t
insert into last select * from t;
create temporary table temp_t
select l1.id,l1.t,count(*) from last l1,last l2
where l1.id in (select distinct id from t) and l1.id=l2.id and l1.t<=l2.t
group by l1.id,l1.t having count(*)<=100;
delete from last
where id in (select distinct id from t) and
t<(select max(t) from temp_t where id=last.id);
drop table temp_t;