我正在运行一个非常长的查询,如下所述。 它会为自动化系统上的每个帐户提取下一个需要执行的操作。
即
SELECT Account.id,
(IFNULL(**Should send message query**,
IFNULL(**Should check inbox**, NULL))) as nextTask FROM Account
实际上,IFNULL的字符串大约为10,每个都是相当复杂的子查询。
我想知道如果满足第一个IFNULL表达式,MySQL是否会计算以下IFNULL表达式的值。也就是说,如果一个帐户应该发送一条消息,它就不必为应检查收件箱
而计算子查询这是MySQL的工作方式吗?
与CASE WHEN的区别是什么
例如
CASE WHEN **Should send message** THEN **Should send message**
WHEN **Should check inbox** THEN **Should check inbox**
END
我只想降低此查询的CPU使用率。
答案 0 :(得分:1)
返回列表中的第一个非NULL值,如果没有则返回NULL 非NULL值。
因此它将是:
SELECT
Account.id,
COALESCE(
**Should send message query**,
**Should check inbox**
) as nextTask
FROM Account
现在是您的实际问题
我想知道MySQL是否将计算以下[IFNULL]的值 如果满足第一个条件则为表达式。
引擎没有理由这样做。您可以使用以下脚本对其进行测试:
set @executed1 = 'no';
set @executed2 = 'no';
select coalesce(
@executed1 := 'yes', -- evaluates to non null
@executed2 := 'yes'
);
select @executed1, @executed2;
结果:
@executed1 | @executed2
yes | no
如您所见,第二个表达式未执行,因为第一个表达式已被评估为非NULL值。
set @executed1 = 'no';
set @executed2 = 'no';
select coalesce(
nullif(@executed1 := 'yes', 'yes'), -- evaluates to null
@executed2 := 'yes'
);
select @executed1, @executed2;
结果:
@executed1 | @executed2
yes | yes
这两个表达式都已执行,因为第一个表达式的计算结果为NULL。
我会说-IFNULL
也是如此。但是我不会用。至少不是您这种情况。