I am creating a blog post scheduling system using CodeIgniter. I want 10 posts to show up a day. There is a field in the posts table named scheduled_date which I will get the posts that are less than or equal to the current date. When an admin user adds a new record to the database, I need an SQL statement that somehow will help me COUNT the number of records with the latest date in the database. For example:
// 9 records returned for the date 2011-01-01
$numbers_of_records == 9;
if($numbers_of_records == 10){
// inserts record with `scheduled_date`='2011-01-01'
}else{
// inserts record with the date latest date +1 day
}
How would I efficiently accomplish this?
Thanks
This will do the trick. It is simple and efficient.
<?php
// It is very bad to have floating values, especially for settings
// it is good to use some sort of factory or settings class
$maxDailyPosts = (int) SettingsFactory::getSettings()->get('maxDailyPosts');
$date = '2011-01-01';
// Load # of post for data
$numberOfRecords = (int) getNumberOfPostPerDate($date);
// Figure out the interval for increment
$dayInterval = ($numberOfRecords >= $maxDailyPosts ) ? 1 : 0;
//
$query = "INSERT INTO tbl (publish_date, ...) VALUES (DATE_ADD('$date', INTERVAL $dayInterval DAY), ...)";
?>
Related
I have run into a delayed processing time for a PHP program,
I have a MySQL record with over 1000 tables;
Each table is created once a new device is added, e.g assets_data_imeixx - to assets_data_imeixx1000th table
Each table contains about 45,000 rows of records inserted every 10 seconds,
Below is my PHP code to query the database and fetch all these records based on datetime.
Issue: The program executes without error but it takes about 1.3minutes to 4mins for very large records.
PHP Code:
$ms = mysqli connection string in config.php //$ms is OKAY
$user_id = '5';
$q = "SELECT * FROM `user_assets` WHERE `user`='".$user_id ."' ORDER BY `imei` ASC";
$r = mysqli_query($ms,$q);
$result = array(); //$result array to contain all data
while($row =mysqli_fetch_array($r)){
//fetch 7 days record
for ($i=1; $i < 7; $i++) {
$date = "-" . $i . " days";
$days_ago = date('Y-m-d', strtotime($date, strtotime('today')));
$sql1 = "SELECT * FROM assets_data_" . $row["imei"] . " WHERE dt_time LIKE '" . $days_ago . "%' LIMIT 1"; // its correct
//$result1 = $conn->query($sql1);
$result1 = mysqli_query($ms,$sql1);
$row2 = mysqli_fetch_array($result1);
echo $row['imei']." ".$row2['dt_server']."<br/>";
}
}
Above code fetches over 1000 devices from user_assets table, These IMEI each has its own table that contains over 45,000 records in each table of location data.
The for loop iterates over each IMEI table and records.
Above code runs without error but take so much time to complete, I want to find a solution to optimize and have code execute in a very short time max 5 seconds.
I need help and suggestions on optimizing and running this large scale of data and iteration.
(from Comment)
CREATE TABLE gs_object_data_863844052008346 (
dt_server datetime NOT NULL,
dt_tracker datetime NOT NULL,
lat double DEFAULT NULL,
lng double DEFAULT NULL,
altitude double DEFAULT NULL,
angle double DEFAULT NULL,
speed double...
(From Comment)
gs_object_data_072101424612
gs_object_data_072101425049
gs_object_data_072101425486
gs_object_data_072101445153
gs_object_data_111111111111111
gs_object_data_1234567894
gs_object_data_222222222222222
gs_object_data_2716325849
gs_object_data_2716345818
gs_object_data_30090515907
gs_object_data_3009072323
gs_object_data_3009073758
gs_object_data_352093088838221
gs_object_data_352093088839310
gs_object_data_352093088840045
gs_object_data_352121088128697
gs_object_data_352121088132681
gs_object_data_352621109438959
gs_object_data_352621109440203
gs_object_data_352625694095355
gs_object_data_352672102822186
gs_object_data_352672103490900
gs_object_data_352672103490975
gs_object_data_352672103490991
gs_object_data_352887074794052
gs_object_data_352887074794102
gs_object_data_352887074794193
gs_object_data_352887074794417
gs_object_data_352887074794425
gs_object_data_352887074794433
gs_object_data_352887074794441
gs_object_data_352887074794458
gs_object_data_352887074794474
gs_object_data_352887074813696
gs_object_data_352887074813712
gs_object_data_352887074813720
gs_object_data_352887074813753
gs_object_data_352887074813761
gs_object_data_352887074813803
900+ tables each having different location data.
Requirement: Loop through each table, fetch data for selected date range say:
"SELECT dt_server FROM gs_object_data_" . $row["imei"] . " WHERE dt_server BETWEEN '2022-02-05 00:00:00' AND '2022-02-12 00:00:00'";
Expected Result: Return result set containing data from each table containing information for the selected date range. That means having 1000 tables will have to be looped through each table and also fetch data in each table.
I agree with KIKO -- 1 table not 1000. But, if I understand the rest, there are really 2 or 3 main tables.
Looking at your PHP -- It is often inefficient to look up one list, then go into a loop to find more. The better way (perhaps 10 times as fast) is to have a single SELECT with a JOIN to do both selects at once.
Consider some variation of this MySQL syntax; it may avoid most of the PHP code relating to $days_ago:
CURDATE() - INTERVAL 3 DAY
After also merging the Selects, this gives you the rows for the last 7 days:
WHERE date >= CURDATE() - INTERVAL 7 DAY
(I did not understand the need for LIMIT 1; please explain.)
Yes, you can use DATETIME values as strings, but try not to. Usually DateTime functions are more efficient.
Consider "composite" indexes:
INDEX(imei, dt)
which will be very efficient for
WHERE imei = $imei
AND dt >= CURDATE() - INTERVAL 7 DAY
I would ponder ways to have less redundancy in the output; but that should mostly be done after fetching the raw data from the table(s).
Turn on the SlowLog with a low value of long_query_time; it will help you locate the worst query; then we can focus on it.
An IMEI is up to 17 characters, always digits? If you are not already using this, I suggest BIGINT since it will occupy only 8 bytes.
For further discussion, please provide SHOW CREATE TABLE for each of the main tables.
Since all those 1000 tables are the same it would make sense to put all that data into 1 table. Then partition that table on date, use proper indexes, and optimize the query.
See: Normalization of Database
Since you limit results to one user, and one row per device, it should be possible to execute a query in well below one second.
I have a basic table with orders, which have a field thats called created_at which is in timestamp format and I want to get the avarage on how many orders have been created per day.
Found a other similar qestion about something like mine question whichI have posted below in the hope that everybody understand what im trying to do.
//not working
$q = new \yii\db\Query();
$q->from('shopping_cart');
$total = $q->average('DATEDIFF(`created_at`, CURRENT_DATE())');
I believe it is more SQL related problem than Yii2. What you need (if I have understood it correctly) is:
count number of days from the beginning to today
count all the rows
divide these number to get the average.
I have tried this and it works fine
SELECT
count(*) / (select round((unix_timestamp() - (select min(created_at) from table)) / 86400))
from table;
back to Yii2: I believe you have to build this query manually
\Yii::$app->db->createCommand()
Method average in $q->average('DATEDIFF('created_at', CURRENT_DATE())'); just adds AVG(DATEDIFF('created_at', CURRENT_DATE())) to SQL command.
As Jiri Semmler said, what you want to do is about SQL not Yii.
All you need to do is find the count of records for the period you are interested in and divide it by the number of days of that period.
It can be something like
// Define period
$timeFrom = '2018-11-30';
$timeTo = '2018-12-02';
// Number of days for the period
$days = date_diff(date_create($timeFrom), date_create($timeTo))->format("%a");
// Query count of records between dates
$q = new \yii\db\Query();
$total = $q->from('order')
->where(['between', 'created_at', $timeFrom, $timeTo])
->count();
// Find average records per day
$average = $total / $days;
If you have Order model class:
// Query count of records between dates
$total = Order::find()
->where(['between', 'created_at', $timeFrom, $timeTo])
->count();
I have this Cron Job that I want to run once a day. It checks to see if it's a new day, and if it is, it inserts a new row into my database table. I use this to keep track of the daily sales for each item on my online store.
I also need it to grab the day from the last row for that product_id, increment it by 1 and then include it in my INSERT query. This is so that I can easily keep track of the current day for each product.
The MAX(time_stamp) is also illegal code, however I don't know what to replace it with. I use it to select the latest day for each item, so that the Cron Job doesn't select every single row.
I've never used Cron Jobs before, meaning that I'm a noob so please be easy on me :)
Thanks for the help! :)
Here's my code:
PHP:
// Checks if past a day
$days = DB::fetch("SELECT * FROM `daily_sales` WHERE MAX(time_stamp)<=CURRENT_DATE - INTERVAL 1 DAY;");
$numrows = count($days);
// If there is a new day
if ($numrows > 0) {
foreach ($days as $i => $day) {
// Adds a fresh day with 0 sales
$sales = 0;
$product_id = $day->product_id;
$day = // Get the day from the last row, and add 1
$time_stamp = time();
DB::query('INSERT INTO `daily_sales` (`product_id`, `sales`, `day`, `time_stamp`) VALUES (?, ?, ?, ?);', array($product_id, $sales, $day, $time_stamp));
}
}
daily_sales: (the DB table)
id | product_id | day | time_stamp | sales
you have to open cron job, add new job and define
how to open cron tab
crontab -e
add following to your cron tab
0 0 * * * php /full/path/to/script.php
OR
you can use mysql triggers http://www.mysqltutorial.org/mysql-triggers.aspx that will allow you to make "audit" table which will contain daly sales. So you can insert/update audit table by using "after insert".
hope this will help
I am begginer using PHP and MSQL and I can not filter the results of my table correctly. I want to show me all the fields in the table that keep the condition that there are still two hours for the date and time I have stored.
I used this query but does not work well, because if the current month is 12 and is stored in the DB 1 is not shown, as if even the time stored is less than 2 hours even another day:
putenv('TZ=Europe/Madrid');
$anoE = date("y");
$mesE = date("m");
$diaE = date("d");
$horaE = date("H");
...
$consulta = "SELECT tipo,nombre,descripcion,hora,minuto ,lugar,duracion,fecha,horacero,id,ano,dia,mes,equipo FROM ".$tabla." WHERE ".$anoE." <= `ano` AND ".$mesE." <= `mes` AND ".$diaE." <= `dia` AND ".$horaE." <= `(hora+2)` ORDER BY ano,mes,dia,hora,minuto";
I don't know how i can write the while condition for the query shows fields which remain them two hours beyond the current time (same day)
If your field type is DATETIME (if not, make it) you can use simple query:
SELECT *
FROM table_name
WHERE datetime_field_name BETWEEN DATE_SUB(NOW(), INTERVAL 2 HOUR) AND NOW()
Which will select all records that are between now - 2 hours and now.
I am trying to update my table with a randomly generated data field with this script
for($i = 0; $i < 10 ; $i++){
$date = rand(1,30).'/'.rand(1,12).'/'.'15';
$dbh = new PDO('mysql:host=localhost;dbname=qp', 'root', '123456');
$sth2 = $dbh->prepare("update r_data set the_date = '$date' where transaction_type = 'send'");
echo $date.'<br/>';
$sth2->execute();
}
but the field is only updated with only one random string i.e 18/5/15 but the variable $date has generated random dates as i want.
Why is the date field not being updated with random data?.
You are updating the same row(s) each time. Once the loop is done, you'll only see the last 'random' value in the database, as all preceding values will have been replaced.
You'll have to qualify the rows your are updating to be different each iteration of your loop if you want to see different dates for different rows.
Alternatively, have MySQL insert random dates for you:
update r_data set
the_date = DATE '2015-01-01' + INTERVAL FLOOR(RAND() * 365) DAY
where transaction_type = 'send'
This'll insert a different random date value for each selected row; no loop required.