From 6bb3586051764beb5fd3a53cc4f32912404c334f Mon Sep 17 00:00:00 2001 From: Ozbolt Menegatti Date: Tue, 10 Sep 2019 16:22:43 +0200 Subject: [PATCH] Attempt at speed optimization with sql-join --- src/match.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/match.py b/src/match.py index 294029d..1d65db0 100644 --- a/src/match.py +++ b/src/match.py @@ -11,16 +11,22 @@ class StructureMatch: @staticmethod def from_db(db, colocation_id, structure): result = StructureMatch(colocation_id, structure) - for match_id in db.execute("SELECT mid_match_id FROM ColocationMatches WHERE mid_colocation_id=?", (colocation_id,)): - to_add = {} + prev_match_id = None - for component_id, word_lemma, word_text, word_msd, word_id in db.execute(""" - SELECT component_id, word_lemma, word_text, word_msd, word_id - FROM Matches WHERE match_id=?""", match_id): + stmt = """SELECT match_id, component_id, word_lemma, word_text, word_msd, word_id + FROM ColocationMatches + JOIN Matches ON Matches.match_id=ColocationMatches.mid_match_id + WHERE mid_colocation_id=? + ORDER BY match_id""" - to_add[str(component_id)] = Word(word_lemma, word_msd, word_id, word_text, False) + for row in db.execute(stmt, (colocation_id,)): + match_id, component_id, word_lemma, word_text, word_msd, word_id = row - result.matches.append(to_add) + if match_id != prev_match_id: + result.matches.append({}) + prev_match_id = match_id + + result.matches[-1][str(component_id)] = Word(word_lemma, word_msd, word_id, word_text, False) for component_id, text in db.execute("SELECT component_id, text FROM Representations WHERE colocation_id=?", (colocation_id,)): result.representations[str(component_id)] = text