标签:cep loop def else color lse 插入数据 tab 简介
在某些场景下,我们需要在一瞬间插入大量数据的情况下,我们要使用一些性能比较好的方式来插入数据,这样才能保证接口的时间。下面测试的数据是1.6W条数据的时间。
@staticmethod def save_data(data, pg_con=None): try: pg_con = pg_con if pg_con else g.pg_db for i in data: loophole = Loophole(**i) pg_con.add(loophole) pg_con.commit() return True except Exception, e: logging.error(‘class path: %s;function name: %s;error message:%s‘ % ( __name__, sys._getframe().f_code.co_name, e.message), exc_info=True)
pg_con.rollback()
return False
注:此方式是最慢的
@staticmethod def save_data(data, pg_con=None): try: pg_con = pg_con if pg_con else g.pg_db loopholes = list() for i in data: loopholes.append(Loophole(**i)) pg_con.bulk_save_objects(loopholes) pg_con.commit() return True except Exception, e: logging.error(‘class path: %s;function name: %s;error message:%s‘ % ( __name__, sys._getframe().f_code.co_name, e.message), exc_info=True)
pg_con.rollback()
return False
注:如果在数据量超大的时候 ,我们可以在每 1000 条的时候 bulk_save_objects 一次。
@staticmethod def save_data(data, pg_con=None): try: pg_con = pg_con if pg_con else g.pg_db pg_con.bulk_insert_mappings(Loophole, data) pg_con.commit() return True except Exception, e: logging.error(‘class path: %s;function name: %s;error message:%s‘ % ( __name__, sys._getframe().f_code.co_name, e.message), exc_info=True) pg_con.rollback() return False
@staticmethod def save_data(data, pg_con=None): try: pg_con = pg_con if pg_con else g.pg_db pg_con.execute( Loophole.__table__.insert(), data ) pg_con.commit() return True except Exception, e: logging.error(‘class path: %s;function name: %s;error message:%s‘ % ( __name__, sys._getframe().f_code.co_name, e.message), exc_info=True) pg_con.rollback() return False
标签:cep loop def else color lse 插入数据 tab 简介
原文地址:https://www.cnblogs.com/shangwei/p/14430930.html