Python's approach to getting full data from a database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.