Is it possible to do select value(s) from supplierf where s.name LIKE '%search%' order by s.name using a index to do a search like that? I know it's possible to create index for the name, but for a search like that I don't know how things work.
Yes, but Oracle may choose not to use the index based on statistics. You can tell Oracle to use the index via a hint, but whether the index actually 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);
You then do this select:
select * from t where text like '%something%';
There are two obvious ways this query can be answered:
- Full table scan on T
- Full index scan on T_I, then 1 ROWID lookup of T per result found in T_I.
Suppose T has 100,000 rows, and only 5 of them match your search criteria. Suppose also that table T occupies 5000 blocks, and the index T_I occupies 1000 (i.e. only 20% of the size of T).
The actual cost of the queries in terms of reads is then:
- 5000 reads (of T)
- 1000 reads (of T_I) followed by 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 (in reads) will be:
- 5000 reads (of T)
- 1000 reads (of T_I) followed by 5000 reads of T by ROWID = 6000 reads
Hence in this example, Oracle will go for the full table scan although the index search would be quicker. You could hint the query to use the index:
select /*+ index(t t_i) */ from t where text like '%something%';
However, note that this is only better if you are sure the query will return less than 5% of the rows most of the time.