Skip to content

Instantly share code, notes, and snippets.

@pichi
Created September 7, 2013 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pichi/6473329 to your computer and use it in GitHub Desktop.
Save pichi/6473329 to your computer and use it in GitHub Desktop.
Fastest previous date solution.
drop table if exists products;
drop table if exists prod;
drop table if exists `out`;
SET max_heap_table_size = 1024*1024*1024;
create table products ( id int not null, partner int not null, start date not null) engine=memory ;
load data infile '/tmp/test.data' into table products columns terminated by ',';
create table prod ( r int not null AUTO_INCREMENT primary key, rp int default 1, id int not null, partner int not null, start date ) engine=memory as select id, partner, start from products order by partner, start;
drop table products;
update prod set rp = r-1;
create table `out` engine=memory select p1.id, p1.partner, p1.start, p2.start prev from prod p1 left join prod p2 on p2.r = p1.rp and p2.partner = p1.partner;
drop table prod;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment