Reputation: 37
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
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:
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
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