example:
select a,b,c from
(select aaa a,bbb b,ccc c from table_1 where....
union
select eee a,fff b,ggg c from table_2 where....
union
...)
order by a
select a,b,c from
(select aaa a,bbb b,ccc c from table_1 where....
union
select eee a,fff b,ggg c from table_2 where....
union
...)
order by a
select 1 as 序号,bbb b,ccc c from table_1 where....
union
select 2 as 序号,fff b,ggg c from table_2 where....
union
..........效率不会降低的.
select a,b,c from
(select aaa a,bbb b,ccc c from table_1 where....
union
select eee a,fff b,ggg c from table_2 where....
union
...)
order by a;
那其实是进行了两次排序,第一次是对union进行group by,第二次是根据order进行排序。建议
1、用union all取代union,效率高很多
2、或者在select语句中根据order的顺序组织,就不需要进行第二次排序了。