-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6. Performance Optimization with Joins (Advanced).HQL
193 lines (134 loc) · 4.37 KB
/
6. Performance Optimization with Joins (Advanced).HQL
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
a. Load the demographics dataset into another Hive table.
CREATE TABLE demographic_data (
customerID STRING,
City STRING,
Lat DECIMAL(9, 6),
Long DECIMAL(9, 6),
country STRING,
iso2 STRING,
State STRING
);
load data local inpath 'file:///home/aeronautbhushan/CustomerDemographics.csv' into table demographic_data;
b. Write HiveQL queries to join the customer churn table and the
demographics table on customerID using different types of joins -
common join, map join, bucket map join, and sorted merge bucket join.
-- Common Join
SELECT *
FROM telecom_data AS td
JOIN demographic_data AS d
ON td.customerID = d.customerID;
-- map Join :
Map join is an optimization technique in Hive where one of the tables is loaded into memory as a hashmap and the other table is streamed through to perform the join. Map join is automatically enabled by Hive when the smaller table can fit in memory.
SET hive.auto.convert.join = true;
SET hive.mapjoin.smalltable.filesize = 25000000; -- Specify the threshold for small tables
SELECT *
FROM telecom_data AS td
JOIN demographic_data AS d
ON td.customerID = d.customerID;
(((
note:
bigger table should be 1st then smaller once
)))
--bucket map join:
Map join is an optimization technique in Hive where one of the tables is loaded into memory as a hashmap and the other table is streamed through to perform the join. Map join is automatically enabled by Hive when the smaller table can fit in memory.
(((note: for bucketed join both table should be bucketed)))
step 1 : make both table bucketed
--telecom_data bucket table
CREATE TABLE telecom_data_bucketed_1 (
customerID STRING,
gender STRING,
SeniorCitizen INT,
Partner STRING,
Dependents STRING,
tenure INT,
PhoneService STRING,
MultipleLines STRING,
InternetService STRING,
OnlineSecurity STRING,
OnlineBackup STRING,
DeviceProtection STRING,
TechSupport STRING,
StreamingTV STRING,
StreamingMovies STRING,
Contract STRING,
PaperlessBilling STRING,
PaymentMethod STRING,
MonthlyCharges FLOAT,
TotalCharges FLOAT,
Churn STRING
)
clustered by (customerID) into 4 buckets;
Load the data from the original table into the bucketed table.
insert overwrite table telecom_data_bucketed_1 select* from telecom_data
--demographic_data
CREATE TABLE demographic_data_bucketed (
customerID STRING,
City STRING,
Lat DECIMAL(9, 6),
Long DECIMAL(9, 6),
country STRING,
iso2 STRING,
State STRING
)
clustered by (customerID) into 4 buckets;
Load the data from the original table into the bucketed table.
insert overwrite table demographic_data_bucketed select* from demographic_data
step 2:
join both table
-- Enable bucket map join optimization :
SET hive.auto.convert.join = true;
SELECT *
FROM telecom_data_bucketed_1 AS td
JOIN demographic_data_bucketed AS dd
ON td.customerID = dd.customerID;
--Sorted Merge Bucket Join:
Sorted merge bucket join is another optimization technique that requires both tables to be bucketed and sorted on the join column. This type of join can be very efficient for certain scenarios.
step 1 : make both table bucketed
--telecom_data bucket table
CREATE TABLE telecom_data_bucketed_2 (
customerID STRING,
gender STRING,
SeniorCitizen INT,
Partner STRING,
Dependents STRING,
tenure INT,
PhoneService STRING,
MultipleLines STRING,
InternetService STRING,
OnlineSecurity STRING,
OnlineBackup STRING,
DeviceProtection STRING,
TechSupport STRING,
StreamingTV STRING,
StreamingMovies STRING,
Contract STRING,
PaperlessBilling STRING,
PaymentMethod STRING,
MonthlyCharges FLOAT,
TotalCharges FLOAT,
Churn STRING
)
clustered by (customerID) SORTED BY (customerID) into 4 buckets;
Load the data from the original table into the bucketed table.
insert overwrite table telecom_data_bucketed_2 select* from telecom_data
--demographic_data
CREATE TABLE demographic_data_bucketed_2 (
customerID STRING,
City STRING,
Lat DECIMAL(9, 6),
Long DECIMAL(9, 6),
country STRING,
iso2 STRING,
State STRING
)
clustered by (customerID) SORTED BY (customerID) into 4 buckets;
Load the data from the original table into the bucketed table.
insert overwrite table demographic_data_bucketed_2 select* from demographic_data;
step 2:
join both table
-- Enable sorted merge bucket join optimization
SET hive.auto.convert.join = true;
SELECT *
FROM telecom_data_bucketed_2 AS td
JOIN demographic_data_bucketed_2 AS dd
ON td.customerID = dd.customerID;