MySQL缓存和日期函数 [英] MySQL cache and date functions

查看:85
本文介绍了MySQL缓存和日期函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在一个性能博客中读到,最好使用PHP的日期函数在MySQL查询中设置日期,而不是使用像curdate()这样的mysql日期函数,因为mysql可以缓存查询或结果或类似的东西.有人对此有见识吗?它可以盛水还是毫无根据?

I once read in a performance blog that it is better to use PHP's date functions to set dates in a MySQL query instead of using mysql date functions like curdate() because mysql can then cache the query or the result or something like that. Does anyone have any insight into this? Does it hold any water or is it baseless?

示例:

$query = 'SELECT id FROM table WHERE publish_date = \''.date('Y-m-d').'\'';

vs

$query = 'SELECT id FROM table WHERE publish_date = CURDATE()';

推荐答案

任何包含CURDATE()的函数都不会被缓存. 来源

Any function containing CURDATE() will not be cached. Source

据我所知,应该仍然对日期进行硬编码.尽管您可能想考虑使用 prepare 功能而不是拼接字符串进入您的查询(出于安全性考虑).

Hardcoding the date should still be cached as far as I can tell. Though you might want to consider using the prepare functionality instead of splicing strings into your query (for sanity and security sake).

这篇关于MySQL缓存和日期函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆