简体   繁体   中英

When to close / open a MySQL connection

I have a program that runs perhaps 100M selects and inserts.

Is there any benefit of closing and reopening the cursor after N number of statements? Or should I just open it once -- at the beginning -- and then close it at the end?

Two options:

def connect(self):
    if self.conn:
        self.conn.close()
    self.conn = MySQLdb.connect (...)
    self.cursor = self.conn.cursor()

1)  
self.connect()
# all statements
self.conn.close()

2)
self.connect()
for num, sql_statement in enumerate(sql_statements):
    if num == N:
        self.connect()
    # sql statement
self.conn.close()

Is there any advantage to using the second route? Is there any difference between the two options?

In general you want to keep connections alive for as little time as possible, but there is a performance impact to opening and closing connections.

If you are running multiple queries in sequence as a batch of sorts, then use one connection for the entire batch (your first scenario).

However, if the statements are discrete transactions then I'd think the best approach would be to open and close the connection for each statement.

Of course, my last statement doesn't take the performance impact into account with respect to the expected performance of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM