Changes between Version 1 and Version 2 of WikiStart/Optimization


Ignore:
Timestamp:
03/25/25 21:49:37 (2 months ago)
Author:
203206
Comment:

mk1

Legend:

Unmodified
Added
Removed
Modified
  • WikiStart/Optimization

    v1 v2  
    1 = Оптимизација
     1== Оптимизација
     2
     3=== Оптимизиран SQL-код, кој работи поедноставно и побрзо
     4
     5{{{
     6create table account(
     7    id serial primary key,
     8    username varchar(255) not null,
     9    email varchar(255) not null,
     10    password varchar(255) not null,
     11    balance decimal(15, 2) not null
     12);
     13
     14create table "transaction"(
     15    id serial primary key,
     16    amount decimal(15, 2) not null,
     17        currency varchar(255) not null,
     18    type varchar(50) not null,
     19    timestamp timestamp default current_timestamp,
     20    account_id int not null,
     21    foreign key (account_id) references account(id)
     22);
     23
     24create table transfer(
     25    id serial primary key,
     26    s_id int not null,
     27    r_id int not null,
     28    foreign key(s_id) references account(id),
     29    foreign key(r_id) references
     30    account(id)
     31
     32);
     33
     34create table deposit(
     35    id serial primary key,
     36    account_id int not null,
     37    amount decimal(15,2) not null,
     38    timestamp timestamp default current_timestamp,
     39    foreign key(account_id) references account(id)
     40);
     41
     42create table withdraw(
     43    id serial primary key,
     44    account_id int not null,
     45    amount decimal(15,2) not null,
     46    timestamp timestamp default current_timestamp,
     47    foreign key(account_id) references account(id)
     48);
     49
     50create table transferred_money(
     51    id serial primary key,
     52    s_account_id int not null,
     53    r_account_id int not null,
     54    t_id int not null,
     55    currency varchar(50) not null,
     56    foreign key (s_account_id) references account(id),
     57    foreign key (r_account_id) references account(id),
     58    foreign key (t_id) references transaction(id)
     59);
     60
     61create or replace function after_transaction_trigger()
     62returns trigger as $$
     63begin
     64
     65        if new.type = 'Deposit' then
     66                insert into Deposit(account_id,amount, timestamp)
     67                values (new.account_id, new.amount, new.timestamp);
     68       
     69        elsif new.type = 'Withdrawal' then
     70                insert into Withdraw(account_id,amount, timestamp)
     71                values (new.account_id, new.amount, new.timestamp);
     72       
     73        elsif new.type like '(MKD) Transfer Out to%' then
     74                insert into transferred_money(s_account_id, r_account_id, t_id, currency)
     75                select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
     76                        new.id, new.currency;
     77
     78        elsif new.type like '(USD) Transfer Out to%' then
     79                insert into transferred_money(s_account_id, r_account_id, t_id, currency)
     80                select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
     81                        new.id, new.currency;
     82       
     83        elsif new.type like '(EUR) Transfer Out to%' then
     84                insert into transferred_money(s_account_id, r_account_id, t_id, currency)
     85                select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
     86                        new.id, new.currency;
     87                       
     88        elsif new.type like '(GBP) Transfer Out to%' then
     89                insert into transferred_money(s_account_id, r_account_id, t_id, currency)
     90                select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
     91                        new.id, new.currency;
     92
     93        end if;
     94        return new;
     95end;
     96$$ language plpgsql;
     97
     98create trigger after_transaction
     99after insert on transaction
     100for each row
     101execute function after_transaction_trigger();
     102
     103
     104CREATE OR REPLACE FUNCTION after_transaction_trigger2()
     105RETURNS TRIGGER AS $$
     106BEGIN
     107    IF NEW.type = 'Deposit' THEN
     108        -- Log deposit as a self-transfer
     109        INSERT INTO transfer(s_id, r_id)
     110        VALUES (NEW.account_id, NEW.account_id);
     111
     112    ELSIF NEW.type = 'Withdrawal' THEN
     113        -- Log withdrawal as a self-transfer
     114        INSERT INTO transfer(s_id, r_id)
     115        VALUES (NEW.account_id, NEW.account_id);
     116
     117    ELSIF NEW.type LIKE '(MKD) Transfer Out to%' OR
     118          NEW.type LIKE '(USD) Transfer Out to%' OR
     119          NEW.type LIKE '(EUR) Transfer Out to%' OR
     120          NEW.type LIKE '(GBP) Transfer Out to%' THEN
     121         
     122        -- Extract recipient username and insert into transfer table
     123        INSERT INTO transfer(s_id, r_id)
     124        SELECT NEW.account_id,
     125               (SELECT id FROM account
     126                WHERE username = TRIM(SUBSTRING(NEW.type FROM POSITION('Transfer Out to ' IN NEW.type) + 15))
     127                LIMIT 1);
     128
     129    END IF;
     130   
     131    RETURN NEW;
     132END;
     133$$ LANGUAGE plpgsql;
     134
     135CREATE TRIGGER after_transaction2
     136AFTER INSERT ON transaction
     137FOR EACH ROW
     138EXECUTE FUNCTION after_transaction_trigger2();
     139
     140
     141
     142select * from account;
     143select * from "transaction";
     144select * from transfer;
     145select * from transferred_money;
     146select * from deposit;
     147select * from withdraw;
     148
     149SELECT
     150    tm.s_account_id AS sender_id,
     151        a_s.username AS sender_username,
     152        tm.r_account_id AS receiver_id,
     153        a_r.username AS receiver_username,
     154    tm.t_id,
     155        t.timestamp,
     156        t.amount
     157   
     158FROM
     159    transferred_money tm
     160JOIN
     161    account a_s ON tm.s_account_id = a_s.id
     162JOIN
     163    account a_r ON tm.r_account_id = a_r.id
     164JOIN
     165    "transaction" t ON tm.t_id = t.id;
     166
     167
     168SELECT
     169    tm.s_account_id AS sender_id,
     170        a_s.username AS sender_username,
     171        tm.r_account_id AS receiver_id,
     172        a_r.username AS receiver_username,
     173    tm.t_id,
     174        t.timestamp,
     175        t.amount
     176   
     177FROM
     178    transferred_money tm
     179JOIN
     180    account a_s ON tm.s_account_id = a_s.id
     181JOIN
     182    account a_r ON tm.r_account_id = a_r.id
     183JOIN
     184    "transaction" t ON tm.t_id = t.id
     185WHERE
     186      timestamp<'2025-03-23 20:30:00.000000';
     187
     188SELECT
     189    tm.s_account_id AS sender_id,
     190        a_s.username AS sender_username,
     191        tm.r_account_id AS receiver_id,
     192        a_r.username AS receiver_username,
     193    tm.t_id,
     194        t.timestamp,
     195        t.amount,
     196        tm.currency
     197   
     198FROM
     199    transferred_money tm
     200JOIN
     201    account a_s ON tm.s_account_id = a_s.id
     202JOIN
     203    account a_r ON tm.r_account_id = a_r.id
     204JOIN
     205    "transaction" t ON tm.t_id = t.id
     206WHERE tm.currency='(EUR)'
     207ORDER BY timestamp;
     208
     209SELECT
     210        max(t.amount) as maximum,
     211        avg(t.amount) as "average",
     212        min(t.amount) as minimum
     213   
     214FROM
     215    transferred_money tm
     216JOIN
     217    account a_s ON tm.s_account_id = a_s.id
     218JOIN
     219    account a_r ON tm.r_account_id = a_r.id
     220JOIN
     221    "transaction" t ON tm.t_id = t.id
     222WHERE tm.currency='(USD)';
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234}}}