dont run common tag optimization strategy if there are 5 or more common tags provided

This commit is contained in:
Lex Berezhny 2019-07-20 14:27:34 -04:00
parent 87e52bec0a
commit 3afd615447

View file

@ -436,16 +436,30 @@ def _apply_constraints_for_array_attributes(constraints, attr, cleaner, for_coun
any_queries = {} any_queries = {}
common_items = any_items & COMMON_TAGS.keys() if attr == 'tag':
if common_items: common_tags = any_items & COMMON_TAGS.keys()
any_items -= common_items if common_tags:
for item in common_items: any_items -= common_tags
index_name = COMMON_TAGS[item] if len(common_tags) < 5:
any_queries[f'#_any_{attr}_{index_name}'] = f""" for item in common_tags:
index_name = COMMON_TAGS[item]
any_queries[f'#_common_tag_{index_name}'] = f"""
EXISTS( EXISTS(
SELECT 1 FROM {attr} INDEXED BY tag_{index_name}_idx WHERE SELECT 1 FROM tag INDEXED BY tag_{index_name}_idx WHERE claim.claim_hash=tag.claim_hash
claim.claim_hash={attr}.claim_hash AND tag = '{item}'
AND {attr} = '{item}' )
"""
elif len(common_tags) >= 5:
constraints.update({
f'$any_common_tag{i}': item for i, item in enumerate(common_tags)
})
values = ', '.join(
f':$any_common_tag{i}' for i in range(len(common_tags))
)
constraints[f'#_any_common_tags'] = f"""
EXISTS(
SELECT 1 FROM tag WHERE claim.claim_hash=tag.claim_hash
AND tag IN ({values})
) )
""" """
@ -455,9 +469,18 @@ def _apply_constraints_for_array_attributes(constraints, attr, cleaner, for_coun
values = ', '.join( values = ', '.join(
f':$any_{attr}{i}' for i in range(len(any_items)) f':$any_{attr}{i}' for i in range(len(any_items))
) )
any_queries[f'claim.claim_hash__in#_any_{attr}'] = f""" if for_count or attr == 'tag':
SELECT claim_hash FROM {attr} WHERE {attr} IN ({values}) any_queries[f'claim.claim_hash__in#_any_{attr}'] = f"""
""" SELECT claim_hash FROM {attr} WHERE {attr} IN ({values})
"""
else:
any_queries[f'#_any_{attr}'] = f"""
EXISTS(
SELECT 1 FROM {attr} WHERE
claim.claim_hash={attr}.claim_hash
AND {attr} IN ({values})
)
"""
if len(any_queries) == 1: if len(any_queries) == 1:
constraints.update(any_queries) constraints.update(any_queries)
elif len(any_queries) > 1: elif len(any_queries) > 1: