11.
11
1.実行プランの強制
プランを強制してみる
sampledb=# explain analyze select * from pgbench_accounts where aid > 1;
QUERY PLAN
------------------------------------------------------------------------
Seq Scan on pgbench_accounts
(cost=0.00..28374.00 rows=1000000 width=97)
(actual time=0.011..224.378 rows=999999 loops=1)
Filter: (aid > 1)
Rows Removed by Filter: 1
Total runtime: 345.456 ms
(4 rows)
sampledb=# SET enable_seqscan = off;
SET
sampledb=# explain analyze select * from pgbench_accounts where aid > 1;
QUERY PLAN
------------------------------------------------------------------------
Index Scan using pgbench_accounts_pkey on pgbench_accounts
(cost=0.42..42169.43 rows=1000000 width=97)
(actual time=0.041..480.360 rows=999999 loops=1)
Index Cond: (aid > 1)
Total runtime: 603.765 ms
(3 rows)
初期状態はSeq Scan
(cost=28374.00)
変更後はIndex Scan
(cost=42169.43)
18.
18
2.EXPLAIN と EXPLAIN ANALYZE
実行SQL:
EXPLAIN ANALYZE
SELECT e.empno,d.dname FROM emp e
JOIN dept d ON e.deptno=d.deptno ;
Column | Type
----------+-----------------------------
empno | integer
ename | character varying(10)
job | character varying(9)
mgr | integer
hiredate | timestamp without time zone
sal | integer
comm | integer
deptno | integer
EMP表
Column | Type
--------+-----------------------
deptno | integer
dname | character varying(14)
loc | character varying(13)
DEPT表
19.
19
2.EXPLAIN と EXPLAIN ANALYZE
実行結果
Hash Join
(cost=1.09..2.32 rows=4 width=50)
(actual time=0.100..0.120 rows=14 loops=1)
Hash Cond:(e.deptno = d.deptno)
-> Seq Scan on emp e
(cost=0.00..1.14 rows=14 width=8)
(actual time=0.013..0.022 rows=14 loops=1)
-> Hash
(cost=1.04..1.04 rows=4 width=50)
(actual time=0.024..0.024 rows=4 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 1kB
-> Seq Scan on dept d
(cost=0.00..1.04 rows=4 width=50)
(actual time=0.011..0.015 rows=4 loops=1)
①
②
③
④
20.
20
2.EXPLAIN と EXPLAIN ANALYZE
実行結果をツリーにすると…
Seq Scan on emp e
cost=0.00..1.14
time=0.013..0.022
Hash Join
cost=1.09..2.32
time=0.100..0.120
Hash
cost=1.04..1.04
time=0.024..0.024
Seq Scan on dept d
cost=0.00..1.04
time=0.011..0.015
cost=1.04+0
time=0.015+0.009
cost=1.04+1.14+0.5
time=0.024+0.022+0.074
21.
21
2.EXPLAIN と EXPLAIN ANALYZE
EXPLAIN ANALYZE
SELECT e.empno,d.dname,s.grade FROM
emp e JOIN dept d ON e.deptno=d.deptno
JOIN salgrade s on e.sal between s.losal and s.hisal
where e.job='SALESMAN';
Column | Type
----------+-----------------------------
empno | integer
ename | character varying(10)
job | character varying(9)
mgr | integer
hiredate | timestamp without time zone
sal | integer
comm | integer
deptno | integer
EMP表
Column | Type
--------+-----------------------
deptno | integer
dname | character varying(14)
loc | character varying(13)
DEPT表
Column | Type
--------+---------
grade | integer
losal | integer
hisal | integer
SALGRADE表
22.
22
2.EXPLAIN と EXPLAIN ANALYZE
もうちょっと複雑な出力結果
Nested Loop
(cost=0.00..3.39 rows=1 width=50)
(actual time=0.031..0.089 rows=4 loops=1)
Join Filter: ((emp.sal >= s.losal) AND (emp.sal <= s.hisal))
Rows Removed by Join Filter: 16
-> Nested Loop
(cost=0.00..2.26 rows=1 width=54)
(actual time=0.022..0.051 rows=4 loops=1)
Join Filter: (emp.deptno = d.deptno)
Rows Removed by Join Filter: 12
-> Seq Scan on emp
(cost=0.00..1.18 rows=1 width=12)
(actual time=0.011..0.018 rows=4 loops=1)
Filter: ((job)::text = 'SALESMAN'::text)
Rows Removed by Filter: 10
-> Seq Scan on dept d
(cost=0.00..1.04 rows=4 width=50)
(actual time=0.001..0.003 rows=4 loops=4)
-> Seq Scan on salgrade s
(cost=0.00..1.05 rows=5 width=8)
(actual time=0.001..0.002 rows=5 loops=4)
①
②
③
④
⑤
23.
23
2.EXPLAIN と EXPLAIN ANALYZEの違い
実行結果をツリーにすると…
Nested Loop
Nested Loop
Seq Scan on emp
Seq Scan on dept d
Seq Scan on dept d x 4
Seq Scan on salgrade s
x 4
Seq Scan on salgrade s
(cost=0.00..1.05 rows=5 width=8)
(actual time=0.001..0.002 rows=5 loops=4)