forked from Abdisalan/blog-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffset_pagination.py
35 lines (29 loc) · 1.01 KB
/
offset_pagination.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
import psycopg2
import time
try:
connection = psycopg2.connect(user = "Abdi",
password = "",
host = "localhost",
port = "5432",
database = "twitter")
cursor = connection.cursor()
page_size = 100
s_total = time.time()
for i in range(2000):
start = time.time()
# print("page %s" % i)
cursor.execute("SELECT * from tweet ORDER BY id DESC LIMIT 100 OFFSET %s", (page_size * i,))
cursor.fetchall()
end = time.time()
if i % 100 == 0:
print("%s,%s" % (end - start,i))
e_total = time.time()
print("TOTAL %s" % (e_total - s_total))
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
# print("PostgreSQL connection is closed")