是否可能在PHP / Mysql / APC / Memcache中存储PDO准备语句以供重用?

时间:2021-08-06 12:02:17

... if so, would it be faster? My use case is a typical LAMP stack that is hosting a REST API. This API is structured in a manner so that I have 10 ( will grow to approx. 50) different queries that will be run with different inputs, and I expect the frequency to be very rapid. I am specifically not asking about result caching of the queries, as I understand enough of that to pursue it separately. I am specifically concerned with the fact that 95% of the application logic will be client side JS, and the high volume of tiny REST requests which will mostly be doing small queries and returning them to the browser to deal with will end up doing a lot of redundant work for each request. If I can use persistant connections, and check APC or memcache for the PDO prepared statement, and re-use it, I would expect to reduce the apache server to mysql server overhead substantially.
I see http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html will likely occur in my use case as well, but it would still have the prepare statement being sent for each request.

…如果是的话,会更快吗?我的用例是一个典型的LAMP堆栈,它托管一个REST API。这个API的结构使我有10个(将增长到大约10个)。不同的查询将使用不同的输入来运行,我预计频率会非常快。我并不是特别询问查询的结果缓存,因为我已经理解了这一点,可以单独进行查询。我特别关心这样一个事实,95%的应用程序逻辑将客户端JS和高体积微小的REST请求将主要做小型查询并返回给浏览器处理最终将为每个请求做了很多多余的工作。如果我可以使用持久连接,并检查APC或memcache以获取PDO准备好的语句,并重新使用它,那么我希望将apache服务器大幅减少到mysql服务器开销。我看到http://dev.mysql.com/doc/refman/5.1/ en/querycache-oper.html也可能出现在我的用例中,但是它仍然会为每个请求发送准备语句。

1 个解决方案

#1


4  

Nope.
That's interesting question and I spent some good time investigating it.
There is no way to pass a prepared statement between calls.

不。这是个有趣的问题,我花了一些时间研究它。在调用之间没有传递准备好的语句的方法。

And, honestly, speed gain is not that great to talk about.

老实说,速度增益并不是很好谈论。

If you're concerned in performance - turn to the queries, not to mere a driver.
It's queries that affect performance, not the way they re called.

如果您关心性能,请转向查询,而不仅仅是驱动程序。它是影响性能的查询,而不是它们被调用的方式。

errr..
after reading your question more throughly, I didn't change my mind, but there are some things to note

哦. .读了你的问题之后,我并没有改变主意,但是有一些事情需要注意

high volume of tiny REST requests which will mostly be doing small queries and returning them to the browser to deal with will end up doing a lot of redundant work for each request.

大量的小的REST请求,主要是执行小的查询,并将它们返回给浏览器处理,最终会为每个请求做大量的冗余工作。

That's right.
So, consider to reduce that number by sending requests in batches and requesting more info in one. Not because of negligible difference in prepared statements but because of considerable network latency.

这是正确的。因此,考虑通过批量发送请求并请求更多信息来减少这个数字。不是因为准备好的语句的差异可以忽略不计,而是因为相当大的网络延迟。

I would expect to reduce the apache server to mysql server overhead substantially.

我希望将apache服务器的开销大大降低到mysql服务器的开销。

And this one is not.
Looks like you're taking prepared statements wrong and confusing them with query cache.
Even if you manage to get prepared statements to persist between requests, it won't affect whatever apache to mysql interchange - you need to execute every subsequent call to prepared statement, means send request to mysql server. So, the only thing you save on is just query parsing which is blazingly fast nowadays. I mean unnoticeable fast.

而这个不是。看起来您正在错误地处理准备好的语句,并将它们与查询缓存混淆。即使您设法使准备语句在请求之间持久存在,它也不会影响任何apache到mysql交换——您需要执行对准备语句的每个后续调用,这意味着向mysql服务器发送请求。所以,您唯一节省的就是查询解析,它现在的速度非常快。我的意思是不显明的快。

#1


4  

Nope.
That's interesting question and I spent some good time investigating it.
There is no way to pass a prepared statement between calls.

不。这是个有趣的问题,我花了一些时间研究它。在调用之间没有传递准备好的语句的方法。

And, honestly, speed gain is not that great to talk about.

老实说,速度增益并不是很好谈论。

If you're concerned in performance - turn to the queries, not to mere a driver.
It's queries that affect performance, not the way they re called.

如果您关心性能,请转向查询,而不仅仅是驱动程序。它是影响性能的查询,而不是它们被调用的方式。

errr..
after reading your question more throughly, I didn't change my mind, but there are some things to note

哦. .读了你的问题之后,我并没有改变主意,但是有一些事情需要注意

high volume of tiny REST requests which will mostly be doing small queries and returning them to the browser to deal with will end up doing a lot of redundant work for each request.

大量的小的REST请求,主要是执行小的查询,并将它们返回给浏览器处理,最终会为每个请求做大量的冗余工作。

That's right.
So, consider to reduce that number by sending requests in batches and requesting more info in one. Not because of negligible difference in prepared statements but because of considerable network latency.

这是正确的。因此,考虑通过批量发送请求并请求更多信息来减少这个数字。不是因为准备好的语句的差异可以忽略不计,而是因为相当大的网络延迟。

I would expect to reduce the apache server to mysql server overhead substantially.

我希望将apache服务器的开销大大降低到mysql服务器的开销。

And this one is not.
Looks like you're taking prepared statements wrong and confusing them with query cache.
Even if you manage to get prepared statements to persist between requests, it won't affect whatever apache to mysql interchange - you need to execute every subsequent call to prepared statement, means send request to mysql server. So, the only thing you save on is just query parsing which is blazingly fast nowadays. I mean unnoticeable fast.

而这个不是。看起来您正在错误地处理准备好的语句,并将它们与查询缓存混淆。即使您设法使准备语句在请求之间持久存在,它也不会影响任何apache到mysql交换——您需要执行对准备语句的每个后续调用,这意味着向mysql服务器发送请求。所以,您唯一节省的就是查询解析,它现在的速度非常快。我的意思是不显明的快。