Stiahnite si na server cvika súbor explain.sql (cp -r /home/rjasko1/db12 ~; cd ~/db12) a spusťte psql -f explain.sql. Potom spusťte psql, postupne vykonávajte nasledujúce príkazy a analyzujte plány, ktoré vytvoril plánovač. Skúste pochopiť, prečo bol daný plán zvolený. Prvý z nasledujúcich príkazov vám umožní zistiť, koľko blokov na disku a koľko riadkov sa v danej relácii (v našom prípade ab) nachádza.
SELECT relpages, reltuples FROM pg_class WHERE relname = 'ab';
explain analyze select * from ab;
explain analyze select * from ab where b > 4 order by b;
explain analyze select * from ab where b = 4 order by b;
create index i1 on ab (b);
explain analyze select * from ab where b > 4 order by b;
explain analyze select * from ab where b = 4 order by b;
create index i1h on ab using hash(b);
explain analyze select * from ab where b > 4 order by b;
explain analyze select * from ab where b = 4 order by b;
Vykonajte posledný príkaz desaťkrát po sebe a sledujte, ako sa mení reálny spotrebovaný čas.
explain select * from ab, bc;
insert into bc select x.id, x.id + 1 from generate_series(1, 1000000) as x(id);
explain select * from ab, bc;
Pozor, ak v poslednom príkaze použijete aj analyze
, bude to trvať veľmi dlho (koľko riadkov by malo byť vo výsledku?).
Spusťte psql -f explain.sql a analyzujte plány, ktoré vytvorí plánovač pri nasledujúcich príkazoch.
explain select * from ab, bc where ab.b = bc.b;
explain select * from ab, bc where ab.b < bc.b;
explain select * from ab, bc where ab.b = bc.b order by ab.b;
explain select * from ab, bc where ab.b < bc.b order by ab.b;
create index i1 on ab (b);
explain select * from ab, bc where ab.b = bc.b;
explain select * from ab, bc where ab.b < bc.b;
explain select * from ab, bc where ab.b = bc.b order by ab.b;
explain select * from ab, bc where ab.b < bc.b order by ab.b;
create index i2 on bc (b);
explain select * from ab, bc where ab.b = bc.b;
explain select * from ab, bc where ab.b < bc.b;
explain select * from ab, bc where ab.b = bc.b order by ab.b;
explain select * from ab, bc where ab.b < bc.b order by ab.b;
insert into bc select x.id, x.id + 1 from generate_series(1, 1000000) as x(id);
insert into bc select x.id, x.id + 1 from generate_series(1, 1000000) as x(id);
insert into bc select x.id, x.id + 1 from generate_series(1, 1000000) as x(id);
explain select * from ab, bc where ab.b < bc.b order by ab.b;
explain analyze select * from ab, bc where ab.b = bc.b order by ab.b;
drop index i2;
create index i22 on bc (b, c);
explain analyze select * from ab, bc where ab.b = bc.b order by ab.b;
create index i1h on ab using hash(b);
create index i2h on bc using hash(b);
explain select * from ab, bc where ab.b = bc.b;
explain select * from ab, bc where ab.b < bc.b;
explain select * from ab, bc where ab.b = bc.b order by ab.b;
Všimnite si, ako "čas" vypočítaný plánovačom rastie, ak index obsahuje nepotrebné stĺpce.
Spusťte psql -f explain.sql a analyzujte plány, ktoré vytvorí plánovač pri nasledujúcich príkazoch.
explain select * from ab, bc, cd where ab.b = bc.b and bc.c = cd.c;
explain select * from ab, bc, cd where ab.b = bc.b and bc.c = cd.c order by bc.b;
create index i3 on cd(c);
explain select * from ab, bc, cd where ab.b = bc.b and bc.c = cd.c order by bc.b;
create index i22 on bc (b, c);
explain select * from ab, bc, cd where ab.b = bc.b and bc.c = cd.c order by bc.b;
insert into ab select x.id, x.id + 1 from generate_series(1, 100000) as x(id);
insert into bc select x.id, x.id + 1 from generate_series(1, 100000) as x(id);
insert into cd select x.id, x.id + 1 from generate_series(1, 100000) as x(id);
explain select * from ab, bc, cd where ab.b = bc.b and bc.c = cd.c;
explain select * from ab, bc, cd where ab.b = bc.b and bc.c = cd.c order by cd.c;
explain select cd.c, count(*) from ab, bc, cd where ab.b = bc.b and bc.c = cd.c group by cd.c order by cd.c;
psql
ALTER USER {vase_prihlasovacie_meno} WITH PASSWORD '{vase_nove_heslo}'
psql -h 127.0.0.1 test {vase_prihlasovacie_meno}
ssh -L5432:127.0.0.1:5432 meno_pouzivatela@cvika.dcs.fmph.uniba.sk
Pokračujeme v práci s tabuľkami vytvorenými minule (cvičenie 6).
psql -f world.sql
psql -f queries_emp.sql
\d emp resp. \d+ emp - zobrazí štruktúru tabuľky \d - zobrazí zoznam tabuliek v aktuálnej databáze \db - zobrazí zoznam databáz \c emp - pripojí sa k databáze emp \q - ukončenie konzoly
:- consult('kniznica.pl').
Body sa dajú získať len za domáce úlohy. Tie budú tri, a to za 30, 30 a 40 bodov.