Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aiomysql executemany is very slow to insert #984

Open
RuoCJ opened this issue Apr 27, 2024 Discussed in #983 · 4 comments
Open

aiomysql executemany is very slow to insert #984

RuoCJ opened this issue Apr 27, 2024 Discussed in #983 · 4 comments

Comments

@RuoCJ
Copy link

RuoCJ commented Apr 27, 2024

Discussed in #983

Originally posted by RuoCJ April 27, 2024
I used aiomysql executemany to insert over 800 pieces of data, which took over thirty seconds, but pymysql only took over two seconds。

@yarodevuci
Copy link

same here, it's slow insert also one row as well..

@RuoCJ
Copy link
Author

RuoCJ commented Jul 22, 2024

same here, it's slow insert also one row as well..这里也是一样,插入一行也很慢。

One idea I recently tried was to combine several insert statements into one, executed using execute instead of executemany

@yarodevuci
Copy link

same here, it's slow insert also one row as well..这里也是一样,插入一行也很慢。

One idea I recently tried was to combine several insert statements into one, executed using execute instead of executemany

Can you post an example how you call multiple statements?

@RuoCJ
Copy link
Author

RuoCJ commented Jul 23, 2024

same here, it's slow insert also one row as well..这里也是一样,插入一行也很慢。

One idea I recently tried was to combine several insert statements into one, executed using execute instead of executemany

Can you post an example how you call multiple statements?

This operation is only for insert statements

sql = "INSERT INTO table_a (field1, field2, ..) VALUES {}"

values_str = create_insert_values(tlis_schp)
conn = db_pool.connection()
cursor = conn.cursor()
cursor.execute(sql, values_str or ())

def create_insert_values(data):
    values_list = []
    for record in data:
        values = []
        for item in record:
            if isinstance(item, str):
                escaped_item = item.replace("'", "''")
                values.append(f"'{escaped_item}'")
            elif item is None or (isinstance(item, float) and math.isnan(item)):
                values.append("NULL")
            elif isinstance(item, datetime.datetime):
                values.append(f"'{item}'")
            elif isinstance(item, decimal.Decimal):
                values.append(f"'{item}'")
            elif isinstance(item, decimal.Decimal):
                values.append(f"'{item}'")
            else:
                values.append(str(item))
        values_str = f"({', '.join(values)})"
        values_list.append(values_str)
    return ',  '.join(values_list)

This is a bit more cumbersome, but much faster than using executemany directly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants