Let’s assume the EMPLOYEES table (from the HR schema) contains many records, and we want to write an (efficient) SQL query that filters it by one or more of the following columns: DEPARTMENT_ID, JOB_ID, MANAGER_ID and LAST_NAME.
For example:
- in one execution we may want to get all the employees with DEPARTMENT_ID = 80
- in a second execution all the employees that their LAST_NAME is King
- in a third execution all the employees that their JOB_ID is ST_CLERK and their MANAGER_ID is 124
- and so on
These columns are indexed, each one in a separate index:
SQL> select index_name, 2 listagg(column_name, ',') within group(order by column_position) index_columns 3 from user_ind_columns 4 where table_name = 'EMPLOYEES' 5 group by index_name; INDEX_NAME INDEX_COLUMNS -------------------- -------------------- EMP_DEPARTMENT_IX DEPARTMENT_ID EMP_EMAIL_UK EMAIL EMP_EMP_ID_PK EMPLOYEE_ID EMP_JOB_IX JOB_ID EMP_MANAGER_IX MANAGER_ID EMP_NAME_IX LAST_NAME,FIRST_NAME 6 rows selected.
Many Queries, Many Indexes
We can write 15 different queries – a query for every possible combination. Continue reading “A Single Query with Many Filter Combinations”