ESSolutions / django-mssql-backend

Django backend for Microsoft SQL Server
https://pypi.org/project/django-mssql-backend/
BSD 3-Clause "New" or "Revised" License
147 stars 51 forks source link

pyodbc fast_executemany feature doesn't work #52

Open KiraTheFirebird opened 4 years ago

KiraTheFirebird commented 4 years ago

Hello! pyodbc has a fast_executemany feature, which significantly increases inserts speed when using the ODBC Driver 17 for SQL Server driver. It works perfectly when I creating connection and cursor using pyodbc API directly:

conn = pyodbc.connect(conn_str, autocommit=False)
cursor = conn.cursor()
cursor.fast_executemany = True

cursor.executemany(f'INSERT INTO [{table_name}] ({columns_string}) VALUES ({columns_values_placeholders_string})', data)
conn.commit()

However, I get no effect (no speed boost and MSSQL profiler shows the _exec spprepexec calls instead of the _exec spexecute ones, which appear when fast_executemany really works) when using it with Django API (i.e. via wrapper provided by django-mssql-backend):

with connection.cursor() as cursor:
    cursor.fast_executemany = True
    inserts go here

Am I doing something wrong or this feature simply not implemented?

Thank you.

MrAlexBailey commented 1 year ago

Years later but this may help others.

There are wrappers around the actual cursor object when you use Django. In this case there are two, Django itself, and django-mssql.

You can set the fast_executemany attribute on the true pyodbc.Cursor via:

with connection.cursor() as cursor:
    cursor.cursor.cursor.fast_executemany = True
    # cursor.executemany() is much faster here

Silly, but without changes to both Django and django-mssql I don't believe there's a cleaner method.