-
Notifications
You must be signed in to change notification settings - Fork 551
/
Copy path03.Tuning_Joins.sql
77 lines (43 loc) · 1.71 KB
/
03.Tuning_Joins.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/***************** Tuning Joins *******************/
--------------- Nested Loop --------------------------------------------------
-- force query plan builder to use nested loop first, just for testing purpose
set enable_nestloop = true;
set enable_hashjoin = false;
set enable_mergejoin = false;
EXPLAIN SELECT s.id,s.last_name,s.job_title,cr.country
FROM staff s
JOIN company_regions cr
ON cr.region_id = s.region_id;
/*
We can see that builder use Nested Lop then Index Scan using primary key (region_id).
As postgreSQL automatically created index on primary key, we will delete this key (for testing purpose)
and see how Nest Loop will full table scan will look like in performance wise.
*/
EXPLAIN SELECT s.id,s.last_name,s.job_title,cr.country
FROM staff s
JOIN company_regions cr
ON cr.region_id = s.region_id;
/* we can see that Cost got increased.
So main take away is when we are using any kind of joins (especially, Nested Loop), it helps to ensure foreign keys
columns and the columns you are trying to matching on are indexed properly.
*/
--------------- Hash Join --------------------------------------------------
set enable_nestloop = false;
set enable_hashjoin = true;
set enable_mergejoin = false;
EXPLAIN SELECT s.id,s.last_name,s.job_title,cr.country
FROM staff s
JOIN company_regions cr
ON cr.region_id = s.region_id;
/*
We can see that in row 3, scanning on staff table.
in row 4, hash table is being buit.
*/
--------------- Merge Join --------------------------------------------------
set enable_nestloop = false;
set enable_hashjoin = false;
set enable_mergejoin = true;
EXPLAIN SELECT s.id,s.last_name,s.job_title,cr.country
FROM staff s
JOIN company_regions cr
ON cr.region_id = s.region_id;