PHP MySql Storing Data for 24 hours - php

I'm building a toplist for my site and instead of recording in and out hits and emptying the database every 24 hours I would like to record in and out hits "within the last 24 hours", this way the toplist will always look busy no matter what time you look at it, while still acurately ranking sites.
So basically I'll have to add a record to the database logging an 'in' hit (for example) and the remove it around 24 hours later, or simply not consider it when ranking the sites.
I want to know the most efficient way of doing this, I have some ideas but they all seem really resource heavy. I hope you understand what I'm trying to achieve :) Any help would be appreciated.

You don't necessarily have to delete "old" data all the time. You can also limit the data set to records within the last 24 hours when querying data.
WHERE
site='xyz'
AND hit_datetime > Now()-Interval 24 hour
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

I would delete the data that is older than 24 hours with a simple
DELETE...WHERE hit_time < now() - interval 24 hour
The other question is - when to call it? The tradeoff is between performance and stale data. The more often you call it, the less "stale" data there will be, but the server load will grow.
I see several approaches, pick one that suits your needs most:
Call it at the start of every script. This can be optmized by calling it only if the script will do something with the hit data. That way every script will always run with "correct" data. However this will have the maximum load.
Schedule a cron job and call it once every 1h/2h/24h/etc. This way there will be a little bit of "stale" data, but the overhead will be reduced to a minimum.
Do it like PHP does it with sessions - on every script startup give it a x% (x is configurable) chance of being run. That is, take a value from 0 to 100, and if it is smaller than x, execute the DELETE.
You can also invent some other schemes - for example, run it once per user session; or run it only if the execution time is evenly divisable by, say, 7; or something else. Either way you trade off performance for correctness.

Write a Stored Procedure that deletes records older than 24 hours. Then write a trigger that runs on every INSERT statement and calls the SP.

you could store the timestamp with each "hit" and then call a query like
$time = time()-86400;
mysql_query("DELETE FROM xxx WHERE timestamp < $time");
or you could same thing within the SELECT statement, depends on if you still need the hits afterwards, etc

If the time-constraint is not really hard (e.g. you'll loose money or are really annoying your users if the data is kept in the the db longer than 24 hours), I'd use use PHP's register_shutdown_function like this:
function cleanup() {
// open db-connection etc.
$query = 'DELETE FROM <yourtable> ' .
'WHERE UNIX_TIMESTAMP(<timstampfield>) < ' . (time() - 86400);
mysql_query($query);
// close connection
}
register_shutdown_function('cleanup');
The above code assumes, <timestampfield> is of one of the the MYSQL-date-datatypes (TIMESTAMP, DATE, DATETIME).

Related

MYSQL insert and delete at the same time IMPACT

Here is the scenario.
I have a schedule job running every minute which inserts data into a MYSQL table "demo". The total number of records per day are 60*24 = 1440.
Table demo already has 55000 records.
I want to clean records less than today's date. Therefore I am using below code to do the work daily at 10.00 AM.
$demo = Demo::whereDate('created_at','<', Carbon::today());
if(count($demo) > 0)
{
$demo->delete();
}
Now a point will come where at the same time I am inserting to the same table and deleting from the same table.
I want to know that it will be safe? Or there will be an error or any other impact.
I don't think this will be an issue, since Carbon::today() returns 00:00:00 as time and you are executing the deletion job at 10:00:00. Only records that are inserted more than 10 hours ago will be deleted.
According to me, there should not be any problem. If there are two requests at the same time MySQL server will handle them by itself. The same thing happens when a website is loaded. There is a lot of call at the same time.
There is no problem. Imagine you made 1000 requests per minutes, no one of them will overwrites an other one.

PHP/MySQL update mysql field after 10 minutes has elapsed.

As the title reads, I am looking for a way to update a mysql field after 10 minutes has elapsed of a query being run.
Something like below is an example without the time restraint:
mysql_query("UPDATE `players` SET `playcoins`=TRUNCATE(ROUND((`playcoins`+$amount),9),8) WHERE `id`=$player[id] LIMIT 1");
Any ideas?
MySQL databases have a class of object called an EVENT. It's basically a hunk of SQL code that runs at particular time, or on a particular interval.
You could use code like this to create an event to do what you require at the right time in history. This particular code will create an event that runs just once, ten minutes in the future.
DELIMITER $$
DROP EVENT IF EXISTS coins_user12345$$
CREATE EVENT coins_user12345
ON SCHEDULE
AT NOW() + INTERVAL 10 MINUTE
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
UPDATE players
SET playcoins=TRUNCATE(ROUND((playcoins+123),9),8)
WHERE id=12345
LIMIT 1;
END$$
DELIMITER ;
To use these EVENT objects, you have to configure the event scheduler correctly. Read this. http://dev.mysql.com/doc/refman/5.6/en/events-configuration.html Some cheap shared hosting providers don't allow the use of events, so this is not guaranteed to work.
You go it the wrong way. Sure you can do it. And you can do it with PHP. But you shouldn't. PHP is not the right language to do such a task. Before I starting talk about shell_execute and sleep, which would be the core elements, you need to do this, I offer you another solution.
If I see right, you want to give a player every 10 minutes, some coins.
The right approach would´basicly be:
Save the last time the player has get coins in the database. If you get the player coins, you first want to check, the last time you give the player coins. Now calculate, how much he has earned in this time difference. Finaly add this to his balance and update the field, where you save the last time, the player has earned coins.
An alternative would be a Cronjob/Scheduled Task to a PHP file, which is called every 10 minutes, to give each player the coins, he should get.

Remove Mysql row after specified time

I'm trying to create a computer reservation system, where user chooses a computer and select the time how long he will be using this PC. In that time other persons can't reserve this pc, I need to find a solution, how to automaticaly delete all rows containing reserved pc's after their time expires. Thank you for the advice.
The common way to handle this is to store an expires_at timestamp on the reservation row. Then your query to find any "open" reservations would have WHERE 'expires_at' < NOW() or something similar.
This is an untested answer, that may only be a suggestion, but I just started looking at these, so am interested in feedback as well. i'm still working through possibilities and drawbacks, but it might well suit your need.
Take a look at MySQL Events, an article about it is here, and official syntax at Mysql Docs.
Per the article:
An event is similar to a trigger. However, rather than running in
response to a data change, events can be scheduled to run any number
of times during a specific period. In effect, it’s a database-only
cron job.
Pondering this, I'd envision a procedure that deleted anything >1hr (if that's the expiration). This procedure would be TRIGGERED on new inserts to get rid of anything expired at that moment, but also in an event to run every 15 minutes or so so that automatic deletes by the trigger aren't dependant on somebody else adding a reservation to trigger that procedure.
If your server is linux, you can use cron jobs to check once a day every reservation dates. If these dates have expired .. modified field reserves to be available.
Normally I would do it this way:
when storing a reservation, store date_from and date_to both of datatype DATETIME
when checking if there is a computer free check for all computers and filter with WHERE '{$my_date}' >= date_to AND '{$my_date}' <= date_from - by this You should be able to get all the PCs that are not reserved within a certain time...
To be complete in the solution, you need to run a CRON job which calls a query to remove all reservations that have a reservation_time + (15 * 60) < unix_timestamp().
I am assuming you have a time that the reservation was placed or started and are using UNIX/Epoch Timestamps.
Instead of doing a expires_now, if you know it will always be a fixed interval ie 15 minutes, you can do:
DELETE FROM reservations WHERE reservation_time + (15 * 60) < unix_timestamp()
Something you could look into is managing cron job's from PHP, http://www.highonphp.com/cron-job-manager.
The above script will, when a reservation is created, insert an entry into /etc/cron.d/ and you could configure it to run at the expected reservation endtime. Then inside the php file which would be executed, you could do:
DELETE FROM reservations WHERE id = :id

Database Values For User Return After Prolonged Amount Of Time?

Database Values For User Return After Prolonged Amount Of Time?
So I have values in my database e.g. stamina, max_stamina, ...
What I want to do is this:
If stamina is < max_stamina then
after 60 seconds stamina=stamina+1
else
nothing
I will need to loop this untill stamina equals max_stamina
I can easily create up the if statement, the problem I need help with is coding the 60 seconds, how could i go about it?
All so this will need to run when the player is and isnt logged in.
take a look at mysql events and schedules think that is what you are looking for. Also if you are having more than 1 users it would be smart to update them all in that 60 seconds interval you need.
Without knowing more about your code architecture, you most likely will need a cronjob to run every minute and to update all stamina records according to your criteria.

PHP MySQL if time is 5 minutes more or over then the stored time

I want to the script to delete the row if the difference between a timestamp that is stored in the db and the current time is 5 minutes or more.
Example
A row in db has 2011/12/25 10:00
the current time is 2001/12/25 10:06
I want it to remove the db row.
Why the cron suggestions? You can easily call the very easy delete statement on every page view. At the moment that becomes a bottlenecks (and then we're talking about either a massively underengineered table structure or a very busy site), it may become appropriate to see why the query is so slow. Run an explain query, apply some indexes, and so on. If the problem then still exists, it could be that it's simply too much work to let a visitors wait for, and then it's time for cron.
Until then, simply execute the following query on each page view:
delete
from
YourTable
where
adddate(YourDateTimeColumn, interval 5 minute) < now()
The code above is slightly wrong. It should be
delete
from
YourTable
where
adddate(YourDateTimeColumn, interval 5 minute) < now()
There was an s on minute, it shouldn't be there.
If you are in Linux, you could write a chron job that executes a php(or almost any other language) script that does this. As far as I know, a database will not have this sort of functionality built in, you need to write code that actively does it. Hope this helps.
try this, not tested tho:
$query = mysql_query("SELECT * FROM table");
$result = mysql_fetch_array($query);
$id = $result['id'];
$timestamp = strtotime($result['timestamp']);
if($timestamp < time()-300){
mysql_query("DELETE FROM table WHERE id=".$id."");
}

Categories