From: Sarah Hoffmann Date: Wed, 22 Nov 2023 16:33:17 +0000 (+0100) Subject: avoid index on rank_address in near search X-Git-Tag: v4.4.0~78^2~4 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/155f26060d0bd87389a22e357db11fd85df1f9de avoid index on rank_address in near search --- diff --git a/nominatim/api/search/db_searches.py b/nominatim/api/search/db_searches.py index 97c4292e..62e9a450 100644 --- a/nominatim/api/search/db_searches.py +++ b/nominatim/api/search/db_searches.py @@ -24,6 +24,13 @@ from nominatim.db.sqlalchemy_types import Geometry #pylint: disable=singleton-comparison,not-callable #pylint: disable=too-many-branches,too-many-arguments,too-many-locals,too-many-statements +def no_index(expr: SaColumn) -> SaColumn: + """ Wrap the given expression, so that the query planner will + refrain from using the expression for index lookup. + """ + return sa.func.coalesce(sa.null(), expr) # pylint: disable=not-callable + + def _details_to_bind_params(details: SearchDetails) -> Dict[str, Any]: """ Create a dictionary from search parameters that can be used as bind parameter for SQL execute. @@ -295,7 +302,7 @@ class NearSearch(AbstractSearch): else_ = tgeom.c.centroid.ST_Expand(0.05))))\ .order_by(tgeom.c.centroid.ST_Distance(table.c.centroid)) - sql = sql.where(t.c.rank_address.between(MIN_RANK_PARAM, MAX_RANK_PARAM)) + sql = sql.where(no_index(t.c.rank_address).between(MIN_RANK_PARAM, MAX_RANK_PARAM)) if details.countries: sql = sql.where(t.c.country_code.in_(COUNTRIES_PARAM)) if details.excluded: