- Mastering PostgreSQL 12
- Hans Jürgen Sch?nig
- 278字
- 2021-08-20 10:00:27
Making use of index-only scans
So far, you have seen when an index is used and when it is not. In addition to this, 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 an 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 in an index here and there to enjoy the benefit of this feature. In MS SQL, adding additional columns is known as covering indexes. Since PostgreSQL 11, we have the same functionality, which uses the INCLUDE keyword in CREATE INDEX.
- 大學(xué)計(jì)算機(jī)基礎(chǔ):基礎(chǔ)理論篇
- 大數(shù)據(jù)項(xiàng)目管理:從規(guī)劃到實(shí)現(xiàn)
- 計(jì)算機(jī)圖形學(xué)
- 數(shù)據(jù)中心建設(shè)與管理指南
- Hands-On Data Science with SQL Server 2017
- 圖形圖像處理(Photoshop)
- ROS機(jī)器人編程與SLAM算法解析指南
- 自主研拋機(jī)器人技術(shù)
- 完全掌握AutoCAD 2008中文版:綜合篇
- 愛犯錯的智能體
- 空間機(jī)器人
- Linux常用命令簡明手冊
- Learning iOS 8 for Enterprise
- 天才與算法:人腦與AI的數(shù)學(xué)思維
- Getting Started with Tableau 2019.2