gpt4 book ai didi

mysql - mysql查询是否缓存动态计算的列

转载 作者:行者123 更新时间:2023-11-29 00:26:37 25 4
gpt4 key购买 nike

我有一个 mysql 查询:

SELECT my_table.* WHERE SOUNDEX(my_col)='whatever' OR SUBSTR(SOUNDEX(my_col),4)='whatever' ORDER BY SUBSTR(SOUNDEX(my_col),4)='whatever',SOUNDEX(my_col)='whatever'

substring 函数和 soundex 函数实际会被调用多少次?我的意思是对于完全相同的输入,mysql 会在一个查询的范围内缓存结果吗?

如果不是,我如何更改查询以使每个函数的调用次数尽可能少。

最佳答案

MySQL 会为每个返回的行调用此函数四次,为了避免这种情况,您可以使用子查询,所以不用

  SELECT * 
FROM song
ORDER BY Substr(pre_calculated_soundex, 1, 1) =
Substr(Soundex("aaaa"), 1, 1)
+ Substr(pre_calculated_soundex
, 2, 1) =
Substr
(Soundex("aaaa"), 2, 1)
+ Substr(pre_calculated_soundex, 3, 1)
= Substr(Soundex("aaaa"), 3, 1)
+ Substr(pre_calculated_soundex, 4, 1
)
= Substr(Soundex("aaaa"), 4, 1)

你可以做到

SELECT *  from (select *, Soundex("aaaa") as current_soundex from song)
ORDER BY
Substr(pre_calculated_soundex, 1, 1) = Substr(current_soundex , 1, 1)
+ Substr(pre_calculated_soundex, 2, 1) = Substr(current_soundex , 2, 1)
+ Substr(pre_calculated_soundex, 3, 1) = Substr(current_soundex , 3, 1)
+ Substr(pre_calculated_soundex, 4, 1) = Substr(current_soundex , 4, 1)

关于mysql - mysql查询是否缓存动态计算的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18616924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com