MySQL会自动优化我的重复查询吗?

时间:2022-02-23 01:08:19

I noticed something peculiar when testing query speeds on my database. I have two queries.

当我在数据库上测试查询速度时,我注意到一些奇怪的事情。我有两个查询。

SELECT * 
FROM  `event_history` 
WHERE  `event_details` LIKE  '%joe submitted order%'

and

SELECT * 
FROM  `event_history` 
WHERE  `event_details` LIKE  '%bob submitted order%'

The first one is run frequently by the application. The second one is something I just ran manually once. I noticed the first one ran only 0.0035 seconds, but the second one took about 0.2500 seconds. This is unexpected.

第一个应用程序经常运行。第二个是我刚才手动运行的。我注意到第一个只跑了0.0035秒,而第二个只跑了0.2500秒。这是意想不到的。

Is MySQL optimizing this query somehow? If so, how can I config this, say to make the second query optimized before I put it into production?

MySQL是否以某种方式优化这个查询?如果是,我如何配置它,比如在将第二个查询放入生产环境之前使其优化?

1 个解决方案

#1


4  

MySQL implements a query result cache. That could certainly explain what you see, since you report that an often run query returns faster than a query you execute for the first time.

MySQL实现了查询结果缓存。这当然可以解释您看到的情况,因为您报告说,一个经常运行的查询返回的速度比您第一次执行的查询快。

Take a look here for the details: http://dev.mysql.com/doc/refman/5.7/en/query-cache.html

在这里查看详细信息:http://dev.mysql.com/doc/refman/5.7/en/querycache.html

#1


4  

MySQL implements a query result cache. That could certainly explain what you see, since you report that an often run query returns faster than a query you execute for the first time.

MySQL实现了查询结果缓存。这当然可以解释您看到的情况,因为您报告说,一个经常运行的查询返回的速度比您第一次执行的查询快。

Take a look here for the details: http://dev.mysql.com/doc/refman/5.7/en/query-cache.html

在这里查看详细信息:http://dev.mysql.com/doc/refman/5.7/en/querycache.html