skytf=> /dt+ test_59 List of relations Schema | Name | Type | Owner | Size | Description --------+---------+-------+-------+--------+------------- skytf | test_59 | table | skytf | 769 MB | Time: 24472.248 ms
查看执行计划,如下:
1 2 3 4 5 6 7 8 9
skytf=> explain ( analyze on, buffers on ) select count(*) from test_59; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=223410.00..223410.01 rows=1 width=0) (actual time=11867.036..11867.037 rows=1 loops=1) Buffers: shared hit=85158 read=13252 -> Seq Scan on test_59 (cost=0.00..198410.00 rows=10000000 width=0) (actual time=61.639..5903.459 rows=10000000 loops=1) Buffers: shared hit=85158 read=13252 Total runtime: 11867.089 ms (5 rows)
备注:vaccum后,表大小没变,全表扫描扫描的PAGE数也没变;
场景二:测试走索引的查询
索引查询
1 2 3 4 5 6 7
skytf=> explain ( analyze on, buffers on ) select * from test_59 where id=1; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------- Index Scan using test_59_pkey on test_59 (cost=0.00..5.91 rows=1 width=8) (actual time=0.017..0.020 rows=1 loops=1) Index Cond: (id = 1) Buffers: shared hit=6 Total runtime: 0.048 ms(4 rows)
备注:索引索引只扫描了 6个 page。
批量更新 function
1 2 3 4 5 6 7 8 9 10
CREATEorreplaceFUNCTION fun_update_test_59() RETURNSINTEGERAS $$ DECLARE i INTEGER ; BEGIN for i in1..10000loop update test_59 setname='test'whereid=1; endloop; return 1; END; $$ LANGUAGE 'plpgsql';
skytf=> explain ( analyze on, buffers on ) select * from test_59 where id=1; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------- Index Scan using test_59_pkey on test_59 (cost=0.00..5.91 rows=1 width=8) (actual time=0.036..1.555 rows=1 loops=1) Index Cond: (id = 1) Buffers: shared hit=59 Total runtime: 1.579 ms (4 rows)
skytf=> explain ( analyze on, buffers on ) select * from test_59 where id=1; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------- Index Scan using test_59_pkey on test_59 (cost=0.00..5.91 rows=1 width=8) (actual time=0.020..0.022 rows=1 loops=1) Index Cond: (id = 1) Buffers: shared hit=4 Total runtime: 0.049 ms (4 rows)