FirebirdSQL / firebird

Firebird server, client and tools
https://firebirdsql.org
1.26k stars 217 forks source link

Query "WHERE A.NAME LIKE :NAME || '%'" does not use index on NAME field #8322

Closed dsonda closed 23 hours ago

dsonda commented 1 day ago

This query does not use the index on NAME field and use a natural plan:

SELECT A.ID, A.NAME FROM PRODUCT A WHERE A.NAME LIKE :NAME || '%'

asfernandes commented 1 day ago

Indices are chosen at prepare time, where the is no value for :NAME, that can start with %. If you replace LIKE by STARTING, it could use. Or use LIKE with constant instead of parameter: LIKE 'Name%'. In this case, Firebird will insert an implicit STARTING WITH 'Name' making it could use an index.

dsonda commented 1 day ago

This command is created by an ORM, so I'm trying to intercept and change to STARTING WITH :NAME, until the ORM fix.

mrotteveel commented 23 hours ago

Duplicates #7912