-
Notifications
You must be signed in to change notification settings - Fork 4
/
PGUtils.py
246 lines (228 loc) · 9.83 KB
/
PGUtils.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Copyright 2018-2021 Xiang Yu(x-yu17(at)mails.tsinghua.edu.cn)
#
# Licensed under the Apache License, Version 2.0 (the "License"): you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import psycopg2
import json
from math import log
from ImportantConfig import Config
class PGConfig:
def __init__(self):
self.keepExecutedPlan =True
self.maxTimes = 5
self.maxTime = 300000
LatencyDict = {}
selectivityDict = {}
LatencyRecordFileHandle = None
config = Config()
class PGGRunner:
def __init__(self,dbname = '',user = '',password = '',host = '',port = '',isCostTraining = True,latencyRecord = True,latencyRecordFile = "RecordFile.json"):
"""
:param dbname:
:param user:
:param password:
:param host:
:param port:
:param latencyRecord:-1:loadFromFile
:param latencyRecordFile:
"""
self.con = psycopg2.connect(database=dbname, user=user,
password=password, host=host, port=port)
self.cur = self.con.cursor()
self.config = PGConfig()
self.isLatencyRecord = latencyRecord
global LatencyRecordFileHandle
self.isCostTraining = isCostTraining
if config.enable_mergejoin:
self.cur.execute("set enable_mergejoin = true")
else:
self.cur.execute("set enable_mergejoin = false")
if config.enable_hashjoin:
self.cur.execute("set enable_hashjoin = true")
else:
self.cur.execute("set enable_hashjoin = false")
# self.cur.execute("set enable_hashjoin = false")
if latencyRecord:
LatencyRecordFileHandle = self.generateLatencyPool(latencyRecordFile)
def generateLatencyPool(self,fileName):
"""
:param fileName:
:return:
"""
import os
import json
if os.path.exists(fileName):
f = open(fileName,"r")
lines = f.readlines()
for line in lines:
data = json.loads(line)
global LatencyDict
LatencyDict[data[0]] = data[1]
f = open(fileName,"a")
else:
f = open(fileName,"w")
return f
def getLatency(self, sql,sqlwithplan):
"""
:param sql:a sqlSample object.
:return: the latency of sql
"""
if self.isCostTraining:
return self.getCost(sql,sqlwithplan)
if sql.useCost:
return self.getCost(sql,sqlwithplan)
global LatencyDict
if self.isLatencyRecord:
if sqlwithplan in LatencyDict:
return LatencyDict[sqlwithplan]
thisQueryCost = self.getCost(sql,sqlwithplan)
if thisQueryCost / sql.getDPCost()<1000000000:
try:
self.cur.execute("SET statement_timeout = "+str(int(sql.timeout()))+ ";")
self.cur.execute("set max_parallel_workers = "+str(config.max_parallel_workers)+";")
self.cur.execute("set max_parallel_workers_per_gather = "+str(config.max_parallel_workers_per_gather)+";")
if config.use_hint and sqlwithplan.find("/*")>-1:
self.cur.execute("set join_collapse_limit = 20;")
self.cur.execute("set geqo_threshold = 20;")
else:
self.cur.execute("set join_collapse_limit = 1;")
if not sql.trained:
self.cur.execute("set geqo_threshold = 20;")
else:
self.cur.execute("set geqo_threshold = 2;")
self.cur.execute("explain (COSTS, FORMAT JSON, ANALYSE) "+sqlwithplan)
rows = self.cur.fetchall()
row = rows[0][0]
import json
# print(json.dumps(rows[0][0][0]['Plan']))
afterCost = rows[0][0][0]['Plan']['Actual Total Time']
# print(1)
except:
self.con.commit()
afterCost = max(thisQueryCost / sql.getDPCost()*sql.getDPlantecy(),sql.timeout())
# print("PGUtils.py excited!!!",afterCost)
else:
afterCost = max(thisQueryCost / sql.getDPCost()*sql.getDPlantecy(),sql.timeout())
afterCost += 5
if self.isLatencyRecord:
LatencyDict[sqlwithplan] = afterCost
global LatencyRecordFileHandle
import json
LatencyRecordFileHandle.write(json.dumps([sqlwithplan,afterCost])+"\n")
LatencyRecordFileHandle.flush()
return afterCost
def getResult(self, sql,sqlwithplan):
"""
:param sql:a sqlSample object
:return: the latency of sql
"""
self.cur.execute("SET statement_timeout = 600000;")
# self.cur.execute("set max_parallel_workers = "+str(config.max_parallel_workers)+";")
# self.cur.execute("set max_parallel_workers_per_gather = "+str(config.max_parallel_workers_per_gather)+";")
if config.use_hint and sqlwithplan.find("/*")>-1:
self.cur.execute("set join_collapse_limit = 20;")
self.cur.execute("set geqo_threshold = 20;")
else:
self.cur.execute("set join_collapse_limit = 1;")
self.cur.execute("set geqo_threshold = 2;")
# self.cur.execute("SET statement_timeout = 4000;")
import time
st = time.time()
self.cur.execute(sqlwithplan)
rows = self.cur.fetchall()
et = time.time()
print('runtime : ',et-st)
return rows
def getCost(self,sql,sqlwithplan):
"""
:param sql: a sqlSample object
:return: the cost of sql
"""
self.cur.execute("set max_parallel_workers = "+str(config.max_parallel_workers)+";")
self.cur.execute("set max_parallel_workers_per_gather = "+str(config.max_parallel_workers_per_gather)+";")
if config.use_hint and sqlwithplan.find("/*")>-1:
self.cur.execute("set join_collapse_limit = 222;")
self.cur.execute("set geqo_threshold = 202;")
else:
self.cur.execute("set join_collapse_limit = 1;")
self.cur.execute("set geqo_threshold = 2;")
self.cur.execute("SET statement_timeout = 40000;")
self.cur.execute("EXPLAIN "+sqlwithplan)
rows = self.cur.fetchall()
row = rows[0][0]
afterCost = float(rows[0][0].split("cost=")[1].split("..")[1].split(" ")[
0])
self.con.commit()
return afterCost
def getPlan(self,sqlwithplan):
"""
:param sql: a sqlSample object
:return: the cost of sql
"""
self.cur.execute("set max_parallel_workers = "+str(config.max_parallel_workers)+";")
self.cur.execute("set max_parallel_workers_per_gather = "+str(config.max_parallel_workers_per_gather)+";")
if config.use_hint and sqlwithplan.find("/*")>-1:
self.cur.execute("set join_collapse_limit = 20;")
self.cur.execute("set geqo_threshold = 20;")
else:
self.cur.execute("set join_collapse_limit = 1;")
self.cur.execute("set geqo_threshold = 2;")
self.cur.execute("SET statement_timeout = 4000;")
sqlwithplan = sqlwithplan +";"
self.cur.execute("EXPLAIN (COSTS, FORMAT JSON) "+sqlwithplan)
rows = self.cur.fetchall()
import json
return rows
def getDPPlanTime(self,sql,sqlwithplan):
"""
:param sql: a sqlSample object
:return: the planTime of sql
"""
import time
startTime = time.time()
cost = self.getCost(sql,sqlwithplan)
plTime = time.time()-startTime
return plTime
def getSelectivity(self,table,whereCondition):
global selectivityDict
if whereCondition in selectivityDict:
return selectivityDict[whereCondition]
# if config.isCostTraining:
self.cur.execute("SET statement_timeout = "+str(int(100000))+ ";")
totalQuery = "select * from "+table+";"
# print(totalQuery)
self.cur.execute("EXPLAIN "+totalQuery)
rows = self.cur.fetchall()[0][0]
# print(rows)
# print(rows)
total_rows = int(rows.split("rows=")[-1].split(" ")[0])
resQuery = "select * from "+table+" Where "+whereCondition+";"
# print(resQuery)
self.cur.execute("EXPLAIN "+resQuery)
rows = self.cur.fetchall()[0][0]
# print(rows)
select_rows = int(rows.split("rows=")[-1].split(" ")[0])
selectivityDict[whereCondition] = -log(select_rows/total_rows)
# else:
# self.cur.execute("SET statement_timeout = "+str(int(100000))+ ";")
# totalQuery = "select count(*) from "+table+";"
# self.cur.execute(totalQuery)
# total_rows = self.cur.fetchall()[0][0]
# resQuery = "select count(*) from "+table+" Where "+whereCondition+";"
# self.cur.execute(resQuery)
# select_rows = self.cur.fetchall()[0][0]+1
# selectivityDict[whereCondition] = -log(select_rows/total_rows)
return selectivityDict[whereCondition]
latencyRecordFile = config.latencyRecordFile
from itertools import count
from pathlib import Path
pgrunner = PGGRunner(config.dbName,config.userName,config.password,config.ip,config.port,isCostTraining=config.isCostTraining,latencyRecord = config.latencyRecord,latencyRecordFile = latencyRecordFile)