Guest User

Untitled

a guest
Feb 28th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const attachment = await client.createDatabase(`statement-cache-test.fdb`);
  2. const transaction = await attachment.startTransaction();
  3. try {
  4.     await attachment.execute(transaction, `
  5.         create table person (
  6.             id integer primary key,
  7.             name varchar(60) not null
  8.         )`);
  9.  
  10.     await attachment.execute(transaction, `
  11.         create table item (
  12.             id integer primary key,
  13.             description varchar(60) not null
  14.         )`);
  15.  
  16.     await attachment.execute(transaction, `
  17.         create table sale (
  18.             id integer primary key,
  19.             person_id integer not null references person,
  20.             sale_date date not null
  21.         )`);
  22.  
  23.     await attachment.execute(transaction, `
  24.         create table sale_item (
  25.             id integer primary key,
  26.             sale_id integer not null references sale,
  27.             item_id integer not null references item,
  28.             val numeric(10,2)
  29.         )`);
  30.  
  31.     await transaction.commitRetaining();
  32.  
  33.     for (let i = 0; i < 500; ++i) {
  34.         await attachment.execute(transaction,
  35.             `insert into person (id, name) values (?, ?)`,
  36.             [i, `${i}`]
  37.         );
  38.  
  39.         await attachment.execute(transaction,
  40.             `insert into item (id, description) values (?, ?)`,
  41.             [i, `${i}`]
  42.         );
  43.     }
  44.  
  45.     for (let i = 0; i < 500; ++i) {
  46.         await attachment.execute(transaction,
  47.             `insert into sale (id, person_id, sale_date) values (?, ?, dateadd(? day to date '2022-12-01'))`,
  48.             [i, i % 500, i]
  49.         );
  50.  
  51.         for (let j = 0; j < 3; ++j) {
  52.             await attachment.execute(transaction,
  53.                 `insert into sale_item (id, sale_id, item_id, val) values (?, ?, ?, ?)`,
  54.                 [i * 3 + j, i, (i * 3 + j) % 500, j]
  55.             );
  56.         }
  57.     }
  58.  
  59.     await transaction.commitRetaining();
  60.  
  61.     await attachment.execute(transaction,
  62.         `create index sale_date on sale (sale_date)`);
  63.  
  64.     await transaction.commitRetaining();
  65.  
  66.     const date1 = new Date(Date.parse('2022-01-01'));
  67.     const date2 = new Date(Date.parse('2022-12-31'));
  68.  
  69.     for (let i = 0; i < 15000; ++i) {
  70.         await attachment.executeSingleton(transaction, `
  71.             select extract(year from s.sale_date) yr,
  72.                    extract(month from s.sale_date) mnt,
  73.                    sum(si.val)
  74.               from person p
  75.               join sale s
  76.                 on s.person_id = p.id
  77.               join sale_item si
  78.                 on si.sale_id = s.id
  79.               join item i
  80.                 on i.id = si.item_id
  81.               where s.sale_date between ? and ?
  82.               group by extract(year from s.sale_date),
  83.                        extract(month from s.sale_date)
  84.               order by 1, 2
  85.             `,
  86.             [date1, date2]
  87.         );
  88.     }
  89. }
  90. finally {
  91.     await transaction.commit();
  92.     await attachment.dropDatabase();
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment