- Mastering PostgreSQL 9.6
- Hans Jurgen Schonig
- 272字
- 2021-07-09 19:57:16
Making use of index only scans
So far, you have seen when an index is used and when it is not. In addition to that, bitmap scans have been discussed.
However, there is more to indexing. The following two examples will only differ slightly although the performance difference might be fairly large. Here is the first query:
test=# EXPLAIN SELECT * FROM t_test WHERE id = 34234;
QUERY PLAN
----------------------------------------------------------------
Index Scan using idx_id on t_test
(cost=0.43..8.45 rows=1 width=9)
Index Cond: (id = 34234)
There is nothing unusual here. PostgreSQL uses an index to find a single row. What happens if only a single column is selected?
test=# EXPLAIN SELECT id FROM t_test WHERE id = 34234;
QUERY PLAN
----------------------------------------------------------------
Index Only Scan using idx_id on t_test
(cost=0.43..8.45 rows=1 width=4)
Index Cond: (id = 34234)
(2 rows)
As you can see, the plan has changed from an index scan to a so called index only scan. In our example, the id column has been indexed so its content is naturally in the index. There is no need to go to the table in most cases if all the data can already be taken out of the index. Going to the table is (almost) only required if additional fields are queried, which is not the case here. Therefore, the index-only scan will promise significantly better performance than a normal index scan.
Practically, it can even make sense to include an additional column into an index here and there to enjoy the benefit of this feature. In MS SQL, adding additional columns is known as covering indexes. Similar behavior can be achieved in PostgreSQL as well.
- Excel 2007函數(shù)與公式自學(xué)寶典
- 機器人智能運動規(guī)劃技術(shù)
- 網(wǎng)絡(luò)綜合布線技術(shù)
- 自動生產(chǎn)線的拆裝與調(diào)試
- Blender Compositing and Post Processing
- 網(wǎng)絡(luò)安全與防護
- Prometheus監(jiān)控實戰(zhàn)
- Grome Terrain Modeling with Ogre3D,UDK,and Unity3D
- Mastering Game Development with Unreal Engine 4(Second Edition)
- RedHat Linux用戶基礎(chǔ)
- 軟件工程及實踐
- Web璀璨:Silverlight應(yīng)用技術(shù)完全指南
- Linux Shell Scripting Cookbook(Third Edition)
- Machine Learning with Spark(Second Edition)
- Serverless Design Patterns and Best Practices