duanlinpi0265 2013-01-15 07:51
浏览 26
已采纳

使用cron时MySQL的性能差异

I have a number of PHP maintenance scripts running on a shared hosting environment using cPanel. Most of the scripts need to run every 3-4 hours and to simplify their execution, I wrote a scheduler script that checks which (if any) of these scripts needs to be run and executes them as necessary. I set the scheduler script to run every 5 minutes in cron. If the script finds no maintenance tasks are currently due, it does nothing. The overhead of invoking the scheduler results in about 5 short SQL queries being executed (so these are performed every 5 minutes).

Everything was working fine until my host complained about high MySQL usage, claiming I was far over the allowed limit. After disabling the scheduler cron job, my resource usage went down to 0.

  • Before disabling cron job: Number of MySQL procs (average) - 0.97
  • After disabling cron job: Number of MySQL procs (average) - 0.00

The strange thing is that after removing the cron job, I still continued to activate the scheduler script manually through a browser every few hours. Since the script was still being run, I was very surprised by how drastically the average number of MySQL processes had fallen. Before disabling the cron job, I began logging how many SQL queries were executed by the scheduler and maintenance scripts.

  • Before disabling cron job: 9899 queries a day (average)
  • After disabling cron job: 9552 queries a day (average)

So when I called the scheduler manually, it still performed nearly as many SQL queries as it did as a cron, but somehow my MySQL usage still dropped to basically nothing.

Are there any performance-related differences between executing a PHP script via a cron job using the php command than with calling it through a browser? I do not explicitly close the database connection in my script since it was my understanding that this happens automatically at the end of execution. Is it possible this connection remains open when the script is run via cron? What other explanations could there be for the substantial performance differences when using cron?

  • 写回答

1条回答 默认 最新

  • douyan2680 2013-01-15 19:45
    关注

    Normally, two php.ini files are installed alongside PHP:

    • One for Apache
    • One for CLI (Command Line Interface)

    Typically, cron jobs will execute php -f yourfile.php, which uses the CLI php.ini file. Likewise, when you call the script over the network and through apache, it will use the apache php.ini file. Additionally, apache allows using php_value or php_flag in your virtual-host configuration or .htaccess files.

    You most probably have a configuration difference between apache and CLI.

    The first thing that pops into my mind would be persistent DB connections. Persistent connections will use a pool of connections to your DB server that will persist between requests i.e. be left open, in order to avoid TCP handshakes. This is a performance optimization but can top your MySQL server's connection limit.

    On my system (Ubuntu) the paths for the two php.ini files are as follows:

    • /etc/php5/apache2/php.ini
    • /etc/php5/cli/php.ini

    You can specify the php.ini file that you want to use when running php over the command line through the -c flag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法