Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a microcontroller which reads temperature from a sensor every second and serially sends it to a computer attached to it via USB. The computer has a LAMP server running on it. It takes the temperature measurements from the microcontroller and adds them to a MySQL server running on the computer. There is also a PHP file on the computer which is able to read the most recent temperature from the database and "echo" it. I have an Android app which queries the PHP file to get the temperature and update it on the screen.
Now, the question is: All of this process is happening on a local area network, so it's pretty fast. Would it be a good idea to "poll" the server every second if this was happening over the internet? How often should I ideally query the PHP file if I was doing this over the internet?
It just depends.
You can query your database as often as you can, however you may meet efficiency issue.You can just write a script which just query your database and set a clock to make that script just run for sometime (30 seconds for example). You count the total query, and output the count into file.
Use this method you can test the max qps of your database with that specific query.
Normally, you should reduce the frequency of every database query, cause the database query may be the bottleneck of your script speed.
You can implement some asynchronous method to update it. Although there is no limitation on how many times you can query but it can hamper your server performance if too many connections are open. So you have to think of some logic or time which will suit your need (get updated data to user) and also not hamper server performance. You can also lower the burden by just querying updated data not entire data (as mentioned by #Rikesh)
Create a updateTime field in your table. Each time you update the row update this field with current time. When you are querying use this field. For the first time the updateTime = 0. store the max updateTime values and use it for the next call. So that each time you are fetching rows that are updated after the given time.
Another technique was cache the fetched values and make a query to fetch the data that are updated after the cache created. And sync the cached data with the updates.
If you query and fetch all data often( 1 sec as you mentioned in question), it will affect the server performance.
Use a cron job e.g set the script to run every so and so minutes...or you could use ajax to call your script every second or on events such as button clicks. If what you are aiming for is a real-time app you might want to look into some real-time technologies such as comet long-polling(facebook notifications), websockets, xmpp or as I said ajax!
If you want to show/get the only update values and reduce the time then you have to compare/check the id value with the previous one,it means
save the current retrieved id or any unique value from row(like datetime)
and for the next time check the id or datetime values (like select * from table where datetime>currendatetime)so that you can get only update rows
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working on an algorithm that gives a score to some messages posted on a website. This score will be used to rank all the messages. If, for one message, the score is high, this message will be ranked above the other messages (that have a lower score) and vice versa. The parameters for this algorithm are the number of upvotes/downvotes and the number of seconds gone by since the message was posted.
I want to display the millions of messages on a webpage using PHP and MySQL. Of course I will use a paging system. Since one of the parameters for the algorithm is the number of seconds gone by since the message was posted, this score will change over the time. But I will need to update it. The only way for me to update the score of each message is to update it automatically with PHP when the client asks for the messages to be displayed, then call them with a MySQL request like that : SELECT * FROM messages ORDER BY score.
But because there are millions of messages, it would take a lot of time to update everything each time someone wants to see some messages.
How do I implement this in PHP ?
So basically, I'm asking how to rank messages (using a score) without having to calculate the score of each message before I call them (because it would take a lot of time) + because I'm gonna be using a paging system, only 20 or 30 messages will be selected at a time from the database.
Thank you very much
Create a field which holds miliseconds since 1970 when a post is created. then use the following:
select milisecondsSince1970 as t,votes as v,* from messages order by (v-a*(t-t0))
where:
"a" is your personal factor for giving desired weight to "t".
t0 is miliseocnds from an exact date which you consider it as
start date rather than 1970.
This solution just works for you question as the criterion of score is "time". In other huge calculations , a periodic update on scores is suggestd.
Updating in real time will be difficult, and expensive in terms of performance. But I have suggestion - you can use a MySQL events scheduler to schedule JOB at regular intervals may be every half hour depending upon DB size. Running batch script as cron job on regular intervals may also work but direct Event execution in MySQL is better choice.
Since rank is calculated based on age of post and votes, so a stored procedure will work.
Other solution, I recommend - run update query to calculate rank on result set as data in shown in paginated form.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The title might be a bit unclear. But I have this problem with my website. I have some data stored in a sql database, including a timestamp. But now I need to make some calculations with that data and send an email, after a certain duration after that timestamp. But how do I do this? Since my website and my database only contain static data...
So to be clear: I have a site on shared hosting with a database. In that database are items that all contain a timestamp value. Now I need the server to undertake an action for every item in the database on a certain duration after that item's timestamp, for example send a mail.
This is probably an easy question, but I have no clue what to look for...
EDIT: I think I have found a solution by using setcronjob.com , thanks!
You could run a job every so often to figure out how old that timestamp is and then send the emails you need to send. But how to run the job?
If you can run scheduled jobs on your hosting (ie, you own the machine/VM and are not on shared hosting) you can run these jobs every x days/hours/minutes/seconds. In Linux, look up cron jobs, and Windows look up Task Scheduler (I think)
If you're on shared hosting it's more difficult. Maybe you can convert some of your static pages into dynamic pages that run these jobs? Depending on how long they take to run you might want to look at running these jobs in a new thread (pthreads). You'll need to have pthreads installed for this on your hosting, so #1 - scheduled jobs - is definitely more preferable.
You probably want to use a Cron task to search your database for every row with the timestamp near the current time. From that point, you can simply do your calculations and send the email.
Could you tell me more about that "certain duration after that timestamp"?
Also, in the database you could setup a job that runs on a schedule to make the calculations and email the results.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
(PHP)
So here is what I would like to achieve.. but apparently I can't find any relevant information anywhere.
I would like my site to select a random value from my database every 5 minutes (for example: an ID from a table), and everyone that visit my site would see that same selected value, until the "server" randomly select another value 5 minutes after the last select.
So, I guess there would be a function that do the select from the database,
but
1) where would I implement that function?
2) where would I call it? I'm not a expert in PHP, but I don't think it can be called from the client, else every client would call the function and they would not see the same value as others?
3) how do I to set a 5 minutes timer to call the function again?
In brief.
Server select a random value (ex: an ID from a table).
It get displayed on the site (same value from everyone for 5 minutes)
5 minutes after last select, another value is randomly selected
It get displayed..
Repeat...
I will probably use Laravel to created my website, I'm saying just in case it's important for the solution.
Thank you for your help!!
1) As far as implementing functions goes, It is pretty simple. If you need to use your function in multiple PHP documents, place int in a separate file and use an include to bring it into the document(that the client calls):
<?php include_once($_SERVER['DOCUMENT_ROOT']."/Path/To/file/phpscript.php");
If you only need it in one document, simple place it in in the document(that the client calls) before you use it.
2 & 3) There are "cron jobs" if you'r using Linux(I personally don't like using them), which could be used to awaken the php script every 5 minuets(in which case keep the function in its own file), but instead I would (edit: not, use cron jobs, this is a last resort)recommend another method instead. In your function(which will run when the client requests it), write the time you last requested it in to some text file, database or anything else. when the function runs, check if the written time was more than 5 minuets ago. If it was, create a new number and write a new time down. If it wasn't, fetch the old number from the database.
EDIT: Use Cron Jobs
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am building a web application that would requires me to reserve a seat for the customer for 30 minutes or closing of the browser before the reservation gets deleted. Any suggestion of the best way to do this?
I thought of adding a record into MYSQL and runs a cron job but that would not give the users the most updated results. Any advises?
Thanks!
Write a record with an expiration date. Use that expiration date when writing queries. Clean up expired records every so often.
Closing the browser however is more complicated and not reliable. Connections to the server in http are stateless and as such you really wont know when the browser has been closed. You can regularly poll with ajax and delete records when there are no longer any poll updates.
This problem is actually 2 problems:
How do you clean up old records
and
How do you ignore expired records.
Add an expire_time field to your reservations table. You probably have some logic that looks at how many seats are still available, add the expire_time to that logic so that expired records are ignored.
Then write a script or function that gets called periodically (cron works, so does register_shutdown_handler()) to clean up the expired records.
This way, should your cron ever fail or be slow for some reason, you're not accidentally blocking new reservations. You could even skip the cleanup step completely and take the (minor) performance hit.
I believe it should be resolved by session, not the mysql.
But if you insist, you should update item every time page get hits, and you can do also some sweepups
EDIT
Yes, of course. The reservation system must be multi user. If one user does the reservation than the other user must not be able to get the seat.
Yes, simple resolution is just to add there new column called reserved that will specify the time until it is reserved.
Why do you need cron for it?
BTW: sessions are stored on server not client. cookies are stored in client not server.
Lets say there is a thread ( on forums ) which will be active for 3 days only. Now, after 3 days, I want this thread automatically closed.
Can I use reference to time when this thread is created in database, and than make if statement if current date + days is bigger than date created, I will print out "<h2>Thread Closed for posting</h2>"
And when I consider some other tasks, I suppose I can use reference to time and have certain event executed on this.
Am I right?
You can use a cron (ran every minute) to set a status field on the thread table to closed such as.
UPDATE threads
SET status='closed'
WHERE lastPost+INTERVAL 3 DAY<NOW()
Then in PHP something such as
if($thread['status'] == 'closed')
{
// Put your HTML here.
}
A 'cron' is a task that runs at a specific interval or time. This means that it should be used for tasks that must be done without user interaction. For example, a backup, automated emails or pulling data from a remote service.
What you want is better suited to a condition on checking whether a thread is closed. Rather than just having a flag, you also check the age. This means you can change your old-thread logic without needing to edit the database.
You could make a PHP script that gets executed by cron (read up on how to execute PHP in the command line) that SELECTs all the posts in a certain date and then sets them to closed. If you ran that, say, twice a day, then you could do a good job in getting all those posts closed.
Good reference on using cron to run PHP