这几天对sql进行优化,发现在临时表对大量数据量很好帮助,特别是复杂的sql语句,特别是有多很多子查询存在,如果我们使用select * from (select * from big_table1 where field =condition ) minus select * from (select * from big_table2 where field =condition )这样子查询,在查询操作过程中,会使数据查询会慢很多,当我们建立两个临时表分别存放select * from big_table1 where field =condition 和select * from big_table2 where field =condition ,再用两个临时表来进行minus操作,这样的操作速度会快很多,但这样一个问题就是要在写sql时要新有对应的临时表,这样对很多的不同的sql语句说是很不适用,在oracle里提供了with这个好东西,让我们省了建立临时表,我们可以用with这样写sql语句 with query1 as( select * from big_table1 where field =condition ) ,query2 as(select * from big_table2 where field =condition) select * from query1 minus select * from query2来操作,这样我们就可以省去了临时表,不过这样sql语句在我们使用ado来操作时,就会无效,这个是比较麻烦的事,所以我们在写好的sql语句后select * from (with query1 as( select * from big_table1 where field =condition ) ,query2 as(select * from big_table2 where field =condition) select * from query1 minus select * from query2h),这样就可以解决了ado不识别with为前缀的sql语句了。
oracle 里的with的用处
最新推荐文章于 2025-05-08 19:01:03 发布