skytf=> explain analyze select id from test_indexonly where id=1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Index Scan using test_indexonly_pkey on test_indexonly (cost=0.00..8.27 rows=1 width=4) (actual time=0.089..0.091 rows=1 loops=1) Index Cond: (id = 1) Total runtime: 0.173 ms (3 rows) skytf=> explain analyze select id from test_indexonly where id=1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Index Scan using test_indexonly_pkey on test_indexonly (cost=0.00..8.27 rows=1 width=4) (actual time=0.022..0.026 rows=1 loops=1) Index Cond: (id = 1) Total runtime: 0.068 ms (3 rows) skytf=> explain analyze select id from test_indexonly where id=1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Index Scan using test_indexonly_pkey on test_indexonly (cost=0.00..8.27 rows=1 width=4) (actual time=0.057..0.060 rows=1 loops=1) Index Cond: (id = 1) Total runtime: 0.204 ms (3 rows)
1.4 测试扫描索引范围的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
skytf=> explain analyze select count(*) from test_indexonly where id>300 and id <401; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=10.53..10.54 rows=1 width=0) (actual time=0.366..0.367 rows=1 loops=1) -> Index Scan using test_indexonly_pkey on test_indexonly (cost=0.00..10.27 rows=101 width=0) (actual time=0.041..0.220 rows=100 loops=1) Index Cond: ((id > 300) AND (id < 401)) Total runtime: 0.462 ms (4 rows) skytf=> explain analyze select count(*) from test_indexonly where id>300 and id <401; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=10.53..10.54 rows=1 width=0) (actual time=0.357..0.358 rows=1 loops=1) -> Index Scan using test_indexonly_pkey on test_indexonly (cost=0.00..10.27 rows=101 width=0) (actual time=0.035..0.201 rows=10 0 loops=1) Index Cond: ((id > 300) AND (id < 401)) Total runtime: 0.421 ms (4 rows)
测试: PostgreSQL 9.2 Beta1
2.1 查询版本
1 2 3 4 5
francs=> select version(); version ---------------------------------------------------------------------------------------------------------- PostgreSQL 9.2beta1 on i686-pc-linux-gnu, compiled by gcc (GCC) 4.4.620110731 (Red Hat 4.4.6-3), 32-bit (1 row)
francs=> explain analyze select id from test_indexonly where id=1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Index Only Scan using test_indexonly_pkey on test_indexonly (cost=0.00..8.28 rows=1 width=4) (actual time=0.068..0.071 rows=1 loops=1) Index Cond: (id = 1) Heap Fetches: 1 Total runtime: 0.142 ms(4 rows)
francs=> explain analyze select id from test_indexonly where id=1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------- Index Only Scan using test_indexonly_pkey on test_indexonly (cost=0.00..8.28 rows=1 width=4) (actual time=0.016..0.019 rows=1 loops=1) Index Cond: (id = 1) Heap Fetches: 1 Total runtime: 0.049 ms (4 rows) francs=> explain analyze select id from test_indexonly where id=1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Index Only Scan using test_indexonly_pkey on test_indexonly (cost=0.00..8.28 rows=1 width=4) (actual time=0.027..0.030 rows=1 loops=1) Index Cond: (id = 1) Heap Fetches: 1 Total runtime: 0.085 ms (4 rows)
francs=> explain analyze select count(*) from test_indexonly where id>300 and id <401; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=10.53..10.54 rows=1 width=0) (actual time=0.451..0.452 rows=1 loops=1) -> Index Only Scan using test_indexonly_pkey on test_indexonly (cost=0.00..10.28 rows=101 width=0) (actual time=0.086..0.275 rows=100 loops=1) Index Cond: ((id > 300) AND (id < 401)) Heap Fetches: 100 Total runtime: 0.579 ms (5 rows) francs=> explain analyze select count(*) from test_indexonly where id>300 and id <401; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=10.53..10.54 rows=1 width=0) (actual time=0.518..0.520 rows=1 loops=1) -> Index Only Scan using test_indexonly_pkey on test_indexonly (cost=0.00..10.28 rows=101 width=0) (actual time=0.167..0.353 rows=100 loops=1) Index Cond: ((id > 300) AND (id < 401)) Heap Fetches: 100 Total runtime: 0.583 ms(5 rows)
总结
3.1 测试场景一:单条记录索引扫描
单条记录索引扫描
PostgreSQL 9.0
PostgreSQL 9.2
第一次查询
0.173 ms
0.142 ms
第二次查询
0.068 ms
0.049 ms
第三次查询
0.204 ms
0.085 ms
3.2 测试场景二:多条记录索引范围扫描
多条记录索引范围扫描
PostgreSQL 9.0
PostgreSQL 9.2
第一次查询
0.462 ms
0.579 ms
第二次查询
0.421 ms
0.583 ms
“Index Only Scan “ 和 “Index Scan using “ 的性能在单条检索,和范围检索上性能没有明显区别。