简体   繁体   English

服务器上的PHP脚本比WAMP本地主机上的PHP脚本慢得多

[英]PHP script much slower on server than on WAMP localhost

So as I've stated above: I have a quite big, but quite easy script, which gets a single json file from a website, decodes it, then saves the data in a PostgreSQL database. 因此,如上所述,我有一个很大但很简单的脚本,该脚本从网站获取一个json文件,对其进行解码,然后将数据保存在PostgreSQL数据库中。 It takes about 4 to 5 minutes to completely finish (about 300 000 records) on my computer (i3M CPU, laptop) but takes a about 10-15 times longer to do the same thing on the server which I've just rented. 在我的计算机(i3M CPU,笔记本电脑)上完全完成(大约300 000条记录)大约需要4到5分钟,但是在我刚刚租用的服务器上完成同一操作大约需要10到15倍的时间。

The dedicated server has an intel Xeon quad (3Ghz) CPU, and on an overall level much better specifications with much better internet access, so I'm pretty sure it has nothing to do with that. 专用服务器具有英特尔至强四核(3Ghz)CPU,并且总体上具有更好的规格和更好的Internet访问,因此,我敢肯定与它无关。 It runs the latest Debian, Apache/2.4.10, PHP Version 5.6.22-0, PostgreSQL 9.5. 它运行最新的Debian,Apache / 2.4.10,PHP版本5.6.22-0,PostgreSQL 9.5。
I've tried to copy the settings and modules from the WAMP settings, figured it my help. 我试图从WAMP设置中复制设置和模块,以得到我的帮助。 Unfortunately, it didn't. 不幸的是,事实并非如此。 Not sure what information might help in solving this problem, but I'm more than happy to answer to any of the questions. 不知道什么信息可以帮助解决这个问题,但是我很乐意回答任何问题。
I am almost positive it has something to do with some option which I must have skipped, so any help would be much appreciated. 我几乎可以肯定,它与某些必须跳过的选项有关,因此,我们将不胜感激。

PS: WAMP uses: 2.4.17 Apache, 5.6.16 PHP, 9.5 PostgreSQL. PS:WAMP使用:2.4.17 Apache,5.6.16 PHP,9.5 PostgreSQL。

Performance-Issues can be caused by much things. 性能问题可能是由很多原因引起的。

  • First of all, i'm quite sure (?) your local PC has a SSD Disk, while the Server might run on "default SAS-Drives". 首先,我非常确定(?)您的本地PC上有SSD磁盘,而服务器可能在“默认SAS驱动器”上运行。 This can make a huge difference, when it comes down to writing / reading stuff to the hdd. 当涉及到向硬盘写入/读取内容时,这可能会产生巨大的变化。 Especially for "Random-IO" (Ie much Selects with conditions and a to "small" dbbuffer) 特别是对于“ Random-IO”(即,根据条件选择,并且要有一个“较小”的dbbuffer)
  • Do you have a "root" server, or a rented VM? 您是否有“根”服务器或租用的VM? If it's a VM keep in mind, that you share your resources with other vms as well. 如果是虚拟机,请记住,您也要与其他虚拟机共享资源。 (especially the access on the SAS-Disks, but also CPU-Time) (尤其是对SAS磁盘的访问,还包括CPU时间)

You should write some Scripts to "identify" the bottleneck: 您应该编写一些脚本来“识别”瓶颈:

  • Generate a PHP Script which uses as much CPU-Power as possible, add a few million iterations, compare the results. 生成一个使用尽可能多的CPU能力的PHP脚本,添加数百万次迭代,比较结果。 (Guess this is not the problem) (猜想这不是问题)
  • Generate a PHP Scirpt, that uses massive DISK-IO - compare the results for thausands of iterations. 生成一个使用大量DISK-IO的PHP Scirpt,比较大量迭代的结果。
  • Generate a PHP script, that uses a massive amount of memory - compare again. 生成一个使用大量内存的PHP脚本-再次比较。 (Be carefull on this - using tooo much memory will cause data be written to the harddisk which "destroys" the result) (请谨慎操作-使用过多的内存将导致数据写入硬盘,从而“破坏”结果)

if you didn't encounter a (unexpected) difference by now, you have eliminated hardware-issues. 如果您现在没有遇到(意外的)差异,那么您已经消除了硬件问题。 - Repeat the task for heavy database loads to figure out if the database might be missconfigured. -对繁重的数据库负载重复执行此任务,以弄清数据库是否配置错误。 Sometimes it is just a simple "Boolean" flag, that might have heavy performance-impacts. 有时,它只是一个简单的“布尔”标志,可能会严重影响性能。

To expand on dognose's answer, I find that optimizing your DB access can make a big difference in performance. 为了进一步了解Dognose的答案,我发现优化数据库访问可以在性能上产生很大的不同。

It might be interesting to see what happens to the run time if you comment out the DB queries. 注释掉数据库查询后,看看运行时会发生什么可能会很有趣。 This will tell you how much of the run time is spent on the DB. 这将告诉您在数据库上花费了多少运行时间。

If the DB is taking significant time, try batching your requests. 如果数据库花费大量时间,请尝试分批处理您的请求。 Instead of sending a single insert at a time to the DB, try holding them in a variable and sending over 50 to 100 inserts (or more) in a batch. 尝试一次将它们保存在变量中,然后批量发送50至100个(或更多)插入,而不是一次将单个插入发送到DB。 Depending on how you setup your DB connection, there could be significant overhead for each request. 根据您设置数据库连接的方式,每个请求可能会有大量开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM