Search code examples
databaseindexingb-tree

What databases have optimization for functions, in order to use indices?


Suppose, I have a float column and a a b-tree index and a million rows:

CREATE TABLE test (
     val FLOAT,
     KEY (val)
);

INSERT INTO test VALUES (random(-1000, 1000)), (random(-1000, 1000)), ...

(1M rows)

Now, if I want to make a query like

SELECT * FROM test WHERE abs(val) > param

or

SELECT * FROM test WHERE int(val) % 2; /* odd integer numbers only */

What databases can optimize this to use the index on val, so that I don't have to use an index on the expression I check, or reverse expression myself?


Solution

  • I don't think any DBMS can use a "plain" index on val if you use that column in a function or expression.

    The main reason is that the result of applying the function could make the index lookup very expensive at best (basically creating an index based on the expression "on the fly") or plain wrong in the worst case in case the index lookup for the "plain" value returns a completely different row than the index lookup on the result of the expression (which is very likely to happen).