Saving last SQL query state in PHP - php

For a two-player game, I need to send updated data to player every 30 seconds.
I have a table (ideally 4 tables) from where I need to select data and sent to user once he/she login. Since it is multi-player interaction game, data needs to be sync every 30-60 seconds.
My problem is, I've a very heavy query to run every 30-60 seconds. So ideally, I should send only updated and new rows to the player during sync (Its also a front end requirement for IPhone/Android game, app don't want whole data during every sync operation).
I went through MySQL: difference of two result sets and hope I'll get only updated/new records through SQL but problem is, how do I save result of last query.
Even if I save first result in Session (probably not recommended) that record will be useless as soon as new row inserted or updated. Updating session record again will definitely put lot of pressure on the server.
Can someone please suggest the best way to achieve this requirement; Not detailed solution, just some hint/link will be sufficient.

Basically, this isn't that hard. Let me provide you with a step plan.
Add a datetime field to each table you want to do this on
In each of your updating queries, set this field to NOW()
Make sure that the application adds the time of its last update to all its requests
Have the server add the time of the update to result it send to the app (which also sends the updated rows)

Can't you just timestamp everything?
Give every row in the tables a timestamp called something like "last_updated"
In the query, filter out all entries with a last_updated that is before the last time the query was executed (or possibly the latest last_updated that the client got the last time it called the server)

Related

Kohana Session Table

Currently i am hosting a website with ~10k unique visitors a day and ~6 clicks per user.
So round about 60k pageviews a day.
I use Kohana 3.2 and save the session datas from every user in the "sessions" table. Every page request execute a timestamp refresh in this table! So it's round about 60k updates (excl. Selects / Inserts / .. ) refreshing timestamps only.
The mysql process is getting pretty low..
So that's my question:
Should i stop using the SESSIONS table for saving user
How can i use the $_SESSION instead the values from the table?
Is there another alternative to handle this problem right now? We orderd more server capacity but have to wait..
EDIT:
Maybe it's enough to deny all these "updates" by every click..?
Okay. At least it was enough to truncate the table "sessions" - for the first moment. There was more than 1kk records - thats why the database operations getting pretty slow..?
Maybe it's just a simple mysql problem, and it's enough when i change it NOSQL.

Undo in PHP CRUD application

What would be the best way to achieve an undo function in a PHP CRUD application? The only solution I've been able to come up with is using a sort of buffer table in my database that is periodically wiped.
If the user clicks the "Undo" button after deleting a record for example, the id of the last change will be passed to a handler which will pull the buffer record and reinstate it into the main table for that data type. If the "Undo" is not done in say, 4 or 5 minutes, a reaper script will drop the entry.
Does this sound feasible? Is there a better way of accomplishing this?
You could use a flag field in your database to mark a row for delete.
And you can setup task (crontab in linux) to delete all rows with delete flag set to true and time difference > to 5 mins.
I've learned to not delete anything, but simply do as Ignacio Ocampo stated by using a flag column in your DB such as status. By default set the status column to open. If your client clicks your delete button, just update that records status column to void, or deleted..
In doing this, you'll need to update your data request to pull only those records with the status column set to open. This allows the data to not be lost, but also not seen.
all undo(s) or redo(s) if applicable can reset the open status to - or + 1 record sorted by a timestamp column.
If db space is at a premium, and you need to remove old data then crontab does work, but I prefer the simplicity phpmyadmin conjob to loop a file that will wipe all void or deleted records older than time()-'(last cron run).
Depending on what and how you're building, you might also want to consider using one of the following solutions.
1) A pure PHP CRUD solution would be something along the lines you've mentioned, with also possibly storing cookies on the client side to track which actions are being done. Every action a new cookie is created, then your application will only have to sort the cookies by date and time. You could also set the cookies to be automatically expire after x amount of time. (Although I would expire after a x amount of steps, instead of time)
2) If you are able to use HTML5 local storage (http://www.w3schools.com/html/html5_webstorage.asp) along with some Javascript would be perfect for this, since you wouldn't have to wait around for the server to respond everytime 'undo' is clicked since all the processing would be handled locally.

How to delete a row of mysql database after a few hours?

I created a script that register for news letter. but before accepting this processes the registered mail must be validate. so ... after the registration , the scrip insert data into row for the news_letter thable with this value: reg_mail,val_code,reg_time and ...
Until now , this process is done after registration. The script creates a random code and get NOW() time. It then saves in database.
After that if someone else go to reg page, the script sends a query to database and deletes rows that passed for example 5 hours.
I want to change this process and tell mysql to automatically delete rows that pass specific time without send any query to database.
Is it possible?
If so, how can I do that?
It's not possible. The best you can do with pure mysql is periodically trigger a stored procedure to clean the database.
If you want this kind of feature, you'll have to use other kind of storage (memcached or cassandra for example manage ttl pretty well) or layer of application to keep in memory id to expires (a simple process reading a file in which you write a list of events to expire with a timestamp could be enough).

Timer based status changes that are saved to database

I have a PHP website using a MySQL database.
We have items that users create, that are on a timer, and once this timer has counted down, without user interaction (basically next time someone sees it) the status needs to have changed.
I'm not sure how to implement this in a way to be accurate to the minute.
So we have an object X, that expires at 10:15pm tommorrow, and the next person to see object X after that time has to see it as expired.
Is the correct way to do this to be the next time object X is loaded we check if it's expired, and if so, update the database?
What happens if 10 people load object X at the same time after it's expired, what's to prevent some sort of race condition from all 10 requests attempting to update the database?
Is there a cron job that runs every minute that I can some how make use of, or any type of timer in MySQL to kick off every minute checking for these and running a script?
I have several ideas on how it -could- be done, like those listed above, but I'm not sure what the most practical is, or what the standard way to do it is as I'm positive someone has solved this problem before.
Is the correct way to do this to be the next time object X is loaded we check if it's expired, and if so, update the database?
Why do you need to update the database? It seems like you might have some redundancy in your DB table - from what you've said, it sounds like you have (for instance) an is_expired column and then an expires_at column.
Why not just get rid of the is_expired column? It's cheap to compare 2 integers, so when you want to determine if something is expired, just fetch the expires_at column and compare with the current time. This avoids any race conditions with expiry, since nothing in the DB changes.
You can do it with cron of course. Or with javascript native function setInterval( "checkFunction()", 10000 ); to alter the db. Or you could use a date field in DB and check for expiration
Make a field date_to_expire as DATE , enter the expiration date and everytime you query for it check to see if the item is expired (this can go up to seconds)

Updating Cells One After Another With Break From Database

I have a Excel like online spreadsheet for some purpose and it's almost done and working perfect but there is a issue I am facing and want to rectify.
You can see spreadsheet there at http://partydesigners.site50.net/Excel%20Like%20App/Index.html
The issue is that at one single time more than 1 users are using this spreadsheet and if one person modify any cell the other don't get it updated in their spreadsheet so I planned to have a setTimeout() function to call a function that will update every cell in sheet there from database. '
Now the problem is there are 40 rows each having 10 records from the database and 400 records needs to be updated every "n" seconds so it hangs the browser and UE suffers. I thought I can create a timer like update one cell and then move to another after few seconds and then update another after few seconds in a chain.
You can imagine as I updated first cell and then when it will be finished updating it will call a function for a cell next to it and so on a continue chain.
So what pseudo-jquery code you would write for it?
As Jonathun Kuhn mentioned in the comments, it would be better to keep track of only the cells that need to be changed and update them accordingly
Depending on how you have this setup with your database, will depend on how to keep track of what has been changed. But my first thought is to have a table that keeps track of one change per row along with a timestamp of when the change happened. Then you can run a function from the browser every 'n' seconds that uses some ajax to request all changes since it's last request (can keep track of the unique id of the last update, sort by timestamp and grab everything new).
This should help speed things up as it will only spend time updating cells that actually need it.
As a disclaimer, however, it is still very possible that a second user updates a cell before the first edit has shown on their screen. (Think two users editing a cell at virtually the same time, maybe the second initiates a 'save' milliseconds after the first.) The best way I can think of handling this is to show a warning to the second user, if it is noticed that they are editing something very quickly after a previous edit, that they may be overwriting data.

Categories