简体   繁体   English

共享主机上的PHP性能

[英]PHP performance on shared hosting

I have php script which calls another script with php exec function. 我有php脚本,用php exec函数调用另一个脚本。 The called script does batch process job, that is updating transaction statuses, notifies customers(puts emails to mail queue which runs separately). 被调用脚本执行批处理作业,即更新事务状态,通知客户(将电子邮件放入单独运行的邮件队列)。 So it will take 20-30 minutes due to a very large table(500000 rows), Now i am running it under my desktop windows machine and php uses up to 50% CPU, mysql 20% CPU. 所以由于一个非常大的表(500000行)需要20-30分钟,现在我在我的桌面Windows机器下运行它,php使用高达50%的CPU,mysql 20%CPU。 Is it normal practice ? 这是正常的做法吗? What if i put this script in a shared hosting ? 如果我把这个脚本放在共享主机中怎么办? Will i have trouble with that ? 我会遇到麻烦吗? It will not break the shared hosting rules ? 它不会破坏共享托管规则? The batch process can be started by a user anytime(normally one a month or can be more frequently). 批处理过程可以由用户随时启动(通常每月一次或更频繁)。

Any suggest on this ? 有什么建议吗?

Thanks for reading. 谢谢阅读。

Almost certainly, however it depends how much CPU power is available to the shared host (probably about as much as your desktop to be honest), in which case you'll see yourself booted off in short order. 几乎可以肯定的是,它取决于共享主机可用的CPU功率(可能与您的桌面大致相同),在这种情况下,您会看到自己在短时间内启动。 Shared hosts tend to be stack-em-high affairs, where you can get 100s of sites hosted. 共享主机往往是堆栈高级事务,您可以在其中获得100个托管的站点。

You could go for a virtual host, where they will give you freedom to do whatever you like, but you'll probably find there that the amount of CPU power you're restricted to is limited, possibly too limited for you. 你可以去一个虚拟主机,在那里他们可以自由地做任何你喜欢的事情,但你可能会发现你所限制的CPU功率是有限的,可能对你来说太有限了。 Virtual hosts tend to split the host between several virtual guests, sometimes as little as 4, but more often (and especially for the cheaper plans) all the way up to 64. 虚拟主机倾向于在几个虚拟客户机之间拆分主机,有时只有4个,但更频繁(尤其是对于更便宜的计划)一直到64个。

Your third option then is to go for a fully dedicated server, you get a whole server all you yourself and can do what you like with it. 那么你的第三个选择是去一个完全专用的服务器,你可以自己获得整个服务器,并且可以用你喜欢的方式做。 These tend to be expensive if you want lots of server hardware (because it uses a lot of very expensive electricity), or lots of bandwidth. 如果您需要大量的服务器硬件(因为它使用了大量非常昂贵的电力)或大量带宽,这些往往会很昂贵。

Obviously those 3 options increase in cost, in the webhosting arena, you do get what you pay for. 显然,这3个选项的成本增加,在网站主办领域,你确实得到你付出的代价。

You might like to find a webhosting forum and ask around on there - be sure to specify the CPU requirements, and how long its likely to take. 您可能希望找到一个虚拟主机论坛并在那里询问 - 确保指定CPU要求,以及可能需要多长时间。 You may get some hoster that'll be more than happy for you to run your script in the 'out of hours' if you agree a time with them (that doesn't interfere with backups, or if they have lower resource time, as websites tend to be accessed around the world in every timezone) 如果您同意与他们共度时间(不干扰备份,或者如果他们的资源时间较短,那么您可能会得到一些非常高兴您在“非工作时间”运行脚本的快乐网站往往遍布全球各个时区)

Generally shared hosts don't like you hogging their precious CPU power so I would not recommend you do anything very intensive on one. 通常共享主机不喜欢你占用他们宝贵的CPU能力,所以我不建议你做一些非常密集的事情。 if you are looking to perform batch jobs you should consider investing in a dedicated server, it would also be useful to have in general. 如果您希望执行批处理作业,您应该考虑投资专用服务器,一般情况下也是有用的。

I'm doing a similar thing with a Facebook application (php, 350,000 notifications). 我正在用Facebook应用程序(php,350,000通知)做类似的事情。 I did have 1 "alert" from my hosting company about CPU usage, but that "alert" came from sales, offering to work with me to upgrade. 我确实从我的托管公司获得了关于CPU使用率的1个“警报”,但“警报”来自销售,提供与我一起升级。

I changed the batch script to use less CPU at a time, essentially by spawning less simultaneous processes (10) and putting a sleep(10) command in a few spots. 我改变了批处理脚本以便一次使用更少的CPU,主要是通过产生较少的同时进程(10)并将sleep(10)命令放在几个位置。 The sleep command is to allow the CPU to drop back down to "normal" levels, so there are CPU spikes, not constant usage. sleep命令允许CPU回退到“正常”级别,因此存在CPU峰值,而不是持续使用。

On a shared host CPU spikes are expected. 在共享主机上,预计会出现CPU峰值。 But a constantly high CPU level will cause alerts to the hosting company (if they are any good). 但是,持续较高的CPU级别将导致向托管公司发出警报(如果它们有任何好处)。 You want to avoid running the CPU at 50%, or any high level for any long length of time. 您希望避免在任何长时间内以50%或任何高级别运行CPU。

If it doesn't matter if the script takes and extra few minutes, put some wait states (sleep) in your code. 如果脚本占用并且没有多余时间,则在代码中放置一些等待状态(sleep)。 This is also just fair to other users on the same machine. 这对于同一台机器上的其他用户来说也是公平的。

In short, it is not appropriate to hog resources on a shared server. 简而言之,在共享服务器上占用资源是不合适的。

Basically, if you yield enough time to other processes, it's not a bad thing. 基本上,如果你有足够的时间来处理其他进程,那不是一件坏事。 Spiking the CPU like you're discussing however is a bad thing and not nice to other users of the system. 然而,就像你正在讨论的那样加入CPU是一件坏事,对系统的其他用户来说并不好。 You should have a yield mechanism in your main loop ( usleep(100) for example) and run the command with a high nice number like 19. 你应该有一个屈服机制,主循环( usleep(100)为例)和高运行命令nice数字,如19。

Also, it sounds like you're doing individual insert/update/etc calls in your batch processing script. 此外,听起来您正在批处理脚本中执行单独的插入/更新/等调用。 With mysql it's a far better practice to do a batch inserts whenever possible (extremely fast compared to the individual ones). 使用mysql,尽可能进行批量插入是一种更好的做法(与单个插入相比速度极快)。 Depending on how you do this however, it can be a trade off of RAM for CPU time (for example, if you store all of the insert values in a string until their ready to be inserted with a single insert statement, then that may add up to a lot of RAM). 但是,根据您的操作方式,它可以在RAM中占用CPU时间(例如,如果将所有插入值存储在字符串中,直到准备插入单个insert语句,那么可能会添加高达很多RAM)。 If RAM is a problem, you can always build a temporary SQL file and then import the whole thing at the end of the process. 如果RAM存在问题,您始终可以构建临时SQL文件,然后在流程结束时导入整个内容。

A batch insert looks something like (for a table with two varchar columns): 批量插入看起来像(对于具有两个varchar列的表):

INSERT INTO `mytable` VALUES ('Field 1-Row 1', 'Field 2-Row 1'), ('Field 1-Row 2', 'Field 2-Row 2');

This would insert two rows at once at a fraction of the time. 这将在很短的时间内一次插入两行。

But then again, based on what you describe as the script's purpose, you probably aren't doing a lot of inserts to begin with. 但话又说回来,根据你所描述的脚本的目的,你可能不会开始做很多插入。 But maybe you could still build all (or many) of your DB updates/inserts/deletes into a final script called at the end? 但也许您仍然可以将所有(或许多)数据库更新/插入/删除构建到最后调用的最终脚本中?

Also, if you're sure that you can keep your foreign keys in proper order doing the import, turning foreign key checks off can significantly improve the speed as well. 此外,如果您确定可以按正确顺序保持外键进行导入,则关闭外键检查也可以显着提高速度。

All other suggestions possible would be based on specific optimization of your code and DB schema (optimizing loops, lookups, indexes, etc). 所有其他可能的建议都将基于代码和数据库模式的特定优化(优化循环,查找,索引等)。

What I'm strongly implying here is that you can do something like this in shared hosting without hogging resources, but your DB structure, SQL statements, and algorithms (loops, etc) must be highly optimized. 强烈暗示的是,您可以在共享主机中执行类似这样的操作,而不会占用资源,但您的数据库结构,SQL语句和算法(循环等)必须进行高度优化。 If you do this, an added benefit is that your process will finish extremely quickly as well. 如果您这样做,额外的好处是您的流程也将非常快速地完成。 There's a common mis-conception that php + mysql = slow/cpu hog but 99% of the time it's a programming or DB design issue. 有一个常见的错误概念,php + mysql = slow / cpu hog,但99%的时间是编程或数据库设计问题。 They should easily be able to handle that many records. 他们应该能够轻松地处理那么多记录。

The servers that run shared hosting are typically much faster than the average home computer, so it probably will not take as long as is does running on your local computer. 运行共享主机的服务器通常比普通家用计算机快得多,因此它可能不会像在本地计算机上运行那样长。 I think the alarm bells would start to go off if you were sending 500,000 emails a day but if you have a reasonable host using that much CPU for a short time shouldn't be too much of an issue. 我认为,如果你每天发送500,000封电子邮件,警报会开始响起,但如果你有一个合理的主机在短时间内使用那么多的CPU应该不是太大的问题。

You could always talk to them about it first, im sure they will sort something out. 你总是可以先和他们谈谈这件事,我相信他们会把事情搞清楚。

I would suggest using a service like Amazon EC2 or Mosso's Cloud Servers (owned by Rackspace). 我建议使用像Amazon EC2Mosso的云服务器 (由Rackspace拥有)这样的服务。 These are virtual servers which have reasonable hourly rates and excellent APIs. 这些虚拟服务器具有合理的小时费率和出色的API。 You get the power of a dedicated server without the minimum monthly commitment. 您无需每月最低承诺即可获得专用服务器的强大功能。 For example, you can configure a virtual server with EC2, then have your regular web server run a weekly/monthly cron job to start that EC2 instance, run your job, then shut down the EC2 instance. 例如,您可以使用EC2配置虚拟服务器,然后让常规Web服务器运行每周/每月cron作业以启动该EC2实例,运行您的作业,然后关闭EC2实例。

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

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