Does mysql query cache the dynamically calculated columns

蹲街弑〆低调 提交于 2020-02-05 08:57:05

问题


I have a mysql query:

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'

How many times will the substring function and soundex functions will actually be called? I mean for exactly same inputs will mysql cache the results over the span of one query?

If not, how can I make the change in the query so that each function is called minimum times possible.


回答1:


MySQL would call this function four times for every returned row, to avoid this you can use a subquery, so instead of

  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) 

You can do

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) 


来源:https://stackoverflow.com/questions/18616924/does-mysql-query-cache-the-dynamically-calculated-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!