简体   繁体   English

慢查询-GAE PHP和Cloud SQL

[英]Slow Queries - GAE PHP and Cloud SQL

I have a scheduling app that allows users to create events in bulk using a weekly template. 我有一个调度应用程序,允许用户使用每周模板批量创建事件。 This works by posting the template lessons via AJAX to a PHP file that takes the template and replicates it the desired number of times. 这是通过将AJAX上的模板课程发布到一个PHP file来完成的,该PHP file接收该模板并将其复制所需的次数。 The number of events it has to create at one time could be from 200-800 depending. 它一次必须创建的事件数量可能在200-800之间,具体取决于。

The problem I'm having is that these queries take a long time to finish, which causes the website to hang and sometimes time out while waiting for all the entries to be made. 我遇到的问题是这些查询需要很长时间才能完成,这导致网站挂起,有时会在等待所有条目输入时超时。

A simplified example: 一个简化的例子:

$events = array('valid array of all events and their details');
foreach($events as $event) {
      $query = 'Valid SQL with individual event detaisl';
      mysqli_query($connection, $query);
}

In reality more would be going on, for example, calculating start and end times, associating users from junction tables , etc. This would have to run 200-800 times to create an entry for each individual event made by the template. 实际上,还会进行更多工作,例如,计算开始和结束时间,将用户与junction tables相关联等。这将需要运行200-800次才能为模板产生的每个单独事件创建一个条目。

How would I go about speeding this up? 我将如何加快速度?

Any insight is greatly appreciated! 非常感谢任何见识!

UPDATE: I've already tried increasing the Tier of the SQL instance. 更新:我已经尝试过增加SQL实例的层。 It doesn't have any effect on the amount of time it takes. 它对所花费的时间没有任何影响。

Some suggestions to consider: 需要考虑的一些建议:

  1. Creating 200-800 new database entries per single request sounds a bit excessive. 每个单个请求创建200-800个新数据库条目听起来有点多余。 The logic of your app and the data models/structure you use could probably be significantly improved and optimized by not creating all events at once, instead, only saving the settings that let you generate the 800 entries with some code that would return an individual event if needed and save it separately if any custom (not from the template) info was added or changed in the future. 通过不立即创建所有事件,而是仅保存设置,使您可以使用一些返回单个事件的代码来生成800个条目的方式,可以显着改善和优化应用程序的逻辑以及所使用的数据模型/结构的逻辑。如果需要的话,并在以后添加或更改任何自定义(不是来自模板)信息的情况下分别保存。 This would probably require a lot of changes in your code and might or might not be worth it (time-wise not speed-wise which would definitely improve). 这可能需要对代码进行大量更改,并且可能值得(也可能不值得)(在时间上而不是速度上肯定会有所改善)。

  2. As already mentioned in the comments, this could be taken into a separate task with some UI feedback to the user saying that the events are being created, and then another notification saying that it is now done. 如评论中已经提到的那样,可以将其纳入一个单独的任务中,并向用户提供一些UI反馈,说正在创建事件,然后再有另一个通知说已经完成了。

  3. By looking at your code it looks like (I am not very familiar with PHP/mysql) when creating 800 events you also contact the database 800 separate times (each time waiting for a completion response). 通过查看您的代码,看起来(在我对PHP / mysql不太熟悉)创建800个事件时,您还需要分别与数据库联系800次(每次都等待完成响应)。 Assuming that the bottleneck was in these multiple connects then by concatenating your generated queries and feeding them to the database all at once you could potentially achieve some performance improvements. 假设瓶颈存在于这些多重连接中,那么通过串联生成的查询并将它们立即馈送到数据库中,就可以潜在地提高性能。 See example below (note the required semicolon at the end of each generated query and the mysqli_multi_query function allowing sending multiple queries at once): 请参见下面的示例(请注意,在每个生成的查询末尾都需要使用分号,并且mysqli_multi_query函数允许一次发送多个查询):

    $events = array('valid array of all events and their details'); $query = ""; foreach($events as $event) { $query .= 'Valid SQL with individual event detaisl;\\n'; } mysqli_multi_query($connection, $query);

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

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