I am trying to make faster to get large size data from SQL.
Here is my system info.
OS: Linux
Language: python3
SQL library: pymssql
sample code
msg= "SELECT TAG_CODE, UI_X, UI_Y, INSERT_TIME FROM dbo.R_TAG_HIST WHERE "
msg+= "CREATE_TIME > '" + self.start_time + \
"' AND CREATE_TIME < '" + self.end_time + "' "
msg += "AND TAG_CODE IN " + \
str(self.tag_ids).replace("[", "(").replace("]", ")")
msg+= " ORDER BY TAG_CODE"
def receive_all_tables(self, msg):
try:
# check connection
try:
hasattr(self.conn, '_conn')
except:
self.connect_db()
with self.conn.cursor() as cursor:
cursor.execute(msg)
tables = cursor.fetchall()
self.conn.commit()
return tables
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
print("fail to receive query.", exc_type, exc_obj, exc_tb.tb_lineno, e)
def result_iterator(self, cursor, arraysize=1000):
# 'iterator using fetchmany and consumes less memory'
while True:
results = cursor.fetchmany(arraysize)
if not results:
break
for result in results:
yield result
def receive_all_tables_by_iterator(self, msg):
try:
# check connection
try:
hasattr(self.conn, '_conn')
except:
self.connect_db()
tables=[]
with self.conn.cursor() as cursor:
cursor.execute(msg)
for result in self.result_iterator(cursor) :
tables.append(result)
# self.conn.commit()
return tables
sample data have 30k lines.
elapsed time using fetchall() : 6.272587060928345 sec\
elapsed time using fetchall() : 6.012945890426636 sec
I want to reduce elapsed time to get data.
I would like to know another good way to receive large data from db.
Help me please :)