doudun5009 2015-05-09 22:20
浏览 57
已采纳

如何在高负载下优化mysql查询的性能?

I'm a bit confused about how to optimize my SQL queries. I have a mid-complex query with some joins to run a hundred to thousand times per second (two tables on SSD, one table in RAM, 4 JOINs)

How can I minimize the overhead for execution? Is there any way to precompile a query so that MySQL does not need to analyze, optimize and compile the query each single time?

I know I can use a prepared statement to precompile a query that is then being executed multiple times in the same session. But what to do if you have multiple sessions and only one query per session? Are prepared statements being cached across different sessions? I don't think so.

Then I thought that stored procedures were the best way, because they're said to be precompiled. Now I read that this assumption is totally wrong, they are in fact not precompiled.

Is the any way to share client-sessions in MySQL, e.g. to use prepared statements in the first session inherited by the following sessions?

Last idea is to write a multithreaded socket server to act like a MySQL client-proxy. But that seems a little bit exaggerative to me. ;-)

I use PHP as Apache2 module. Is there any chance to "store" the MySQL session in shared memory, so that the following HTTP requests can use the existing MySQL session instead of starting a new one? So that I can use prepared statements across different HTTP requests?

  • 写回答

3条回答 默认 最新

  • drtiwd06558 2015-05-09 23:33
    关注

    Q: Is there a way to "re-use" a MySQL connection so that a subsequent request can make use an existing connection?

    A: Yes. You can use a connection pool implementation. This is a familiar pattern with Java, with several implementations available.

    For a connection pool implementation in PHP, you can use the PHP extension mysqldnd-ms.

    Reference: http://php.net/manual/en/mysqlnd-ms.pooling.php

    NOTE: I don't have personal experience with this PHP extension.


    Some of the other questions you asked...

    Q: How can I minimize the overhead for execution? Is there any way to precompile a query so that mysql does not need to analyze, optimize and compile the query each single time?

    A: In MySQL 5.6, you can use server side prepared statements. The execution plan for the prepared statement is cached in the session, so a repeated call to the same SQL statement can re-use the previously prepared execution plan. (This feature is not available in MySQL versions before 5.6.)

    Reducing the amount of "connection churning** will reduce MySQL overhead. Connecting and disconnecting from the database server is work that the server has to do. It's simple enough to test and compare performance. In one process, open a connection, and do some repeated work (repeated execute a simple statement, like SELECT NOW(), and then disconnect. In another process, run the same repeated executions of the SELECT, but connect and disconnect for each execution.

    Q: Are prepared statements being cached across different session?

    A: No. Statements are cached at the statement session level.

    Q: Is the any way to share client-sessions in mysql, e.g. to use prepared statements in the first session inherited by the following sessions?

    A: No. The only way you're going to get that to happen is to not disconnect from the database, and pass the handle of that session to a subsequent client that requests a connection. And we get that to happen by implementing a connection pool.


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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题