From cb9a30f28529cc7a12d443ae2fb7d8c25f7c3ccc Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Thu, 16 Apr 2020 10:48:40 -0400 Subject: [PATCH 1/2] faster query --- lbry/wallet/server/db/reader.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lbry/wallet/server/db/reader.py b/lbry/wallet/server/db/reader.py index b60990001..6766ff132 100644 --- a/lbry/wallet/server/db/reader.py +++ b/lbry/wallet/server/db/reader.py @@ -545,11 +545,19 @@ def _apply_constraints_for_array_attributes(constraints, attr, cleaner, for_coun f':$any_{attr}{i}' for i in range(len(any_items)) ) if for_count or attr == 'tag': - any_queries[f'#_any_{attr}'] = f""" - {CLAIM_HASH_OR_REPOST_HASH_SQL} IN ( - SELECT claim_hash FROM {attr} WHERE {attr} IN ({values}) - ) - """ + if attr == 'tag': + any_queries[f'#_any_{attr}'] = f""" + (claim.claim_type != {CLAIM_TYPES['repost']} + AND claim.claim_hash IN (SELECT claim_hash FROM tag WHERE tag IN ({values}))) OR + (claim.claim_type == {CLAIM_TYPES['repost']} AND + claim.reposted_claim_hash IN (SELECT claim_hash FROM tag WHERE tag IN ({values}))) + """ + else: + any_queries[f'#_any_{attr}'] = f""" + {CLAIM_HASH_OR_REPOST_HASH_SQL} IN ( + SELECT claim_hash FROM {attr} WHERE {attr} IN ({values}) + ) + """ else: any_queries[f'#_any_{attr}'] = f""" EXISTS( From 7ffdfd12f8011074d14f30c977c5ad50c1ef192b Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Thu, 16 Apr 2020 11:04:24 -0400 Subject: [PATCH 2/2] faster not tags --- lbry/wallet/server/db/reader.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lbry/wallet/server/db/reader.py b/lbry/wallet/server/db/reader.py index 6766ff132..a98330efb 100644 --- a/lbry/wallet/server/db/reader.py +++ b/lbry/wallet/server/db/reader.py @@ -604,11 +604,19 @@ def _apply_constraints_for_array_attributes(constraints, attr, cleaner, for_coun f':$not_{attr}{i}' for i in range(len(not_items)) ) if for_count: - constraints[f'#_not_{attr}'] = f""" - {CLAIM_HASH_OR_REPOST_HASH_SQL} NOT IN ( - SELECT claim_hash FROM {attr} WHERE {attr} IN ({values}) - ) - """ + if attr == 'tag': + constraints[f'#_not_{attr}'] = f""" + (claim.claim_type != {CLAIM_TYPES['repost']} + AND claim.claim_hash NOT IN (SELECT claim_hash FROM tag WHERE tag IN ({values}))) AND + (claim.claim_type == {CLAIM_TYPES['repost']} AND + claim.reposted_claim_hash NOT IN (SELECT claim_hash FROM tag WHERE tag IN ({values}))) + """ + else: + constraints[f'#_not_{attr}'] = f""" + {CLAIM_HASH_OR_REPOST_HASH_SQL} NOT IN ( + SELECT claim_hash FROM {attr} WHERE {attr} IN ({values}) + ) + """ else: constraints[f'#_not_{attr}'] = f""" NOT EXISTS(