From 202b815efca83f0de9acd87d77d65140b2345551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 4 Aug 2022 10:44:49 +0200 Subject: [PATCH 1/2] enable ANALYZE for >100 records only --- fast_update/copy.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fast_update/copy.py b/fast_update/copy.py index 5f9a471..e91c954 100644 --- a/fast_update/copy.py +++ b/fast_update/copy.py @@ -906,7 +906,12 @@ 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})') + # micro 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}"') From d009b4ce5c91d2fb85ff1b1dfe4d30b5d7c192d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 4 Aug 2022 10:51:32 +0200 Subject: [PATCH 2/2] fix formatting --- fast_update/copy.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fast_update/copy.py b/fast_update/copy.py index e91c954..475a298 100644 --- a/fast_update/copy.py +++ b/fast_update/copy.py @@ -906,10 +906,12 @@ 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]) - # micro 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) + # 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))