Skip to content

Commit 38b7a6c

Browse files
author
sreeder
committed
add code to test optimization of saving datavalues
1 parent 04dc270 commit 38b7a6c

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/api/ODM2/services/createService.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,54 @@ def createTimeSeriesResultValues(self, resultid, datavalues, datetimes, datetime
467467

468468
def createTimeSeriesResultValues(self, datavalues):
469469
try:
470-
datavalues.to_sql(name="timeseriesresultvalues", if_exists='append', con=self._session_factory.engine, index=False)
470+
#using Pandas built-in --slow
471+
#changing way values sent --unknown error on insert
472+
datavalues.to_sql(name=TimeSeriesResultValues.__tablename__,
473+
schema=TimeSeriesResultValues.__table_args__['schema'],
474+
if_exists='append',
475+
chunksize= 1000,
476+
con=self._session_factory.engine,
477+
index=False)
471478
self._session.commit()
479+
480+
481+
#using sqlalchemy core --sending empty parameters
482+
# data = datavalues.to_dict('records')
483+
# self._session.execute(TimeSeriesResultValues.__table__.insert(data))
484+
485+
#using cursor and StringIO --not all cursors have the copy_from function
486+
# print "using cursor"
487+
# import cStringIO
488+
# #stream the data using 'to_csv' and StringIO(); then use sql's 'copy_from' function
489+
# output = cStringIO.StringIO()
490+
# #ignore the index
491+
# datavalues.to_csv(output, sep='\t', header=False, index=False)
492+
# #jump to start of stream
493+
# output.seek(0)
494+
# contents = output.getvalue()
495+
# connection = self._session_factory.engine.raw_connection()
496+
# cur = connection.cursor()
497+
# #null values become ''
498+
# cur.copy_from(output, 'ODM2.TimeSeriesResultValues', null="")
499+
# connection.commit()
500+
# cur.close()
501+
502+
#using Bulk Insert * user must have permissions --file created locally code running remote
503+
# datavalues.to_csv('C:\\Users\\Stephanie\\temp.csv')
504+
# sql = """
505+
# BULK INSERT ODM2.TimeSeriesResultValues
506+
# FROM 'C:\\Users\\Stephanie\\temp.csv' WITH (
507+
# FIELDTERMINATOR=',',
508+
# ROWTERMINATOR='\\n');
509+
# """
510+
# self._session.execute(sql)
511+
512+
513+
514+
472515
return datavalues
473516
except Exception, e:
474-
print e.message
517+
print e
475518
return None
476519

477520

0 commit comments

Comments
 (0)