问题
I get two answers: 1, 2. But I still do not know which is efficient. Here is explain
of both:
explain for my
query:
Hash Anti Join (cost=136.09..138.49 rows=1 width=556)
Hash Cond: (t1.id = t2.parent_id)
CTE tree
-> Recursive Union (cost=0.14..132.81 rows=101 width=556)
-> Index Scan using resource_type_idx_parent_resource_type_id on resource_type (cost=0
Index Cond: (parent_resource_type_id IS NULL)
-> Hash Join (cost=0.33..12.26 rows=10 width=556)
Hash Cond: (rt.parent_resource_type_id = tree.id)
-> Seq Scan on resource_type rt (cost=0.00..11.30 rows=130 width=524)
-> Hash (cost=0.20..0.20 rows=10 width=36)
-> WorkTable Scan on tree (cost=0.00..0.20 rows=10 width=36)
-> CTE Scan on tree t1 (cost=0.00..2.02 rows=101 width=556)
-> Hash (cost=2.02..2.02 rows=101 width=4)
-> CTE Scan on tree t2 (cost=0.00..2.02 rows=101 width=4)
Laurenz Albe
answer:
Nested Loop Anti Join (cost=132.81..388.39 rows=100 width=556)
Join Filter: (t2.deep_name ~~ (t1.deep_name || '.%'::text))
CTE tree
-> Recursive Union (cost=0.14..132.81 rows=101 width=556)
-> Index Scan using resource_type_idx_parent_resource_type_id on resource_type (cost=0
Index Cond: (parent_resource_type_id IS NULL)
-> Hash Join (cost=0.33..12.26 rows=10 width=556)
Hash Cond: (rt.parent_resource_type_id = tree.id)
-> Seq Scan on resource_type rt (cost=0.00..11.30 rows=130 width=524)
-> Hash (cost=0.20..0.20 rows=10 width=36)
-> WorkTable Scan on tree (cost=0.00..0.20 rows=10 width=36)
-> CTE Scan on tree t1 (cost=0.00..2.02 rows=101 width=556)
-> CTE Scan on tree t2 (cost=0.00..2.02 rows=101 width=32)
The diff beetwen them:
--- CTE Scan on tree (cost=132.81..134.83 rows=101 w (Selection)
+++ (clipboard)
@@ -1,5 +1,5 @@
- Hash Anti Join (cost=136.09..138.49 rows=1 width=556)
- Hash Cond: (t1.id = t2.parent_id)
+ Nested Loop Anti Join (cost=132.81..388.39 rows=100 width=556)
+ Join Filter: (t2.deep_name ~~ (t1.deep_name || '.%'::text))
CTE tree
-> Recursive Union (cost=0.14..132.81 rows=101 width=556)
-> Index Scan using resource_type_idx_parent_resource_type_id on resource_type (cost=0
@@ -10,5 +10,4 @@
-> Hash (cost=0.20..0.20 rows=10 width=36)
-> WorkTable Scan on tree (cost=0.00..0.20 rows=10 width=36)
-> CTE Scan on tree t1 (cost=0.00..2.02 rows=101 width=556)
- -> Hash (cost=2.02..2.02 rows=101 width=4)
- -> CTE Scan on tree t2 (cost=0.00..2.02 rows=101 width=4)
+ -> CTE Scan on tree t2 (cost=0.00..2.02 rows=101 width=32)
Which one of them is faster? how to know that from explain results?
来源:https://stackoverflow.com/questions/51964031/which-query-is-faster