Skip to content
Advertisement

Use of LIKE in Oracle with high number of variables

On Oracle database, I have individual queries which I would like to add based on substitution in the search word. Consider each word can have its word substitution and additional synonym substitution. For instance, the word 4MULA has word substitution such as formula, fourmula. The word VIEW has word substitution such as view, vu and Education has synonyms such as university, college, school

The structure of the where has the following order (3 words):

Each word has to be substitute by the all possible values to replace it. I have done individual queries, but then I have an excessive amount of queries (about 500 individual queries replacing all possible substitutions). For instance, another individual query would be:

The final number of queries depends on the total number of permutations:

To optimize time I added all possible permutations using OR condition. For instance, I have added as follows:

What is the best way to add all permutations in a single query?

Advertisement

Answer

You can use object type and collections to create a list of synonyms for each word and then create a user-defined function to handle the substitutions:

Oracle Setup:

Test Data:

Query:

outputs:

TEXT                       | SUBSTITUTIONS             
:------------------------- | :-------------------------
FORMULA VIEW UNIVERSITY    | 4MULA VIEW UNIVERSITY     
4MULA VIEW UNIVERSITY      | 4MULA VIEW UNIVERSITY     
FOURMULA VIEW UNIVERSITY   | 4MULA VIEW UNIVERSITY     
FORMULA VU UNIVERSITY      | 4MULA VIEW UNIVERSITY     
4MULA VU UNIVERSITY        | 4MULA VIEW UNIVERSITY     
FOURMULA VU UNIVERSITY     | 4MULA VIEW UNIVERSITY     
FOURMULA VU SCHOOL         | 4MULA VIEW UNIVERSITY     
FOURMULAE VULCAN SCHOOLING | FOURMULAE VULCAN SCHOOLING

and:

outputs:

| TEXT                     |
| :----------------------- |
| FORMULA VIEW UNIVERSITY  |
| 4MULA VIEW UNIVERSITY    |
| FOURMULA VIEW UNIVERSITY |
| FORMULA VU UNIVERSITY    |
| 4MULA VU UNIVERSITY      |
| FOURMULA VU UNIVERSITY   |
| FOURMULA VU SCHOOL       |

db<>fiddle here


Currently, in test_data table I’d have just a record such as FORMULA VIEW UNIVERSITY. In addition, I have a table for word substitution and synonyms in very similar fashion you included. Considering I have just the record FORMULA VIEW UNIVERSITY how could I invoke replace_synonyms such as: SELECT text FROM test_data WHERE replace_synonyms(...)) LIKE '%4MULA VU%%UNIVERSITY%'. Notice that usually I will want to compare a new name which is not in test_data as the case of '%4MULA VU%%UNIVERSITY%'

Swap it round so that the synonyms are normalised in the LIKE filter and not in the text in the table:

Then:

Outputs:

| TEXT                    |
| :---------------------- |
| FORMULA VIEW UNIVERSITY |

db<>fiddle here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement