PDA

View Full Version : Limiting MySQL queries per section of site


OneSeventeen
3-28-06, 11:16 AM
I'm pretty sure we have a limit of 72,000 queries per user per database per hour, correct?

I want to make a web site that allows people to sign up and have an account on my page (kind of like myspace, but nothing at all like myspace...) But, I want to limit the number of queries per hour so I don't go near my limit. (If someone wants more queries, they will be directed to a powweb sign up page so they can get their own site :p )

How hard would it be to keep track of the queries for each user, and block their site if it takes up too many resources while allowing the other, less popular accounts to remain up and running?

I'm thinking maybe a SQLITE database to keep track of visitors and just use that instead of queries, or perhaps use the SQLITE database to keep track of individual queries... I'm using a class to handle SQL stuff, so adding a line to increment the value in a database should be a matter of adding the code inside the query method.

Any tips?

satis
3-28-06, 12:32 PM
that would be my recommendation. Since you're using OOP classes, it shouldn't be hard to just increment some global variable every time a query is used, then increment a counter somewhere. During the __construct() portion of your class you could have something check that number (and maybe a date/time stamp since it would be an hourly limit or whatever) and then either continue or die() or something similar.

The hardest part I could see is figuring out when to expire counts. I guess you could just write a new row for every query with a timestamp, and then just do a count, but if you have alot of users that table could get awfully big awfully fast. Well, I guess that could be taken care of too. :p

OneSeventeen
3-29-06, 12:13 PM
I'm thinking now that all I would have to do is store a row with the timestamp being the most recent hour (so if it is 8:00 AM, it would be 8:00 AM, but if it was 8:35, it would set it to 8:00) then just increment that counter until the next hour.

So it would first check to see if there is a row.
If there is a row, it will check to see the hour.
If the hour is older than the most recent hour, empty it and start a new count at 1 for the most recent hour.
Otherwise, check to see if the counter exceeds the limit.
If it does, include a PHP file that displays a nice screen with a message as to when the user can return.
Otherwise, increment the counter and go about displaying the site.
If there isn't even a row to begin with, it will create a new row with the most recent hour. (that would probably only happen when the table is new)

I'm also using the Smarty template engine, so I may configure my script to use caching so I don't have to call so many queries.

Or, even better yet, just have it generate the HTML files, and have them updated each time an admin adds an item or edits content.

satis
3-29-06, 12:20 PM
that sounds like a plan. I'm not familiar with Smarty, but the rest of it sounds very workable. You going to store that in mysql (potentially eating up more queries) or in some sort of flat file system?