sqlチューニング入門 入門編

Download SQLチューニング入門 入門編

Post on 22-Jul-2015

1.085 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

PostgreSQL SQL

PostgreSQL SQL PostgreSQL20142014125TIS11TIS2014TISPostgreSQLPostgreSQLJPUG(http://www.postgresql.jp/wg/shikumi/study30_materials)PGEConsPostgreSQL2PostgreSQL SQL PostgreSQLSQLSQLExplain

PostgreSQLSQLExplain()Explain

3SQLSQLPostgreSQL4PostgreSQLSQL

DBSQL

5

Explain SQL (parse tree)

(query tree) OID (analyzer)

PostgreSQL VIEW RULE (rewriter)

(plan tree)

(executor) 5SQLSELECT oid FROM pg_proc WHERE oid = 1;SELECTTargetListoidRelationListpg_procQualifierExpression=1oid6SQL 1SELECT customer_name, balance FROM customers WHERE balance > 0 ORDER BY balance 6PostgreSQLSQL

DBSQL

7

Explain SQL (parse tree)

(query tree) OID (analyzer)

PostgreSQL VIEW RULE (rewriter)

(plan tree)

(executor) 7DBOIDSELECT oid FROM pg_proc WHERE oid = 1;SELECTTargetListoidRelationListpg_procQualifierExpression=1oidoid8 (analyzer) pg_classpg_operater*

8PostgreSQLSQL

DBSQL

9

Explain SQL (parse tree)

(query tree) OID (analyzer)

PostgreSQL VIEW RULE (rewriter)

(plan tree)

(executor) 9PostgreSQLVIEWRULESQL

10 (rewriter)

10PostgreSQLSQL

DBSQL

11

Explain SQL (parse tree)

(query tree) OID (analyzer)

PostgreSQL VIEW RULE (rewriter)

(plan tree)

(executor) 11SQL()()1212

Explain

1313PostgreSQLSQL

DBSQL

14

Explain SQL (parse tree)

(query tree) OID (analyzer)

PostgreSQL VIEW RULE (rewriter)

(plan tree)

(executor) 14

151516SQLPostgreSQLSQLSQLPostgreSQLPostgreSQLExplain17ExplainSQL17ExplainSQLDB(SQL)SQLSQL()PostgreSQLPostgreSQLExplain18Explain18Explain19Explain Plan=# EXPLAIN SELECT * FROM pg_proc ORDER BY proname;

QUERY PLAN----------------------------------------------------Sort (cost=181.55..185.92 rows=1747 width=322) Sort Key: proname -> Seq Scan on pg_proc (cost=0.00..87.47 rows=1747 width=322)202021Explaining Cost2: () ()I/O1 1.0 =# EXPLAIN SELECT oid FROM pg_proc; QUERY PLAN------------------------------------------ Seq Scan on pg_proc (cost=0.00..87.47 rows=1747 width=4)2111 1.022cost=0.00..87.47

22Explaining Costseq_page_cost11.00()random_page_cost14.004cpu_tuple_cost10.01100cpu_index_tuple_cost10.005200cpu_operator_cost10.0025400effective_cache_size128MBN/A2323I/O3CPUeffective_cache_sizePostgreSQLOS

24Explaining RowsPostgreSQL 8.0VACUUM/ANALYZE1000vacuum analyze=# EXPLAIN SELECT oid FROM pg_proc; QUERY PLAN------------------------------------------ Seq Scan on pg_proc (cost=0.00..87.47 rows=1747 width=4)24PostgreSQLupdateupdatevacuum25Explaining Widths=# EXPLAIN SELECT oid FROM pg_proc; QUERY PLAN------------------------------------------ Seq Scan on pg_proc (cost=0.00..87.47 rows=1747 width=4)smallint2integer4bigint8boolean1char(n)n+1n+4varchar(n)text [ n ]25

262627Explaining Explain AnalyzeSQLloops(time)=# EXPLAIN ANALYZE SELECT oid FROM pg_proc; QUERY PLAN------------------------------------------ Seq Scan on pg_proc (cost=0.00..87.47 rows=1747 width=4) (actual time=0.077..17.082 rows=1747 loops=1) Total runtime: 20.125 ms27*actual time actual timeloops*rowsloops*loops*total runtime

Explain28ExplainSQLPostgreSQL(work_mem)SQL2929Seq scanIndex scanBitmap scanIndex only scanTid scanID(ctid)Function scangatherNested LoopMerge JoinHash Join30limitLIMIT,OFFSETUniqueUNIQUEAggregate(count,sum,,,)GroupAggregateGROUP BYResultAppendUNION()SetOpINTERSECT(),EXCEPT()Sort3120Explaing Explain 2http://www.postgresql.jp/wg/shikumi/sikumi_20/3132Seq ScanIndex ScanBitmap ScanIndex Only ScanTid ScanFunction ScanNested LoopMerge JoinHash JoinlimitUniqueAggregateGroupAggregateResultAppendSetOpSort33Seq Scan (), (), (oid)92.12=# EXPLAIN SELECT oid FROM pg_proc WHERE oid = 1; QUERY PLAN-------------------------------------------------- Seq Scan on pg_proc (cost=0.00..92.12 rows=1 width=4) Filter: (oid = 1::oid)3334Seq Scan Seq Scan

id = 1id = 11id = 34id = 45idSeq Scan = DISK I/O + CPU = sequential_page_cost + cpu_tuple_cost + cpu_operator_cost pg_class relpages pg_calss reltuples DISK1 sequential_page_cost = 1 1CPU cpu_tuple_cost = 0.01 1CPU () cpu_operator_cost = 0.002534Seq ScanSeq Scan=(DISK I/O)(CPU) (Seq_page_cost) ( cpu_tuple_cost ) (cpu_operator_cost)

35=# SELECT relpages,reltuples FROM pg_class WHERE relname = 'pg_proc'; relpages | reltuples----------+----------- 61 | 2490=# EXPLAIN SELECT oid FROM pg_proc WHERE oid = 1; QUERY PLAN-------------------------------------------------- Seq Scan on pg_proc (cost=0.00..92.12 rows=1 width=4) Filter: (oid = 1::oid) = (611.0) + (24900.01) + (24900.0025) = 92.125 0.0025pg_class = (61 1.0) + { (2490 0.01) + (2490 0.0025) } = 92.125 reltuples Cpu_operator_costWhere

3536Index Scan Index Cond=# EXPLAIN SELECT oid FROM pg_proc WHERE oid=1; QUERY PLAN----------------------------------------------------- Index Scan using pg_proc_oid_index on pg_proc (cost=0.00..5.99 rows=1 width=4) Index Cond: (oid = 1::oid)36seq scan37Index Scan id = 1id = 11id = 34id = 4545100idIndex Scan = I/O + I/O+ CPU + CPU I/O= sequential_page_cost1 I/O= (14) CPU= cpu_index_tuple_cost0.005CPU= cpu_tuple_cost0.01(effective_cache_size)37IndexIndextable

38Explain rows=100 =

pg_statspg_statistic ANALYZE

3839Bitmap Scan 8.1BitmapOr, BitmapAnd

3940Bitmap Scan OR

id1 = 1, id2 = 33id1 = 11, id2 = 55 id1 = 34, id2 = 77id1 = 45, id2 = 993445id1ON33557799id2WHERE (id1 BETWEEN 10 AND 40) OR (id2 BETWEEN 20 AND 70) 01101100Bitmap Index Scan1110BitmapOrTIDORBitmap Heap ScanI/O

40Tid1bitTid1

index scanIndex Only Scan9.2(index only scan)

41Index Only ScanIndex Only ScanVisibility Map()Visibility Map

Visibility Map

42Visibility Map1bit&&01

43121234143Visibility Mapbit0

44

1234!

3445SELECT id FROM table1 WHERE id BETWEEN 1 AND 11

SELECT * FROM table144Visibility Mapbit1

45

1234

3445SELECT id FROM table1 WHERE id BETWEEN 1 AND 111145vacuum

462ViisibilityMap

2

1345VACUUMVACUUM2ViisibilityMap

21345

VACUUM4647Tid Scan IDctid==# EXPLAIN SELECT oid FROM pg_proc WHERE ctid ='(0,1)'; QUERY PLAN------------------------------------------------------Tid Scan on pg_proc (cost=0.00..4.01 rows=1 width=4) Filter: (ctid = '(0,1)'::tid)47Octid

ID

48Seq ScanIndex ScanBitmap ScanIndex Only ScanTid ScanFunction ScanNested LoopMerge JoinHash JoinlimitUniqueAggregateGroupAggregateResultAppendSetOpSort49Sort : ORDER BY : Unique, Sort-Merge Join : =# EXPLAIN SELECT oid FROM pg_proc ORDER BY oid; QUERY PLAN---------------------------------------------Sort (cost=181.55..185.92 rows=1747 width=4) Sort Key: oid-> Seq Scan on pg_proc (cost=0.00..87.47 rows=1747 width=4)49

50Sort work_memDISK I/O DISK I/OI/O work_mem work_mem /src/backend/optimizer/path/costsize.c cost_sort

7492DISK 74922479247981351358DISKDISK < work_memwork_mem > work_memwork_mem DISK5051Sort# EXPLAIN ANALYZE SELECT * FROM pgbench_accounts ORDER BY bid;-------------------------------------------------------------------------------------------------------------------------- Sort (cost=290114.34..292614.34 rows=1000000 width=97) (actual time=882.522..1186.626 rows=1000000 loops=1) Sort Key: bid Sort Method: external sort Disk: 104600kB -> Seq Scan on pgbench_accounts (cost=0.00..26394.00 rows=1000000 width=97) (actual time=0.023..176.540 rows=1000000 loops=1) Total runtime: 1264.377 msLOG: temporary file: path "base/pgsql_tmp/pgsql_tmp32001.0", size 107110400STATEMENT: EXPLAIN ANALYZE SELECT * FROM pgbench_accounts ORDER BY bid; postgresql.conf log_temp_files = 0# SET work_mem=200MB;# explain analyze select * from pgbench_accounts order by bid;--------------------------------------------------------------------------------------------------------------------------Sort (cost=126051.84..128551.84 rows=1000000 width=97)(actual time=472.334..563.570 rows=1000000 loops=1) Sort Key: bid Sort Method: quicksort Memory: 165202kB -> Seq Scan on pgbench_accounts (cost=0.00..26394.00 rows=1000000 width=97)(actual time=0.030..166.788 rows=1000000 loops=1) Total runtime: 634.155 ms work_mem 1200MB5152Seq ScanIndex ScanBitmap ScanIndex Only ScanTid ScanFunction ScanNested LoopMerge JoinHash JoinlimitUniqueAggregateGroupAggregateResultAppendSetOpSort53Nested Loop ()INNER JOIN LEFT OUTER JOIN select=# EXPLAIN SELECT * FROM pgbench_accounts AS a,pgbench_accounts AS b; QUERY PLAN--------------------------------------------------------------- Nested Loop (cost=0.00..27637054248.00 rows=1000000000000 width=194) -> Seq Scan on pgbench_accounts a (cost=0.00..25874.00 rows=1000000 width=97) -> Materialize (cost=0.00..46011.00 rows=1000000 width=97) -> Seq Scan on pgbench_accounts b (cost=0.00..25874.00 rows=1000000 width=97)53Nested loop0(O(MN))

5454Merge Join # EXPLAIN SELECT * FROM pgbench_accounts AS a,pgbench_tellers AS t where a.aid = t.tid; QUERY PLAN--------------------------------------------------------------- Merge Join (cost=5.94..11.25 rows=100 width=449) Merge Cond: (a.aid = t.tid) -> Index Scan using pgbench_accounts_pkey onpgbench_accounts a (cost=0.42..39669.43 rows=1000000 width=97) -> Sort (cost=5.32..5.57 rows=100 width=352