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.
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 8 years ago.
Improve this question
PHP newbie here. My question is related to the logic/best practice for executing a certain task.
I have a news event/announcement being displayed on a website and needs to be removed/expire after a certain date/time. I have set a expiration date/time in the database, when a user visits the website after the date/time has passed a query is triggered and the news event/announcement is set to "0" (which is hidden).
I would like to know if this the best practice of accomplishing it or is there a better way?
Thanks.
The method you mention is usually effective, especially for small applications, but not a best practice. The reason it's not a best practice is because:
Issues
You are making the user wait for task execution
If there is no activity on your website, these tasks will not be done
If anything happens while executing a task, your user will receive the errors
The first two might not seem to matter much, but that's only because the tasks you have now are very fast and non-critical. However, if a task would take a second or two, that would mean the user now has to wait 2 extra seconds before he sees the page, which is bad.
Likewise, if nobody visits your site for a week and there's a list of 15 tasks that need to be done, the user now would have to wait for 30 seconds. It might even time out the whole page; which would mean your tasks are now unfinished and the user is annoyed with getting a timeout for seemingly no reason.
In addition, if one of your tasks is time critical, it still won't be done. For example if the task is to send someone a reminder email after 24 hours but nobody logs in, the mail won't be sent.
The last one is also a problem; both because this makes it hard to see when a task fails (as the error is logged as a user problem, if at all) and because your user (again for no reason) is now looking at an error screen.
Solution
If you want to use the best practice, move all these sorts of tasks to either a Scheduled Task (under windows) or a Cronjob (under unix). This means you have a system service that periodically starts up and executes a PHP script that can do maintenance to your site, such as removing these news messages, sending out emails, or other things.
This has a number of advantages:
The server will always be there on time to run the tasks when they need to be run
You can disable timeouts and upgrade memory availability to run intensive tasks
Users will not have to wait for anything to complete
You can add special logging to the server, so that you know when these important but hidden tasks fail
Most providers allow you to set these kinds of tasks even on cheap hosting packages.
By far the simplest way to do this is to have a publisheduntil field with a date time. Each time you get a list of events to be shown on a page check this field eg
Select * from my_table where published = true and published_until > todays_date
This is avery simple way of ensuring that events disappear when they should.
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 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
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am trying to create a time-sheet for my work. I don't know if getting myself into a lot of work by doing this as I am quiet new to PHP and MYSQL but I do have a good understanding/knowledge
of the two. I want the below fields in my database.
Job
weekPeriod ------A list of weeks Monday > Sunday
dateWorked ------List Of dates in the form coming from a database e.g. 1/1/2011
startTime ------List of times from 12:00am>11:00pm 30 min intervals e.g. 11:30-12:30
endTime ------List of times from 12:00am>11:00pm 30 min intervals e.g. 11:30-12:30
totalHours ------Automated
amount ------Automated based on dayWorked
comments ------Any messages here
I want to be able to fill in some drop down boxes through a form that will then submit all information to my database.
I want the script to know that if the date worked is on a Weekday Mon-Fri e.g. my rate of pay is 30.00ph On a sat it is 35.00ph and on a Sunday it is 40ph
I then want to create a page where i select a particular week and see how many hours i worked and how much i earn and so on.
Please let me know if there is such a program already established or if this is something that requires a bit of time and if I could do it being new to PHP and MYSQL
Try this it is great what you are looking for:-
phpcollab
I made exactly the same program, but build on the facebook platform. Some things that you might want to consider are:
Shift start time
Shift end time
Paid Breaks
Unpaid Breaks
Overtime
Calculate break entitlements for overtime
Different rates of pay
DST
What happens if you finish early?
When are you pay periods, and what time of day does it cut off (mine was at 3am!)
The key thing you need to do is set down your rules currently in place at work. E.G if you do over time, what do you get paid. Would this mean you are entitled to another break, is it paid? What happens when the clocks change?
Concentrate on getting the program logic to work out the correct pay for different scenarios. I would use arrays to imitate form input. Once you've got it working then build the interface.
In your database all you need is
- Job
- Start time (date/timestamp)
- End time (date/timestamp)
- Comments (text/varchar)
Then in a seperate table
- Week number (UID)
- Start Date (date/timestamp)
- End Date (date/timestamp)
Everything else can be calculated easily and doesn't really need to be stored. This means your logic can be updated much more easy if you calculate it when you do your lookups.
I would look up the following function:
http://php.net/manual/en/book.datetime.php
This works very well for me. It's in an advanced stage of development. http://www.simpleinvoices.org/
But if you want to use this project as practice to gain comfort with php/mysql, it would be a fairly straightforward learning project.