dongxi2163 2014-05-10 17:00
浏览 122
已采纳

持久的mysql连接的优点

On my website, when an user opens his profile or any other page, (almost all pages use data from mysql), my website makes around 50 connections to mysql in loading the page. This is the case on my development server, running elementary OS.

When I came to know about persistent connections in mysql, I was puzzled. If I am to run this website on a VPS (with low RAM in starting), and considering the overhead produced by a large number of mysql connections, will using persistent connections improve my website's performance?

Currently, I start and end a connection in every function. Is there a better way to connect to mysql?

And, taking into account that if 100 users are using my website simultaneously, what will be the performance if each page makes around 50-60 connections?

  • 写回答

2条回答 默认 最新

  • dongpu9481 2014-05-10 18:44
    关注

    You asked so I will answer. You are doing this incorrectly. You should start the processing on each page request (each externally-accessible .php file) by using a common function to establish a single database connection, then you should reuse that connection.

    You are getting away with this because you're probably using an automatic connection pool built in to your php database-access library, and because you have not yet scaled up your application.

    You won't be able to scale this up very far using this multiconnection strategy because it will perform very badly indeed when you add users.

    There are lots of examples of working open-source php-based web app systems you can look at. WordPress is an example. You'll find that most of them start by opening a database connection and storing its handle in a global variable.

    You asked:

    According to CBroe's comment, I changed my strategy. Actually, I am using multiple database connections, but the functions are same (don't ask why lol). So if I open connections on start, and then pass the handler to the function, will that be an improvement?

    Yes, that will be fine. You need to avoid churning connections to get best performance.

    If you need connections to more than one different database, you can open them all. But it sounds like you need to hit just one database.

    PHP doesn't have significant overhead when passing a handler to a function, so don't worry about that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3