Display latest temperature by location quickly - php

The Table I have has temperatures, locations and datetime and other data that isn't currently used. Message is the temperature and locationmap is where the sensor is ie, lounge, kitchen, outside!)
Query :
SELECT
t1.*
FROM
temperatures t1
JOIN (
SELECT
locationmap
, MAX(timeof) AS timeof
FROM
temperatures
GROUP BY
locationmap
) AS t2
ON t1.locationmap = t2.locationmap AND t1.timeof = t2.timeof
WHERE
DATE(t1.timeof) BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 MINUTE) AND CURDATE()";
Resulting PHP code to display current temperatures;
// Print out result
while($row = mysql_fetch_array($result)){
echo $row['locationmap']. " - ". $row['message']. " #". $row['timeof'];
echo "<br />";
}
Just want it to display the latest temperature for each location, which it currently does, but I was wondering if this the best optimised query for speed, as at the moment it takes 7secs to display results!? Mind u I have a slow server, but I'm the only user.
Thx.
FYI.

Can't say without table create statements, more information about indexes and storage engine
And please make your SQL code more readable in your PHP code, It's damn hard to read like this.
SELECT
t1.*
FROM
temperatures t1
JOIN (
SELECT
locationmap
, MAX(timeof) AS timeof
FROM
temperatures
GROUP BY
locationmap
) AS t2
ON t1.locationmap = t2.locationmap AND t1.timeof = t2.timeof
WHERE
DATE(t1.timeof) BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 MINUTE) AND CURDATE()
The same query but more readable
And thanks for the minus 1, because i tryed to help...
This query should run sightly faster
SELECT
t1.locationmap
, t1.message
, t1.timeof
FROM
temperatures AS t1
JOIN (
SELECT
locationmap
, MAX(timeof) AS timeof
FROM
temperatures
GROUP BY
locationmap
) AS t2
ON t1.locationmap = t2.locationmap AND t1.timeof = t2.timeof
WHERE
DATE(t1.timeof) BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 MINUTE) AND CURDATE()
And make sure you have a covering BTREE (if you use MyISAM or innodb indexes are already BTREE) index on locationmap, timeof and message (optional) (be sure you build the index like this)
Please note that inserts, updates and deleted run sightly slower because of the covering index.
So try to multiinsert to keep the speed up.

Related

Using a formulla through mysql queries: error

I have created a database of kittens where I need to use the following formula. In this regard I have two tables: birth and disposal.
Birth table contains id, dob, owner, date of purchase
disposal table contains id, date of disposal (dodisposal), cause of death, sold, treatment
I am now trying use a formula for the two table using the following MySQL query but it is not working.
Select birth.owner, (((select count(disposal.id) from disposal WHERE
dodisposal BETWEEN DATE_SUB(NOW(), INTERVAL 600 DAY) AND NOW()) /
(select count(birth.id) from birth where birth.id not in
(select disposal.id from disposal)
)
) * 100)
from birth left join disposal on
disposal.brandnumber = birth.id group by birth.owner
but I keep getting same results for all owners:
ie
rita : 79.6
sunita : 79.6
Smith : 79.6
The result I am expecting should be through the following formula:
Number of deaths in the current year / total number of live cats * 100
I found a solution to this problem by making two separate views and then their results using a MySQL query.
create view cats as select id, count(disposal.id) from disposal WHERE
dodisposal BETWEEN DATE_SUB(NOW(), INTERVAL 600 DAY) AND NOW()) as dead
create view livecates as select count(birth.id) from birth left join disposal on disposal.id = birth.id where birth.id not in (select disposal.id from disposal) as live
select livecats.id, (dead/live * 100) from livecats left join cats on livecats.id = cats.id group by livecats.id

Optimizing the SQL Query to get data from large amount MySQL database

I am having a problem getting data from a large amount MySQL database.
With the below code it is ok to get the list of 10K patients and 5K appointments which is our test server.
However, on our live server, the number of patients is over 100K and the number of appointments is over 300K and when I run the code after a while it gives 500 error.
I need the list of the patients whose patient_treatment_status is 1 or 3 and has no appointment after one month from their last appointment. (The below code is working for small amount of patients and appointments)
How can I optimise the first database query so there will be no need the second database query in the foreach loop?
<?php
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 0);
require_once('Db.class.php');
$patients = $db->query("
SELECT
p.id, p.first_name, p.last_name, p.phone, p.mobile,
LatestApp.lastAppDate
FROM
patients p
LEFT JOIN (SELECT patient_id, MAX(start_date) AS lastAppDate FROM appointments WHERE appointment_status = 4) LatestApp ON p.id = LatestApp.patient_id
WHERE
p.patient_treatment_status = 1 OR p.patient_treatment_status = 3
ORDER BY
p.id
");
foreach ($patients as $row) {
$one_month_after_the_last_appointment = date('Y-m-d', strtotime($row['lastAppDate'] . " +1 month"));
$appointment_check = $db->single("SELECT COUNT(id) FROM appointments WHERE patient_id = :pid AND appointment_status = :a0 AND (start_date >= :a1 AND start_date <= :a2)", array("pid"=>"{$row['id']}","a0"=>"1","a1"=>"{$row['lastAppDate']}","a2"=>"$one_month_after_the_last_appointment"));
if($appointment_check == 0){
echo $patient_id = $row['id'].' - '.$row['lastAppDate'].' - '.$one_month_after_the_last_appointment. '<br>';
}
}
?>
First off, this subquery likely does not do what you think it does.
SELECT patient_id, MAX(start_date) AS lastAppDate
FROM appointments WHERE appointment_status = 4
Without a GROUP BY clause, that subquery will simply take the maximum start_date of all appointments with appointment_status=4, and then arbitrarily pick one patient_id. To get the results you want you'll need to GROUP BY patient_id.
For your overall question, try the following query:
SELECT
p.id, p.first_name, p.last_name, p.phone, p.mobile,
LatestApp.lastAppDate
FROM
patients p
INNER JOIN (
SELECT patient_id,
MAX(start_date) AS lastAppDate
FROM appointments
WHERE appointment_status = 4
GROUP BY patient_id
) LatestApp ON p.id = LatestApp.patient_id
WHERE
(p.patient_treatment_status = 1
OR p.patient_treatment_status = 3)
AND NOT EXISTS (
SELECT 1
FROM appointments a
WHERE a.patient_id = p.patient_id
AND a.appointment_status = 1
AND a.start_date >= LatestApp.lastAppDate
AND a.start_date < DATE_ADD(LatestApp.lastAppDate,INTERVAL 1 MONTH)
)
ORDER BY
p.id
Add the following index, if it doesn't already exist:
ALTER TABLE appointments
ADD INDEX (`patient_id`, `appointment_status`, `start_date`)
Report how this performs and if the data appears correct. Provide SHOW CREATE TABLE patient and SHOW CREATE TABLE appointments for further assistance related to performance.
Also, try the query above without the AND NOT EXISTS clause, together with the second query you use. It is possible that running 2 queries may be faster than trying to run them together, in this situation.
Note that I used an INNER JOIN to find the latest appointment. This will result in all patients that have never had an appointment to not be included in the query. If you need those added, just UNION the results those found by selecting from patients that have never had an appointment.

How to calculate difference between values coming from the same row in mysql

I am trying to calculate the difference of values list coming from a database.
I would like to achieve it using php or mysql, but I do not know how to proceed.
I have a table named player_scores. One of its rows contains the goals scored.
Ex.
pl_date pl_scores
03/11/2014 18
02/11/2014 15
01/11/2014 10
I would like to echo the difference between the goals scored during the matches played in different dates.
Ex:
pl_date pl_scores diff
03/11/2014 18 +3
02/11/2014 15 +5
01/11/2014 10 no diff
How can I obtain the desired result?
You seem to want to compare a score against the score on a previous row.
Possibly simplest if done using a a sub query that gets the max pl_date that is less than the pl_date for the current row, then joining the results of that sub query back against the player_scores table to get the details for each date:-
SELECT ps1.pl_date, ps1.pl_scores, IF(ps2.pl_date IS NULL OR ps1.pl_scores = ps1.pl_scores, 'no diff', ps1.pl_scores - ps1.pl_scores) AS diff
FROM
(
SELECT ps1.pl_date, MAX(ps2.pl_date) prev_date
FROM player_scores ps1
LEFT OUTER JOIN player_scores ps2
ON ps1.pl_date > ps2.pl_date
GROUP BY ps1.pl_date
) sub0
INNER JOIN player_scores ps1
ON sub0.pl_date = ps1.pl_date
LEFT OUTER JOIN player_scores ps2
ON sub0.prev_date = ps2.pl_date
There are potentially other ways to do this (for example, using variables to work through the results of an ordered sub query, comparing each row with the value stored in the variable for the previous row)
SELECT score FROM TABLE WHERE DATE = TheDateYouWant
$score = $data['score'];
SELECT score FROM TABLE WHERE date = dateYouWant
$difference = $score - $data['score'];
Something like this?
You could use two queries, one to get the value to use in the comparison (in the example below is the smaller number of scores) and the second one to get the records with a dedicated column with the difference:
SELECT MIN(pl_scores);
SELECT pl_date, pl_scores, (pl_scores - minScore) as diff FROM player_scores;
Or, using a transaction (one query execution php side):
START TRANSACTION;
SELECT MIN(Importo) FROM Transazione INTO #min;
SELECT Importo, (Importo - #min) as diff FROM Transazione;
select *,
coalesce(
(SELECT concat(IF(t1.pl_scores>t2.pl_scores,'+',''),(t1.pl_scores-t2.pl_scores))
FROM tableX t2 WHERE t2.pl_date<t1.pl_date ORDER BY t2.pl_date DESC LIMIT 1)
, 'no data' ) as diff
FROM tableX t1
WHERE 1
order by t1.pl_date DESC

MySql database optimization on inner join

I am facing problem while running the below query. It takes longer time for short month duration and time out for longer duration.
SELECT DISTINCT rcs.ID,
rcs.UniqueID,
rcs.ReferanceNo,
rcs.OrderDateActual,
rcs.DelivaredToCus,
rcs.PacketNumber,
rcs.PackingCharge,
rcs.TransportCharge,
rcs.OtherAdjustment,
SUM(rcsd.TotalPrice) AS TotalPrice
FROM regular_challan_sales rcs
LEFT JOIN regular_challan_sales_details rcsd ON rcsd.UniqueID = rcs.UniqueID
WHERE rcs.OrderDateActual >= '2013-12-01'
AND rcs.OrderDateActual <= '2014-05-01'
GROUP BY rcs.UniqueID
ORDER BY rcs.OrderDateActual ASC
I need a quick solution please.

Collecting two separate column results from mySQL query

I have two database tables containing similar data - one is the amount of work completed by a process, the other is the amount of errors made by that process.
I would like to display the percentage of right first time work.
My query for counting the number of items by process is:
SELECT Counter.Process, count( Counter.Process ) AS count
FROM Counter WHERE (TIME BETWEEN '2012-07-01 00:00:00' AND '2012-07-06 23:59:59')
GROUP BY Counter.Process
ORDER BY count DESC
The above gives me a list similar to:
Process | count
-------------------
Process A | 40
Process B | 32
Process C | 102
Process D | 23
I have a second table with the same fields in it for recording errors, called 'Errors'.
By using the same query (but changing the table names obviously) I can get a list of processes and errors.
What I want to do is create a PHP web page showing the percentage of errors per process.
My problem is if I create the two queries separately, when I loop through them using a PHP Where loop I don't know how to make the calculation to show the percentage.
I tried using a UNION between the two SQL queries, but I can't get it to show the two different count readings against the process.
Ideally, I'd like a way to show the data like this:
Process | Counter.count | Errors.count
Process A 40 2
Process B 32 0
Process C 102 18
Process D 23 8
I would really appreciate any advice/suggestions as to how I can accomplish this - if you need more info on my database tables, please let me know
Thanks for reading
S
I did not run it so there might be mistakes. you can make the two selects, and left join the results:
select s.process, s.success_count, e.error_count
from
(SELECT Counter.Process as process, count( Counter.Process ) AS success_count
FROM Counter
WHERE (TIME BETWEEN '2012-07-01 00:00:00' AND '2012-07-06 23:59:59')
GROUP BY Counter.Process
) s left join
(SELECT Errors.Process, count( Errors.Process ) AS error_count
FROM Errors
WHERE (TIME BETWEEN '2012-07-01 00:00:00' AND '2012-07-06 23:59:59')
GROUP BY Errors.Process
) e
on (s.Process = e.process)
I personally prefer to run situations like this through PHP as it confuses me less, so I will explain how I think I would do it in PHP (I say 'think' because I might not know something about your database structure or system specifications that would make me thing otherwise):
$rows = array();
$res = mysql_query('SELECT Counter.Process, count( Counter.Process ) AS count
FROM Counter WHERE (TIME BETWEEN '2012-07-01 00:00:00' AND '2012-07-06 23:59:59')
GROUP BY Counter.Process
ORDER BY count DESC');
while ($arr = mysql_fetch_assoc($res))
{
$rows[] = $arr;
}
$res2 = mysql_query('SELECT Errors.Process, count( Errors.Process ) AS count
FROM Counter WHERE (TIME BETWEEN '2012-07-01 00:00:00' AND '2012-07-06 23:59:59')
GROUP BY Errors.Process
ORDER BY count DESC');
while ($arr2 = mysql_fetch_assoc($res2)
{
for ($i=0; $i<count($rows); $i++)
{
if ($arr2["Process"] == $data[$i]["process"])
{
break;
}
//Do your functions with $arr and $data[$i] at this point...
}
}
Doing it without a subquery should be simple:
SELECT
Counter.Process
, COUNT(Counter.Process) AS ProcessCount
, COUNT(Errors.Process) AS ProcessErrors
FROM
Counter
LEFT JOIN
Errors
ON Counter.Process = Errors.Process
WHERE
Counter.TIME BETWEEN '2012-07-01 00:00:00' AND '2012-07-06 23:59:59'
GROUP BY
Counter.Process
You can do an explain on both queries to see which is the best for your data.
-edit: I've changed it to a LEFT JOIN so you don't miss rows if a process has no errors.
You could use a sub-query:
SELECT Counter.Process, count( Counter.Process ) AS Count,
(SELECT count(Errors.Process)
FROM Errors
WHERE Process=Counter.Process
AND (TIME BETWEEN '2012-07-01 00:00:00' AND '2012-07-06 23:59:59')) AS Errors
FROM Counter WHERE (TIME BETWEEN '2012-07-01 00:00:00' AND '2012-07-06 23:59:59')
GROUP BY Counter.Process
ORDER BY count DESC
Regards,
Neil.

Categories