22,209
社区成员
发帖
与我相关
我的任务
分享
with t(id,Result) as
(
select 1,'2.2' union all
select 2,'3.3' union all
select 3,'2...2'
)
select * into #A from t;
select * from #A where cast(Result as decimal(18,5))>3 and id in(1,2) --谓词里:先执行cast(Result as decimal(18,5))>3,后执行id in(1,2)。报错
select * from #A where id in(1,2) and cast(Result as decimal(18,5))>3 --因为,in等级靠后,即使写前面,谓词里也会排后面,顺序同上。报错
select * from #A where cast(Result as decimal(18,5))>3 and id <=2 --">" 与 "<="同级,但有函数的等级估计比没函数的等级低,估计不报错
select * from #A where id <=2 and cast(Result as decimal(18,5))>3 --百分百不报错
;
with t(id,Result) as
(
select 1,'2.2' union all
select 2,'3.3' union all
select 3,'2...2'
)
select * from t where id =2 and cast(Result as decimal(18,5))>3 --都等号了,优化器还把它的谓词顺序调到后面,这是为啥?
-- Create table
create table TEST_ARAY
(
a NUMBER(1),
b VARCHAR2(10)
);
-- Create/Recreate indexes
create bitmap index IDX_A on TEST_ARAY (A);
create index IDX_B on TEST_ARAY (B);
with t(id,Result) as
(
select 1,'2.2' union all
select 2,'3.3' union all
select 3,'2...2'
)
select * from t where id =2 and cast(Result as decimal(18,5))>3
with t(id,Result) as
(
select 1,'2.2' union all
select 2,'3.3' union all
select 3,'2...2'
)
select * into #a from t
select * from #a where id =2 and cast(Result as decimal(18,5))>3
按你的理论 这么执行也会报错 但是实际上是通过的with t(id,Result) as
(
select 1,'2.2' union all
select 2,'3.3' union all
select 3,'2...2'
)
select * into #A from t;
--1.
SELECT * from #A where id*1 <=2 and cast(Result as decimal(18,5))>3
--2.
select * from #A where CAST(id AS INT) <=2 and cast(Result as decimal(9,5))>3
--3.
select * from #A where CAST(id AS DECIMAL(9,5)) <=2 and cast(Result as decimal(9,5))>3
--4.
select * from #A WHERE CAST(Result as decimal(9,5))>3 AND CAST(id AS DECIMAL(9,5)) <=2
--要掌握用以上方法动手去测
--以上的开销成本相同的,所以不存在成本开销问题,条件是否报错的取决于表达式的运算优先级(语句1/2),当表达式相同时取决于条件先后(3/4)
--分析器运算,
--1.分析器会把运算符号列id*1低于转换类型列cast(Result as decimal(18,5))
--2.转换类型列cast(Result as decimal(18,5))低于id不需要转换列
谓词里,靠后的条件,只会遍历靠前的条件筛选出的结果集,而不会遍历全表了
你现在的反问,都是在针对这个,但你举例都是用where,而不是谓词,你看了每句的谓词了没?
我想说的是这个结论,到我目前的测试语句中都成立,还没有发现不成立的。
所以,我现在要做的事,如何优化,才能上谓词的顺序,随我的要求变化
,即我知道了某些优先级,写where时就能控制谓词顺序。
但with打破了我前面总结的优先级 1.Equal谓词2.Range谓词3.In列表4.Like谓词
所以,才发问,想知道有with 时,有啥优先级。