Python's approach to getting full data from a database
Studied: 79503658
The original worship:
ImportPsycopg2.pool fromDatetimeImportdatetime#Bulk Query Sizebatch_size = 1000defcursor_query ():#using a database connection pool, running with a normal connection method can also seem to have a spike in memory, so instead of a connection poolSimple_conn_pool = Psycopg2.pool.SimpleConnectionPool (Minconn=1, maxconn=5, database="dbname", user="username", Password="123456", host="172.0.0.1", port="5432") #getting connections from a database connection poolconn =Simple_conn_pool.getconn ()#Auto-Commit transaction set to FalseConn.autocommit =False#Create a cursor, passing in the name parameter, returns a server-side cursor or returns a client cursorcursor = Conn.cursor ('Cursorname') #first, full-scale data is queriedCursor.execute ('SELECT * FROM tablename') Count=0#Start TimeStart_time =DateTime.Now () whileTrue:count= Count + 1#each fetch is moved from the position of the last cursor to a size position, returning the size bar datadata =Cursor.fetchmany (batch_size)#Interrupt loop When data is empty if notData: Break Print('get%s to%s data successfully'% ((count-1) * batch_size, Count *batch_size)) Print('Fetchmany time to obtain the full amount of data:', (DateTime.Now ()-start_time). seconds) Cursor_query ()
Python's approach to getting full data from a database