我有一个具有以下架构的postgres表
Table "public.myTable"
Column | Type | Modifiers
----------- +--------------------------+-----------
serial_number | character varying(255) |
name | character varying(255) |
Designation | character varying(255) |
place | character varying(255) |
timeOfJoining | timestamp with time zone |
timeOfLeaving | timestamp with time zone |
Indexes:
"name_Designation_place" btree (name, Designation, place)
"Designation_place_name" btree (Designation, place, name)
"Designation_name_place" btree (Designation, name, place)
"timeOfJoining_timeOfLeaving" btree (timeOfJoining, timeOfLeaving)
"timeOfJoining_timeOfLeaving" btree (timeOfJoining, timeOfLeaving)
现在我运行表单的查询:
explain analyze select place from myTable where Designation='Manager' and timeOfJoining>'1930-10-10';
我正在制定以下计划:
Index Scan using Designation_place_name on myTable (cost=0.00..67701.36 rows=22043 width=27) (actual time=0.061..3.796 rows=3376 loops=1)
Index Cond: ((relation)::text = 'Manager'::text)
Filter: (timeOfJoining > '1930-10-10 00:00:00+05:53:20'::timestamp with time zone)
Total runtime: 4.082 ms
(4 rows)
现在我无法理解查询计划的执行方式。查询计划是否首先从myTable上的索引Designation_place_name检索serial_number,然后转到myTable并获取行,然后在timeOfJoining上执行过滤
OR
查询计划是否同时获取索引timeOfJoining_timeOfLeaving和Designation_place_name,然后执行连接,并在此连接时完成过滤?
答案 0 :(得分:1)
查询计划是否同时获取索引timeOfJoining_timeOfLeaving和Designation_place_name,然后执行连接,并在此连接时完成过滤?
由于计划中未提及索引timeOfJoining_timeOfLeaving
,因此不使用它。就这么简单。
查询计划是否首先从myTable上的索引Designation_place_name检索serial_number,然后转到myTable并获取行,然后在timeOfJoining上执行过滤?
主要是:是的。但不是使用serial_number
而是内部类型的链接。除了那个小问题,这就是计划告诉你的。
答案 1 :(得分:1)
这个计划:
Index Scan using Designation_place_name on myTable (cost=0.00..67701.36 rows=22043 width=27) (actual time=0.061..3.796 rows=3376 loops=1)
Index Cond: ((relation)::text = 'Manager'::text)
Filter: (timeOfJoining > '1930-10-10 00:00:00+05:53:20'::timestamp with time zone)
Total runtime: 4.082 ms
(4 rows)
基本上意味着:
在步骤2中,磁盘页面是“随机”访问的,而不是按顺序访问,也就是说索引包含磁盘上匹配行的地址,Postgres按索引指示的顺序访问这些地址。 (这可能是昂贵的,顺便说一下。有时,规划人员会决定更便宜的只是读取整个表(seq扫描)或批量获取页面上的所有行,而忽略索引指示的顺序(位图索引扫描)。)< / p>
注意:该查询中没有(表)连接。如果有的话,你会看到额外的缩进级别。从大多数缩进到最小缩进读取它们。