A regular B-tree index on id is created in the records(id) table. Why will the next query not use the index? select * from records where id % 2 = 0 - GIN type index is needed for id - Indexes do not work with expressions in WHERE - % is a comparison operation, not a filter - limit and offset are mandatory for index optimization - The query accesses a numeric field, not a string field
sobes.tech AI
Answer from AI
The query select * from records where id % 2 = 0 will not use a regular B-tree index on the id field because the condition contains an expression with the % operator (modulo), which transforms the field value before comparison.
A B-tree index works efficiently only with direct comparison of a field to a constant or a range, for example id = 5 or id > 10. When a function or expression is used on an indexed field in the WHERE clause, the index cannot be applied directly.
The correct answer: Indexes do not work with expressions in WHERE.
For such cases, functional indexes can be created, for example, an index on the expression (id % 2), if the DBMS supports it, or other types of indexes can be used, but a regular B-tree index on id will not help.