Is it possible to select a value from the providerf, where s.name LIKE '% search%' order by s.name using the index to do such a search? I know that you can create an index for a name, but for such a search, I do not know how everything works.
Yes, but Oracle may not use a statistics-based index. You can tell Oracle to use the index with a hint, but whether the index really helps will depend on your data. Suppose you have this table and index:
create table t (id integer primary key, text varchar2(50), other_cols...); create index t_i on t (text);
Then you choose:
select * from t where text like '%something%';
There are two obvious ways to respond to this request:
- Full table scan on T
- Checking the full index on T_I, then 1 ROWID search on T for the result found in T_I.
Suppose T has 100,000 rows, and only 5 of them match your search criteria. Suppose also that the table T occupies 5,000 blocks and the index T_I occupies 1000 (i.e., only 20% of the size T).
Actual cost of requests in terms of reading:
- 5000 views (from T)
- 1000 views (from T_I), and then 5 reads of T by ROWID = 1005 reads
Clearly, in this case, the index is better. However, Oracle tends to assume that the LIKE query will return 5% of the rows (i.e. 5000 rows), so its estimated costs (when reading) will be:
- 5000 views (from T)
- 1000 views (from T_I), and then 5000 reads of T by ROWID = 6000 reads
Therefore, in this example, Oracle will do a full table scan, although index search will be faster. You might be hinting at an index usage request:
select from t where text like '%something%';
However, note that this is better if you are sure that the query will return less than 5% of the rows in most cases.