You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:ea select * from track where albumid > 0;
create index track_albumid on track(albumid asc);
:ea select * from track where albumid > 0;
covering index
:ea select*from track where albumid=1;
:ea select trackid, composer from track where albumid=1;
createindextrack_albumid_1on track(albumid) include(trackid, composer);
partial index
create index employee_city on employee(city) where city not in ('Lethbridge', 'Edmonton');
:ea select * from employee where city='Lethbridge';
:ea select * from employee where city='Calgary';
index forward and backward scan
createindexcustomer_repidon customer(supportrepid asc);
:ea select*from customer order by supportrepid;
:ea select*from customer order by supportrepid desc;
hints
:ea
/*+Leading ( ( ( a t ) ar ) ) */SELECTt.TrackId,
t.NameAS track_name,
a.TitleAS album_title,
ar.NameAS artist_name
FROM Track t
INNER JOIN Album a ONt.AlbumId=a.AlbumIdINNER JOIN Artist ar ONa.ArtistId=ar.ArtistIdWHEREt.TrackId=5;
:ea
/*+Leading ( ( ar ( a t ) ) ) */SELECTt.TrackId,
t.NameAS track_name,
a.TitleAS album_title,
ar.NameAS artist_name
FROM Track t
INNER JOIN Album a ONt.AlbumId=a.AlbumIdINNER JOIN Artist ar ONa.ArtistId=ar.ArtistIdWHEREt.TrackId=5;
batch nested loop
set yb_bnl_batch_size=512;
:ea
SELECTp.PlaylistId,
p.NameAS playlist_name,
t.NameAS track_name,
ar.NameAS artist_name
FROM Playlist p
INNER JOIN PlaylistTrack pt ONp.PlaylistId=pt.PlaylistIdINNER JOIN Track t ONpt.TrackId=t.TrackIdINNER JOIN Album a ONt.AlbumId=a.AlbumIdINNER JOIN Artist ar ONa.ArtistId=ar.ArtistIdWHEREp.PlaylistId=3;
prepare statements
prepare employee_salary(int) asselect ename, sal from emp where empno=$1;
execute employee_salary(7900);
common table expression (cte)
with emp_evaluation_period as (
select ename,
deptno,
hiredate,
hiredate + case
when job in ('MANAGER', 'PRESIDENT') then interval '3 month'
else interval '4 weeks'
end evaluation_end
from emp
)
select*from emp_evaluation_period e1
join emp_evaluation_period e2 on (e1.ename>e2.ename)
and (e1.deptno=e2.deptno)
where (e1.hiredate, e1.evaluation_end) overlaps (e2.hiredate, e2.evaluation_end);
recursive cte
with recursive emp_manager as (
select empno,
ename,
ename aspathfrom emp
where ename ='JONES'union allselectemp.empno,
emp.ename,
emp_manager.path||' manages '||emp.enamefrom emp
join emp_manager onemp.mgr=emp_manager.empno
)
select*from emp_manager;
window functions
select dname,
ename,
job,
coalesce (
'hired '|| to_char(
hiredate - lag(hiredate) over (per_dept_hiredate),
'999'
) ||' days after '|| lag(ename) over (per_dept_hiredate),
format('(1st hire in %L)', dname)
) as"last hire in dept"from emp
join dept using(deptno) window per_dept_hiredate as (
partition by dname
order by hiredate
)
order by dname,
hiredate;
group by
with groups as (
select ntile(3) over (
order by empno
) group_num,
*from emp
)
select string_agg(format('<%s> %s', ename, email), ', ')
from groups group by group_num;
procedure
-- create procedure
create or replace procedure commission_transfer(empno1 int, empno2 int, amount int) as $$
beginupdate emp set comm=comm-commission_transfer.amountwhere empno=commission_transfer.empno1and comm>commission_transfer.amount;
if not found then raise exception 'Cannot transfer % from %',amount,empno1; end if;
update emp set comm=comm+commission_transfer.amountwhereemp.empno=commission_transfer.empno2;
if not found then raise exception 'Cannot transfer from %',empno2; end if;
end;
$$ language plpgsql;
-- call the procedure
call commission_transfer(7521,7654,100);
-- select from empselect*from emp where comm is not null;
-- call the procedure
call commission_transfer(7521,7654,1000000);
triggers
-- add columnaltertable dept add last_update timestamptz;
-- create triggercreate or replacefunctiondept_last_update() returns trigger as $$
beginnew.last_update:=transaction_timestamp();
return new;
end;
$$ language plpgsql;
-- add triggercreatetriggerdept_last_update
before updateon dept
for each row
execute procedure dept_last_update();
-- select tableselect deptno, dname, loc, last_update from dept;
-- update tablebegin transaction;
update dept set loc='SUNNYVALE'where deptno=30;
select pg_sleep(3);
update dept set loc='SUNNYVALE'where deptno=40;
commit;
-- select tableselect deptno, dname, loc, last_update from dept;
materialized views
-- create materialized view
create materialized view report_sal_per_dept asselect deptno,
dname,
sum(sal) sal_per_dept,
count(*) num_of_employees,
string_agg(distinct job, ', ') distinct_jobs
from dept
join emp using(deptno)
group by deptno,
dname
order by deptno;
-- create indexcreateindexreport_sal_per_dept_salon report_sal_per_dept(sal_per_dept desc);
-- refresh materialized view
refresh materialized view report_sal_per_dept;
-- select from materialized viewselect*from report_sal_per_dept
where sal_per_dept <=10000order by sal_per_dept;
-- explain plan
:ea
select*from report_sal_per_dept
where sal_per_dept <=10000order by sal_per_dept;