python - When to close / open a MySQL connection -
i have program runs perhaps 100m selects , inserts.
is there benefit of closing , reopening cursor after n number of statements? or should open once -- @ beginning -- , close @ end?
two options:
def connect(self): if self.conn: self.conn.close() self.conn = mysqldb.connect (...) self.cursor = self.conn.cursor() 1) self.connect() # statements self.conn.close() 2) self.connect() num, sql_statement in enumerate(sql_statements): if num == n: self.connect() # sql statement self.conn.close()
is there advantage using second route? there difference between 2 options?
in general want keep connections alive little time possible, there performance impact opening , closing connections.
if running multiple queries in sequence batch of sorts, use 1 connection entire batch (your first scenario).
however, if statements discrete transactions i'd think best approach open , close connection each statement.
of course, last statement doesn't take performance impact account respect expected performance of application.
Comments
Post a Comment