os: centos 7.4
db: postgresql 10.11
Nested Loop 比較適合兩個表的數據量都比較少的情況(最簡單的 table join 方式)
- 外層驅動表爲小表,且過濾後的數據量較少。
- 內層被驅動表的關聯列上最好有高效索引(主鍵或者唯一性索引)。
版本
# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
#
# su - postgres
$
$ psql -c "select version();"
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 10.11 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)
create table
$ psql
psql (10.11)
Type "help" for help.
postgres=#
postgres=# drop table if exists tmp_t4;
drop table if exists tmp_t5;
postgres=# create table tmp_t4(
id int8 primary key,
name varchar(100)
);
create table tmp_t5(
id int8 primary key,
name varchar(100)
);
postgres=# insert into tmp_t4
select id,
md5(id::varchar)
from generate_series(1,1000000) as id;
insert into tmp_t5
select id,
md5(id::varchar)
from generate_series(1,1000000) as id;
postgres=# vacuum analyze tmp_t4;
vacuum analyze tmp_t5;
Nested Loop
postgres=# set max_parallel_workers_per_gather=0;
postgres=# explain analyze
select t4.*,t5.*
from tmp_t4 t4,
tmp_t5 t5
where 1=1
and t4.id = t5.id
and t4.id = 499999
;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost=0.85..16.90 rows=1 width=82) (actual time=0.015..0.016 rows=1 loops=1)
-> Index Scan using tmp_t4_pkey on tmp_t4 t4 (cost=0.42..8.44 rows=1 width=41) (actual time=0.008..0.008 rows=1 loops=1)
Index Cond: (id = 499999)
-> Index Scan using tmp_t5_pkey on tmp_t5 t5 (cost=0.42..8.44 rows=1 width=41) (actual time=0.006..0.007 rows=1 loops=1)
Index Cond: (id = 499999)
Planning time: 0.079 ms
Execution time: 0.032 ms
(7 rows)
執行計劃顯示: tmp_t4 作爲驅動表 通過 Nested Loop 方式 探測 tmp_t5 數據,類似 for 循環
for(t4.data in tmp_t4)
{
t5.data in tmp_t5 on t5.data = t4.data
}
參考: