In oracle: I have a table rel with these columns (object_id1, object_id2) that relates pairs of objects in a system. The object_ids are varchars and the first 3 chars identifies the type of the object. E.g. users start with 001 and books start with 002 but there are many more types. Now I’d like to get all user-book pairs that are related in this table with this query:
SELECT * FROM rel WHERE rel.object_id1 LIKE '001%' AND rel.object_id2 LIKE '002%'
To do this I’d need a b-tree index index on the first three chars of object_id1 and on object_id2. Is it possible to have an index on part of a column, in this case the first 3 chars? How?
You could create a function-based index
But then your queries would need to use the same function calls
Normally, you would create user-defined functions (i.e. get_object_type) that were marked as deterministic and would use those user-defined functions in both the index definition and the queries in order to ensure that someone doesn’t inadvertently use a different construct in order to get the first three characters of a field which would prevent the function-based index from being used.
All that being said, having a column in a table where the first three characters represents some other data element is a violation of basic normalization. You’d almost always be better off storing the first three characters in a separate column rather than trying to parse the composite column at runtime.