php - 在 App Engine for PHP 中优化 MySQL 连接的推荐方法是什么?

标签 php mysql sql google-app-engine google-cloud-sql

我正在为一个移动应用程序构建一个非常传统的 PHP/MySQL 后端,并且在过去我一直因为没有正确关闭 MySQL 连接而受到重创...

如果我使用 mysqli_connect() 将基于 App Engine 的 PHP 连接到 Google Cloud SQL,如果用户 session 可以,我如何确保将与我的 Cloud SQL 实例的同时连接数保持在最低限度会很长吗?

我读到 PHP 5.3+ 支持与 mysqli_connect 的持久连接,只要您在主机名前加上“p:”,但 Cloud SQL 在从 App 连接时根本不采用任何主机名引擎...

最佳答案

您可以在 mysql_connect 的第一个参数前加上 p:

但是您必须检查打开/关闭连接的时间与“数据”查询时间相比是否重要,如 google gloud SQL FAQ 中所述:

How to best manage your database connections depends on your use case. For example, if the time to create a new database connection is greater than checking and reusing an existing connection, then we recommend you use connection pooling. Conversely, if the time to create a new connection is about the same as testing if an existing connection is alive and reusing it, then we recommend that you create a new connection to service each HTTP request, and reuse it for the duration of the request. In particular, the latter case may apply when you connect from Google App Engine to Google Cloud SQL.

为了保持较低的 SQL 连接数量,您必须广泛使用缓存(例如使用 memcached)并避免在不需要时进行连接(即不要在标题中创建连接)如果你不需要它,你的页面)。

关于php - 在 App Engine for PHP 中优化 MySQL 连接的推荐方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24879247/

相关文章:

php - 使用mySql根据条件检索特定值

mysql - rails 3;事件记录;在哪里;数据库中两列之间的 NOT EQUAL 条件比较

mysql - 所有数据未从 CSV 加载到 MYSQL

java - SQL AVG() 函数在多列时返回 INT

mysql - 使用 SQL 汇总 2 个不同表中的 3 列

sql - 数据库表中的键/值对

php - 在 Doctrine2 中将字段聚合到获取的对象

java - PHP 等同于 Java 的 toCharArray()

php - ajax,语法错误: unexpected token A in json at position 0

mysql - 如何让三重自连接正常工作