-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_pgrunner.py
27 lines (24 loc) · 937 Bytes
/
my_pgrunner.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
import psycopg2
import json
from time import time
class PGRunner():
def __init__(self, db_name, user, password, host, port):
self.conn = psycopg2.connect(database=db_name,user=user,password=password,host=host,port=port)
self.cur = self.conn.cursor()
self.cur.execute("load 'pg_hint_plan';")
def getCost(self, sql):
# start = time()
self.cur.execute("explain (COSTS, FORMAT JSON) "+sql)
rows = self.cur.fetchall()
plan_json = rows[0][0][0]
# stop = time()
# print("time: ", stop - start)
# start = time()
# self.cur.execute("explain (COSTS, FORMAT JSON, ANALYSE) "+sql)
# rows = self.cur.fetchall()
# plan_json = rows[0][0][0]
# stop = time()
# print("time: ", stop - start)
# input()
return plan_json['Plan']['Total Cost']
pg_runner = PGRunner('imdb', 'postgres', '', '127.0.0.1', 5432)