简体   繁体   中英

Using query hints to use a index in an inner table

I have a query which uses the view a as follows and the query is extremely slow.

select *
from a
where a.id = 1 and a.name = 'Ann';

The view a is made up another four views b,c,d,e.

select b.id, c.name, c.age, e.town
from b,c,d,e
where c.name = b.name AND c.id = d.id AND d.name = e.name;

I have created an index on the table of c named c_test and I need to use it when executing the first query.

Is this possible?

Are you really using this deprecated 1980s join syntax? You shouldn't. Use proper explicit joins ( INNER JOIN in your case).

You are joining the two tables C and D on their IDs. That should mean they are 1:1 related. If not, "ID" is a misnomer, because an ID is supposed to identify a row.

Now let's look at the access route: You have the ID from table B and the name from tables B and C. We can tell from the column name that b.id is unique and Oracle guarantees this with a unique index, if the database is set up properly.

This means the DBMS will look for the B row with ID 1, find it instantly in the index, find the row instantly in the table, see the name and see whether it matches 'Ann'.

The only thing that can be slow hence is joining C, D, and E. Joining on unique IDs is extremely fast. Joining on (non-unigue?) names is only fast, if you provide indexes on the names. I'd recommend the following indexes accordingly:

create index idx_c on c (name);
create index idx_e on e (name);

To get this faster still, use covering indexes instead:

create index idx_b on b (id, name);
create index idx_c on c (name, id, age);
create index idx_d on d (id, name);
create index idx_e on e (name, town);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM