Skip to content

Commit

Permalink
Merge pull request #15 from netzkolchose/optimize_analyze
Browse files Browse the repository at this point in the history
ANALYZE optimization
  • Loading branch information
jerch committed Aug 4, 2022
2 parents 43bbd82 + d009b4c commit 59b0e6f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion fast_update/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,14 @@ def copy_update(
c.execute(f'CREATE TEMPORARY TABLE "{temp}" ({create_columns(column_def)})')
copy_from(c, temp, objs, attnames, colnames, get, encs,
encoding or CONNECTION_ENCODINGS[c.connection.encoding])
c.execute(f'ANALYZE "{temp}" ({pk_field.column})')
# optimization (~6x speedup in ./manage.py perf for 10 instances):
# for small changesets ANALYZE is much more expensive than
# a sequential scan of the temp table
# tipping point is 150+-80 records (higher for less fields)
# --> enable ANALYZE for >100 only (assuming rather big records
# for copy_update)
if len(objs) > 100:
c.execute(f'ANALYZE "{temp}" ({pk_field.column})')
c.execute(update_sql(model._meta.db_table, temp, pk_field.column, fields))
rows_updated = c.rowcount
c.execute(f'DROP TABLE "{temp}"')
Expand Down

0 comments on commit 59b0e6f

Please sign in to comment.