Python frequently reads MySQL related issues

Source: Internet
Author: User

1, need to select large amounts of data frequently, long time, memory consumption, how to solve the MySQL performance problem?

If there is no requirement for the number of results returned, you can control the number returned:

Cursor.fetchmany (size=1000)

This returns only 1000 data, and if the returned result is less than size, all data is returned;

If you only need one, it's simpler: Fetchone ()

2, every time the data inserted is too large, MySQL server has gone away how to solve?

stored as BLOB type;

Modified in my.conf: Max_allowed_packet = 500m

3, to the Python list type into MySQL, and then the next time you need to read in the list format, how to do?

Because the list type contains a comma with half-width, or if the inserted data contains special symbols, it is not possible to insert MySQL properly.

There are many ways in Google, and I'm taking base64. The data that will be inserted base64 encode can be stored in MySQL normally.

BASE64STR = Base64.b64encode (str (MYSQLSTR))

Mysqlstr = Base64.b64decode (B64STR)

Note: When you read, you need to Base64decode, when you get the STR, you can not use the list sequence to take the value. What to do?

Eval (String)

As above, Eval can be a good solution to this problem, the Str into a tuple, it can be used directly.

4, the frequent operation of MySQL more censored data, it is best to use multi-threaded database, to avoid the trouble caused by my.conf configuration problems.

Here is a MySQL multithreaded operation class:

1 classMYSQL:2     def __init__(self,sql):3Self.sql =SQL4Self.conn = MySQLdb.connect (charset='UTF8', user='yourname', passwd='passwd', db='your dbname')            5Self.cursor =self.conn.cursor ()6  7     defInsert (self):8 Self.cursor.execute (self.sql)9 Self.conn.commit ()Ten self.cursor.close () One self.conn.close () A         returnTrue -   -     defSelect (self): the Self.cursor.execute (self.sql) -AllData =Self.cursor.fetchall () - self.cursor.close () - self.conn.close () +         returnAllData -   +     defUpdate (self): A Self.cursor.execute (self.sql) at Self.conn.commit () - self.cursor.close () - self.conn.close () -         returnTrue

Python frequently reads MySQL related issues

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.