0

I'm trying to implement an AJAX script that will be responsible for checking for new data on my MySQL server and updating a list if new data exist. The one solution that I found is to have a timer to checking any given time, which works, but because on my system i'm going to have more than 50 online users at the same time, they will execute this script and I will have an impact on the processing time of my MySQL server. The second solution is to use Comet model but in order to update the A form I have to link somehow the B form to inform the server that new data has been inserted in order for the server to send a response to A form to inform it that new data have been added, which i don't want to link the B form with the server. Is there any different scenario that I can use in order for limiting the processing time, overloading the server ,minimize the performance and not reaching the max MySQL connections on my server for checking for new data.

Thank you

George Panayi
  • 1,768
  • 6
  • 26
  • 42
  • 1
    [How to limit the sql execution time](http://stackoverflow.com/questions/11772854/how-to-limit-the-sql-execution-time) **or** [MySQL - can I limit the maximum time allowed for a query to run?](http://stackoverflow.com/questions/4794747/mysql-can-i-limit-the-maximum-time-allowed-for-a-query-to-run) **or** [Anyway to Limit MySQL Query Execution time?](http://stackoverflow.com/questions/8254687/anyway-to-limit-mysql-query-execution-time) **or** [How to limit the sql execution time](http://stackoverflow.com/questions/11772854/how-to-limit-the-sql-execution-time) – Pred Oct 07 '14 at 07:54
  • I don't want to minimize the execution time of an sql query, I want to limit the connections that the AJAX will do. for example if I have 50 users, they will send 50 requests to the server ask for new data and then after 5 secs for example they will send another 50 request to check if there are any new data – George Panayi Oct 07 '14 at 08:16
  • If the query is fast enough and the connections are closed immediately after the query successfully processed, the connections will be distributed in time, so with 50 users there should not be 50 actice connections in a specific point in time. (Advices: run as simple query as you can and close the connection manually) – Pred Oct 07 '14 at 08:29

1 Answers1

0

If you are using php, use set_time_limit ( int $seconds ); to limit the max execution time of the php script that you will be calling using AJAX. That will also limit the max execution time of the mysql running.
Also, you can use ini_set('max_execution_time', $seconds); to limit the max execution time

h2O
  • 544
  • 1
  • 15
  • 38
  • 1
    The `set_time_limit()` function and the configuration directive `max_execution_time` only affect the execution time of the script itself. **Any time spent on activity that happens outside the execution of the script such as** system calls using system(), stream operations, **database queries**, etc. **is not included when determining the maximum time that the script has been running.** This is **not true on Windows** where the measured time is real. (http://php.net/manual/en/function.set-time-limit.php) – Pred Oct 07 '14 at 07:34
  • @Pred, yes the docs say that. But consider this => I remember that once I queried a database (1GB+) with php and it took more than the max execution time (15s). It showed a fatal error and stopped the query. Any idea why? – h2O Oct 07 '14 at 07:39
  • It will stop the query as it is executed in a loop and values in each loop is passed from the database. So, php has the power of stopping the script. While inserts usually do not take up much time – h2O Oct 07 '14 at 07:41
  • The processing of the result (when the result is big enough) could result `Allowed memory size of X bytes exhausted` or `Execution time exceeded` errors. Remember, that you are processing the result with php (`while (false === $row = mysqli_fetch_assoc()`) – Pred Oct 07 '14 at 07:42
  • @Pred, yeah that is what I meant to say. If the query results in `Execution time exceeded` error, it can stop mysql from executing. – h2O Oct 07 '14 at 07:44
  • Nope.. The query runs **outside the loop**, you are just processing the **result of the query** with php. You can only limit the php script with this directive (this is true), but the query execution will not be limited or the queries run time will not be counted in the execution time of PHP (except on windows) – Pred Oct 07 '14 at 07:49