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

标签 mysql optimization

我有一个 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/

相关文章:

mysql - "Cascade"外键通过多个表?

MYSQL 创建表语句给我 "non-unique"错误?

java - 如何在JComboBox中获取mysql表

MySQL 时间戳仅在创建时

上证所 SIMD 的上限/下限

mysql - 从给定位置的最近位置查找司机

c++ - 获得完整整数乘法的高半部分和低半部分

python - 如何访问从 scipy.optimize.basinhopping (不是最小化)返回的 OptimizeResult 实例的 `success` 属性?

c++ - 大整数值会导致 C++ 中的性能下降?

perl - 我如何在执行的各个阶段进行 Perl CGI 性能测量、基准测试、时间测量?