dongzi0850 2013-03-03 13:47
浏览 196
已采纳

有没有办法在PHP中保持与mysql数据库的连接?

Im trying to optimize some of my PHP code. I found out that most time of my PHP script is spent during the connection to my mysql database at the beginning of the script.

I only connect to the database once at the beginning and close the database connection at the end of the script.

But for each user requesting this page a new connection has to be established.

Is there a way to hold a reference to the database and share it for all requests?

  • 写回答

2条回答 默认 最新

  • dongna9185 2013-03-03 14:02
    关注

    You can make your connection persistent (for PDO use PDO::ATTR_PERSISTENT => true), but my recomandation is to find out why your script is spending a lot of time connecting to mysql and make some improvments there.

    Consider this:

    • Use MySQL server IP instead of hostname, to eliminate time needed to resolve dns.
    • Disable mysql autocompletition: (comment skip-auto-rehash from my.cnf)
    • If you make a lot of queries which will return the same thing over and over again, consider using a caching system to cache for query results.
    • Post your code on codereview.stackexchange.com to benefit from others expertise on further improving your code.
    • To further debug the problem, connect from console to MySQL server and check the time needed to open connection, change database, select, etc.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭