Home > database >  In SQL, using union replaced or, will bring benefits, this is right or wrong?
In SQL, using union replaced or, will bring benefits, this is right or wrong?

Time:10-09

In SQL, using union replaced or, will bring benefits, this is right or wrong? Whether, for example

CodePudding user response:

This error, also have to see the scene, in general, or affect the SQL statements go right index will consider using the union rewrite the SQL, but sometimes it is not like that, since the or no problem, why do you use the union rewrite?

CodePudding user response:

this is right or wrong statement this is too dogmatic

CodePudding user response:

That's a bullshit, said this was meant to or will lead to not go index, to the union can walk index, but the actual use of time will have a variety of situations,

Oracle11g test

 create table my_big_table as the select level a, cast (' b '| | level as varchar2 (8)) b from dual connect by level & lt;=200000; 
The create index idx_my_big_table on my_big_table (a, b);

The following two queries are equivalent, but will not go index
 select * 
The from my_big_table t1
Where a t1. A=55654
The or t1. B='b55654';

Select *
The from my_big_table t1
Where a t1. A=55654
The union
Select *
The from my_big_table t1
Where a t1. B='b55654


Try again in a proper index
 create table my_big_table2 as the select level a, cast (' b '| | level as varchar2 (8)) b from dual connect by level & lt;=200000; 
The create index idx_my_big_table2_1 on my_big_table2 (a);
The create index idx_my_big_table2_2 on my_big_table2 (b);

 


And you can see the optimizer self-assertion give turned bitmap,

With a hint for compulsory modify execution plan

Can be seen from the execution plan does to the union more efficient, but the optimizer will automatically help you to do this step, and it is according to the statistics do intelligent judgment, no manual to the union,

Another group of data of the exotic
 create table my_big_table3 as select mod (level 2), a, cast (' b '| | level as varchar2 (8)) b from dual connect by level & lt;=200000; 
The create index idx_my_big_table3_1 on my_big_table3 (a);
The create index idx_my_big_table3_2 on my_big_table3 (b);


Select * from my_big_table3 t1 where t1. A=1 or t1. B='b1235'; Or just take t1. A=1 this condition, you can't do it again walk index,

Again with my work just write a SQL for example
 
Select *
The from aaa. Tab1 t1
Where a t1. Aaaa_date='20200504'
And t1. Day_id=substr (' 20200504 ', 7, 2)
And t1. Aaaa_no='01'

And (
(# {levelType}='1')
Or (# {levelType}='2' and # {saleId}=t1, col1)
Or (# {levelType}='3' and # {saleId}=t1. Col2)
)

Aaa. Tab1 a partition table despite 40 w data, but I was to a full table query, why do you want to rewrite? Even if I only find a few data will not be changed, because there is no index on this table, if no index, all the above will not stand

CodePudding user response:

reference nayi_224 reply: 3/f
this is a bullshit, said this was meant to or will lead to not go index, to the union can walk index, but the actual use of time will have a variety of situations,

Oracle11g test

 create table my_big_table as the select level a, cast (' b '| | level as varchar2 (8)) b from dual connect by level & lt;=200000; 
The create index idx_my_big_table on my_big_table (a, b);

The following two queries are equivalent, but will not go index
 select * 
The from my_big_table t1
Where a t1. A=55654
The or t1. B='b55654';

Select *
The from my_big_table t1
Where a t1. A=55654
The union
Select *
The from my_big_table t1
Where a t1. B='b55654


Try again in a proper index
 create table my_big_table2 as the select level a, cast (' b '| | level as varchar2 (8)) b from dual connect by level & lt;=200000; 
The create index idx_my_big_table2_1 on my_big_table2 (a);
The create index idx_my_big_table2_2 on my_big_table2 (b);

 


And you can see the optimizer self-assertion give turned bitmap,

With a hint for compulsory modify execution plan

Can be seen from the execution plan does to the union more efficient, but the optimizer will automatically help you to do this step, and it is according to the statistics do intelligent judgment, no manual to the union,

Another group of data of the exotic
 create table my_big_table3 as select mod (level 2), a, cast (' b '| | level as varchar2 (8)) b from dual connect by level & lt;=200000; 
The create index idx_my_big_table3_1 on my_big_table3 (a);
The create index idx_my_big_table3_2 on my_big_table3 (b);


Select * from my_big_table3 t1 where t1. A=1 or t1. B='b1235'; Or just take t1. A=1 this condition, you can't do it again walk index,

Again with my work just write a SQL for example
 
Select *
The from aaa. Tab1 t1
Where a t1. Aaaa_date='20200504'
And t1. Day_id=substr (' 20200504 ', 7, 2)
And t1. Aaaa_no='01'

And (
(# {levelType}='1')
Or (# {levelType}='2' and # {saleId}=t1, col1)
Or (# {levelType}='3' and # {saleId}=t1. Col2)
)

Aaa. Tab1 a partition table despite 40 w data, but I was to a full table query, why do you want to rewrite? Even if I only find a few data will not be changed, because there is no index on this table, if no index, the above all can not stand

Just look from the code, the feeling is the union is twice a lookup table, and the or is a query table, just different query condition
 
Select *
The from my_big_table t1
Where a t1. A=55654
The or t1. B='b55654';

Select *
The from my_big_table t1
Where a t1. A=55654
The union
Select *
The from my_big_table t1
Where a t1. B='b55654'

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related