Throttling database requests
A user on a web site can click on a button to vote for one image after another. This sends some requests to a MySQL database through jQuery. I'm not sure but the web site account probably allows 10 connections maximum.
In this context, can repeated clicking by a single user cause mult开发者_高级运维iple requests and in turn cause performance issues? If so, what simple ways exist to throttle requests?
Edit: Question reformulated as flagged as unclear.
If there is no reason for anyone to ever click the button more then once every few seconds, just setup a disable function for that button. So when it is clicked it is disabled for 5 seconds, then renables again.
You may want to look up a better timer method, this is one way i know off hand.
You can use Button onclick-
document.getElementById("button").disabled = true;
var enableButton = setInterval( "enableButton()", 5000 );
function enableButton()
{
document.getElementById("button").disabled = false;
clearInterval(enableButton)
}
Good Luck
First ensure that your problem is the database, maybe you have some logic on your webserver that is taking too long to process.
But, to 'throttle' your requests, you should have to optimize your queries, use indexes, cache on the server, or maybe redesign your database structure.
Hope this helps. Cheers
MySQL Setting Account Resource Limits
Starting from MySQL 4.0.2, you can limit access to the following server resources for individual accounts:
The number of queries that an account can issue per hour
The number of updates that an account can issue per hour
The number of times an account can connect to the server per hour
Additional Info -
MySQL does not have a query timeout value, or any other built-in way to throttle an individual query. However, it is pretty trivial to write a script that will kill long-running queries. You could even capture the user and query so that you can beat the offending user/programmer later.
Here is an example of one using PERL.
Edit to address aditional info
Depending on the queries your app makes, even a single click can put down the whole site. Although, voting can be implemented in a very lightweight way, so even if you have 1000 users giving repeated clicks to have the site responsive.
You could also limit the DB hits at application level - for example by holding in a session variable the id of the photos the user already voted on - check that variable before hitting the DB and you should be ok
精彩评论