Last active
December 29, 2015 14:28
-
-
Save dmitry-vsl/7683672 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drop table if exists user_course cascade; | |
drop table if exists users cascade; | |
drop table if exists courses cascade; | |
create table users(id serial,data text, primary key(id)); | |
insert into users(data) select lpad('',100,md5(id::text)) | |
from generate_series(1,1000000) t(id); | |
create table courses(id serial,data text,primary key(id)); | |
insert into courses(data) select lpad('',100,md5(id::text)) | |
from generate_series(1,100000) t(id); | |
create table user_course(course_id bigint, user_id bigint, primary key(user_id,course_id), | |
foreign key(course_id) references courses(id), | |
foreign key(user_id) references users(id)); | |
do $$ | |
declare c record; | |
begin | |
for c in select * from courses loop | |
for i in 1..10 loop | |
begin | |
insert into user_course(course_id,user_id) values( | |
c.id, | |
(random()*1000000)::bigint | |
); | |
exception when unique_violation then | |
end; | |
end loop; | |
end loop; | |
end | |
$$; | |
analyze; | |
create or replace function unassigned_users_top_10(bigint) returns setof users as ' | |
select * from users | |
where not exists | |
(select user_id from user_course | |
where course_id = $1 and | |
users.id = user_id) order by id limit 10; | |
' language sql; | |
do $$ | |
declare i bigint; | |
declare rnd bigint; | |
begin | |
for i in select a from generate_series(1,1000) t(a) loop | |
perform unassigned_users_top_10((i * 13*19*17 % 100000)::bigint); | |
end loop; | |
end | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment