From edf1383048bfbd117ec27fb25a74c4b02a430ecd Mon Sep 17 00:00:00 2001 From: Tim Laszlo Date: Fri, 15 Jun 2012 09:41:50 -0500 Subject: DBStats: Load entries in bulk Improve load time by fetching all entries at once instead of individually. --- src/lib/Bcfg2/Server/Reports/importscript.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/Bcfg2/Server/Reports/importscript.py b/src/lib/Bcfg2/Server/Reports/importscript.py index 4e6ca2b96..14f2bb1f9 100755 --- a/src/lib/Bcfg2/Server/Reports/importscript.py +++ b/src/lib/Bcfg2/Server/Reports/importscript.py @@ -178,6 +178,9 @@ def load_stat(cobj, statistics, encoding, vlevel, logger, quick, location): (client_name, traceback.format_exc().splitlines()[-1])) + entries_cache = {} + [entries_cache.__setitem__((e.kind, e.name), e) \ + for e in Entries.objects.all()] counter_fields = {TYPE_BAD: 0, TYPE_MODIFIED: 0, TYPE_EXTRA: 0} @@ -189,8 +192,11 @@ def load_stat(cobj, statistics, encoding, vlevel, logger, quick, location): counter_fields[type] = counter_fields[type] + 1 rr = _fetch_reason(x, build_reason_kwargs(x, encoding, logger), logger) - entry, created = Entries.objects.get_or_create(\ - name=x.get('name'), kind=x.tag) + try: + entry = entries_cache[(x.tag, x.get('name'))] + except KeyError: + entry, created = Entries.objects.get_or_create(\ + name=x.get('name'), kind=x.tag) Entries_interactions(entry=entry, reason=rr, interaction=current_interaction, @@ -204,8 +210,11 @@ def load_stat(cobj, statistics, encoding, vlevel, logger, quick, location): if good_reason == None: # Do this once. Really need to fix Reasons... good_reason = _fetch_reason(x, build_reason_kwargs(x, encoding, logger), logger) - entry, created = Entries.objects.get_or_create(\ - name=x.get('name'), kind=x.tag) + try: + entry = entries_cache[(x.tag, x.get('name'))] + except KeyError: + entry, created = Entries.objects.get_or_create(\ + name=x.get('name'), kind=x.tag) Entries_interactions(entry=entry, reason=good_reason, interaction=current_interaction, type=TYPE_GOOD).save() -- cgit v1.2.3-1-g7c22