michael
michael

Reputation: 37

how to optimise ajax queries to mysql

I made a webpage which has to retrieve data from a database every 2 seconds in order to show the user a dynamically changed market with prices and volumes.

I got the following code:

  $.ajax({
      type: "get",
      url: "getData.php",
      data: {
          item: 'H',
          type: 'price'
      },
      success: function (high) {

          $.ajax({
              type: "get",
              url: "getData.php",
              data: {
                  item: 'L',
                  type: 'price'
              },
              success: function (low) {

                  var dist = high - low;
                  // do something with the high and low prices... 
                  // keep retrieving data based on the high and low prices... 
                  //more ajax called are nested inside here...
              }
          });
      }
  });

Will this nested ajax call cause the server cpu overuse?

In my getData.php file, I always have require_once('connect.php'); which connects to the database. Does it cause lots of mysql connections, because I sometimes get the error 'exceed the max mysql connection number'? How can I fix it? Do I merge similar ajax calls and use parseJSON?

Thank you.

Upvotes: 0

Views: 155

Answers (2)

jdog
jdog

Reputation: 2539

As always with databases, the database itself should be your major performance concern. Instead of increasing your connections, work on minimising the server impact your database has on the server:

  • If possible make sure that your query(ies) are well indexed and indexes are used (not table scans). Use the explain statement to test for this. Sometimes, small changes to the database schema can make huge differences
  • Try to ensure that the queries are in the database query cache, ie. they will actually only be executed if the data changes. Otherwise the result should come out of memory. For this the query cache needs to be big enough and there are some query conditions that you need to watch out for, such as Mysql functions and the like
  • You may be better off simplyfying queries. While a bigger query may perform well on its own, if you break them down a larger amount of them may be served from the query cache and they will generally run shorter by themselves.

Hope this helps, if you need more help for this, you probably need to post some example queries and the model (and probably put it on dba.stackexchange.com)

Upvotes: 1

Lloyd Banks
Lloyd Banks

Reputation: 36659

I am not sure in what context you are using these AJAX calls, but nesting AJAX calls inside of other AJAX calls won't cause more I/O usage since AJAX calls aren't loops. You're essentially doing the same as

$.ajax{
    //do something
}
$.ajax{
    //do something else
}

except you are requesting a condition to be satifised for the second call to be initiated.

You want to edit your MySQL "my.cnf" file to get rid of the error you are getting:

max_connections = 100

Default value here is 100. It sounds like you're running an AJAX monster so change this to a higher number

Upvotes: 0

Related Questions