-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselectBench4.py
More file actions
182 lines (162 loc) · 6.4 KB
/
selectBench4.py
File metadata and controls
182 lines (162 loc) · 6.4 KB
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
"""Perform select benchmark test.
usage: selectBench4.py [-h] [--wal]
options:
-h, --help Show this help message and exit
--wal Use WAL(Write a head log) instead of traditional rollback journal for SQLite.
"""
from docopt import docopt
import io, datetime, bench
address = ['Tokyo', 'Chiba', 'Saitama', 'Kanagawa', 'Osaka', 'Kyoto']
department = ['Sales1', 'Sales2', 'Consulting', 'HumanResources', 'Marketing']
def bulkUpdate(conn, beginTranFunc, commitTranFunc, updateFunc):
cur = conn.cursor()
# Bulk insert 100 records at once.
indicies = range(1, 50000)
for chunk in [indicies[x:x+100] for x in range(1, len(indicies), 100)]:
beginTranFunc(cur)
updateFunc(cur, chunk)
commitTranFunc(cur)
def selectBenchSqlite(conn):
def insertAddress(cur):
cur.executemany(
"insert into addresses (address) values (?)",
map((lambda x: (x,)), address)
)
def insertDepartment(cur):
cur.executemany(
"insert into departments (department_name, created) values (?, CURRENT_TIMESTAMP)",
map((lambda x: (x,)), department)
)
def insertFunc(cur, chunk):
for i in chunk:
cur.execute("select address_id from addresses where address = ?", [address[i % len(address)]])
aid = cur.fetchone()[0]
cur.execute(
"""
insert into users (address_id, user_name, first_name, last_name, created)
values (?, ?, ?, ?, CURRENT_TIMESTAMP)
""",
[aid, "user%08d" %i, "first%08d" %i, "last%08d" %i]
)
def insertFunc2(cur, chunk):
for i in chunk:
if (i % 33) != 0:
cur.execute(
"select department_id from departments where department_name = ?",
[department[i % len(department)]]
)
did = cur.fetchone()[0]
cur.execute(
"select user_id from users where user_name = ?",
["user%08d" %i]
)
uid = cur.fetchone()[0]
cur.execute(
"""
insert into user_department (user_id, department_id)
values (?, ?)
""",
[uid, did]
)
def performInsert():
cur = conn.cursor()
insertAddress(cur)
insertDepartment(cur)
bulkUpdate(
conn,
lambda cur: cur.execute('BEGIN TRANSACTION'), lambda cur: cur.execute('COMMIT'), insertFunc
)
bulkUpdate(
conn,
lambda cur: cur.execute('BEGIN TRANSACTION'), lambda cur: cur.execute('COMMIT'), insertFunc2
)
def performSelect():
cur = conn.cursor()
for i in range(1, 500):
cur.execute(
"""
select count(u.user_id) from users u
inner join addresses a on u.address_id = a.address_id
left join user_department ud on u.user_id = ud.user_id
inner join departments d on ud.department_id = d.department_id
where (d.department_name = 'Sales1' or d.department_name is null) and address = 'Tokyo'
"""
)
cur.fetchone()
bench.createTableSqlite(conn)
bench.withStopwatch("insert departments with SQLite", performInsert)
bench.withStopwatch("select departments with SQLite", performSelect)
def selectBenchPgsql(conn):
def insertAddress(cur):
cur.executemany(
"insert into addresses (address) values (%s)",
map((lambda x: (x,)), address)
)
def insertDepartment(cur):
cur.executemany(
"insert into departments (department_name, created) values (%s, CURRENT_TIMESTAMP)",
map((lambda x: (x,)), department)
)
def insertFunc(cur, chunk):
for i in chunk:
cur.execute("select address_id from addresses where address = %s", [address[i % len(address)]])
aid = cur.fetchone()[0]
cur.execute(
"""
insert into users (address_id, user_name, first_name, last_name, created)
values (%s, %s, %s, %s, CURRENT_TIMESTAMP)
""",
[aid, "user%08d" %i, "first%08d" %i, "last%08d" %i]
)
def insertFunc2(cur, chunk):
for i in chunk:
if (i % 33) != 0:
cur.execute(
"select department_id from departments where department_name = %s",
[department[i % len(department)]]
)
did = cur.fetchone()[0]
cur.execute(
"select user_id from users where user_name = %s",
["user%08d" %i]
)
uid = cur.fetchone()[0]
cur.execute(
"""
insert into user_department (user_id, department_id)
values (%s, %s)
""",
[uid, did]
)
def performInsert():
cur = conn.cursor()
insertAddress(cur)
insertDepartment(cur)
bulkUpdate(
conn, (lambda cur: None), lambda cur: conn.commit(), insertFunc
)
bulkUpdate(
conn, (lambda cur: None), lambda cur: conn.commit(), insertFunc2
)
def performSelect():
cur = conn.cursor()
cur.execute(
"prepare myquery as "
"""
select count(u.user_id) from users u
inner join addresses a on u.address_id = a.address_id
left join user_department ud on u.user_id = ud.user_id
inner join departments d on ud.department_id = d.department_id
where (d.department_name = 'Sales1' or d.department_name is null) and address = 'Tokyo'
"""
)
for i in range(1, 500):
cur.execute("execute myquery")
cur.fetchone()
bench.createTablePgsql(conn)
bench.withStopwatch("insert departments with Postgres", performInsert)
bench.withStopwatch("select departments with Postgres", performSelect)
if __name__ == '__main__':
args = docopt(__doc__)
bench.withSqliteConnection("/tmp/test.db", selectBenchSqlite, isolationLevel = None, useWal = args["--wal"])
bench.withPgsqlConnection(selectBenchPgsql)