I have a history table with 3700000+ entries. I am using server-side processing in the datatable to initially display 25 records. It is taking lots of time to initially load the datatable even though the query fetches only 25 records.
I am using MySQL database. Now I want to limit the total number of entries from which the data should be processed. I want only past 15 days entries to be considered.
Is there a way by which I can load the table quickly?
There are a number of ways you can limit how much data you get back from a SQL Query.
Using WHERE will allow you to only select entries after a certain date.
$sql = "SELECT * FROM history WHERE date > ".strtotime("-15 days");
The above query assumes that in your history database you have a date that is a unix based timestamp. Another way that you could limit the amount of data that is returned is by using the LIMIT function.
$sql = "SELECT * FROM history ORDER BY id DESC LIMIT 15";
This query assumes that you only have one entry per day, and will limit it to only 15 entries to be displayed.
Along with this, if your history table has a lot of data that doesn't need to be used, then you can only select the columns that you need, doing something like this:
$sql = "SELECT id, time, name FROM history";
SELECT * FROM $table_name WHERE created_date>= DATE(NOW() - INTERVAL 15 DAY)
Related
I am working on a Office Management System in php and I want to create two appointment datatables. One will show previous appointments in descending order and other will show upcoming appointments in ascending order of date and time. Now in my MySQL Database I have date and time as different parameters. Now should I fetch all entries from database and filter using php and show them in different datatables or should I fetch entries using a filtered query and then show them in different datatables.
I have tried these filtered queries but these are not working:
For Upcoming
$sql = "SELECT * FROM p_appointment WHERE UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(date,' ',time), '%Y-%m-%d %H:%i:%s')) >= UNIX_TIMESTAMP(now()) ORDER BY date ASC
For Previous
$sql = "SELECT * FROM p_appointment WHERE UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(date,' ',time), '%Y-%m-%d %H:%i:%s')) < UNIX_TIMESTAMP(now()) ORDER BY date DESC
date is being stored in format 28/07/2021 and time as 2:25 PM
Any solution using php or MySQL will be helpful.
You can get cuttent_date() and current_time() like this:
$sql = "SELECT * FROM p_appointment WHERE date >= current_date() AND time >= current_time() ORDER BY date,time ASC
Also you shouldn't fetch all data from database if you have a lot of records (>100). It is a good practice to use LIMIT to get only part of records and perform pagination for records.
I am trying to achieve the Sum of Hours spent per day, where my mysql database has multiple logging records of every activity they perform (e.g; login, view, update, logout). What i am trying to do is SELECT (all records of a particular day) and find the difference in epoch time between the first and last entry giving me the 'Time Spent' for that period. Later add it by Month etc.
Database schema;
id, userid, time, action
**I can select the first and last entry from the query below:
**
SELECT
(SELECT time FROM log WHERE time(myDate) = DATE(NOW()) ORDER BY time LIMIT 1) as 'first',
(SELECT column FROM table WHERE time(myDate) = DATE(NOW()) ORDER BY time DESC LIMIT 1) as 'last'
But i am guessing a cross join or SUM for all these have to happen. Some guidance would be much appreciated.
So I have this php code:
"SELECT * FROM thread_db ORDER BY id DESC LIMIT 3";
This echos me later the last 3 rows of my mysql database. I somehow need it to get sorted by time so I get the 3 latest entries by time (closest to current date).
Try this
"SELECT * FROM thread_db ORDER BY your_time_field DESC LIMIT 3";
Replace your_time_field with the exact name of your time column in the query
I'm working on a web application written on php. I have some objects (represented as rows) in mysql table. And I need to show them randomly during a day.
How can I limit the show count of a particular object, e.g not more than 10 times for an hour?
By the show count I mean how many times the object was rendered.
For example, there are 100 images and with each pageview random 5 are shown. I need to normalize the image shows distribution, by limiting images' show count for an hour, for preventing 1000 shows for one image and 3 to another.
Hope its useful explanation.
Probably the simplest way to do it would be to add a field called last_shown to your table and then exclude it from the candidate list if it's been shown within the hour. eg something along these lines:
SELECT id FROM my_objects WHERE last_shown < DATE_SUB(NOW(), INTERVAL 1 HOUR) ORDER BY RAND() LIMIT 1
Then when you display that actual object, timestamp the column, ie:
UPDATE my_objects SET last_shown = NOW() WHERE id = <the_id_you_displayed>
This approach is simpler, but just as effective. If you reduced the timeframe to once every 6 minutes, it would effectively be similar logic to '10 times within the hour', and not require an entire new reference table.
You could create a log table with id and date_displayed.
Each time you select the rows random, you make sure that you select only rows which were not displayed more than 10 times in the last hour.
SELECT * FROM table
WHERE id NOT IN (
SELECT id FROM log
WHERE date_displayed > now() - interval 1 hour
GROUP BY id HAVING COUNT(*) >= 10
)
ORDER BY rand()
Also, after one hour you no longer need older inserts, so you might want to do a DELETE query to remove old records.
DELETE FROM log WHERE date_displayed < now() - interval 1 hour
I owns a website which is doing lots of searches per day. these search results are stored in my MySQL databse. what I want is I want retrieve today's top searches from MySQL limit to 20 and display in my website. how do I do this with PHP & MySQL??
Thanks
Mathew
You will need to do something looked like this:
$today = date("F j, Y"); // NOTICE, you have to format your date like you have # database field
$q = " SELECT search_string_field, count(search_string_field) as count
FROM search_results_table
WHERE search_date_field = '$today'
ORDER BY count DESC
GROUP BY search_string_field
LIMIT 20";
$results = mysql_query($q);
and fetch the results after executing query
You'll need to store the time for each search. With that you can query the database (using date ranges and limit) for the popular searches within a time period or you can do it with PHP.
What metric do you have for storing searches. Do you store every search term, or update a number if the term was used more than once?
If you have a number of terms searched
$query = 'SELECT * FROM searches ORDER BY searched DESC LIMIT 20';
But I think you need to give more info... what does your table look like?