Most recent rows at least 1 hour apart in MySQL - mysql

I am collecting data every five minutes and entering it into a MySQL database. I would like to extract the most recent row that's over 2 hours old, followed by over 1 hour old, followed by the most recent row.
I was thinking something along the lines of the following, but I think this will get me the last row of each hour. Meaning if I run the query at 8:05 I might get back rows from 6:57, 7:57 and 8:02, the last 2 of which are much less than an hour apart.
SELECT *
FROM (
SELECT *
FROM mytable
ORDER BY Date DESC
GROUP BY HOUR(Date) LIMIT 3
) x
ORDER BY Date ASC
Thanks for any help or suggestions you can provide.

SELECT * FROM (
SELECT * FROM mytable
WHERE `Date`<DATE_SUB(NOW(), INTERVAL 2 HOUR)
ORDER BY DATE DESC LIMIT 1
)
UNION
SELECT * FROM (
SELECT * FROM mytable
WHERE `Date`<DATE_SUB(NOW(), INTERVAL 1 HOUR)
ORDER BY DATE DESC LIMIT 1
)
UNION
SELECT * FROM (
SELECT * FROM mytable
ORDER BY DATE DESC LIMIT 1
)
ORDER BY `Date`ASC

Related

Need to optimize SQL Query - Taking lot a of time for execution

We have a below query which takes approximately 6-8 secs to execute.
Total number of records : 522954
(SELECT
*
FROM
tbl_insights_copy
WHERE insightscat = 21
AND submitedon >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
ORDER BY submitedon DESC
LIMIT 5)
UNION
(SELECT
*
FROM
tbl_insights_copy
WHERE insightscat = 22
AND submitedon >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
ORDER BY submitedon DESC
LIMIT 5)
UNION
(SELECT
*
FROM
tbl_insights_copy
WHERE insightscat = 23
AND submitedon >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
ORDER BY submitedon DESC
LIMIT 5)
UNION
(SELECT
*
FROM
tbl_insights_copy
WHERE insightscat = 24
AND submitedon >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
ORDER BY submitedon DESC
LIMIT 5)
Can someone help to optimize this query as to reduce the execution time.
Thanks in advance.
The only thing you are changing between one select and another, is the filter value of the column insightscat I am not sure that this is what you want but....
You may try the IN instruction for this. Example:
SELECT
*
FROM
tbl_insights_copy
WHERE insightscat in (20,21,22,23,24)
AND submitedon >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
ORDER BY submitedon DESC
For this query:
SELECT ic.*
FROM tbl_insights_copy ic
WHERE insightscat = 21 AND
submitedon >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
ORDER BY submitedon DESC
LIMIT 5
You want an index on tbl_insights_copy(insightscat, submittedon).
This should work for all the subqueries. This is probably the best approach with MySQL.
SELECT t1.*
FROM (SELECT t.* ,ROW_NUMBER() OVER (ORDER BY insightscat) AS Row
FROM
(select * from
tbl_insights_copy
WHERE insightscat in (20,21,22,23,24)
AND submitedon >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
ORDER BY submitedon DESC ) as t ) t1
WHERE Row <= 5
Without SHOW CREATE TABLE, I am going to have to guess that you do not have the optimal
INDEX(insightscat, submitedon)
Since the SELECTs are distinct, use UNION ALL instead of the default UNION DISTINCT. This will avoid an unnecessary (but fast) de-dup pass over the 20 rows.
If you want 20 rows
If so, my suggestions above might be best.
If you want 6 rows
If you want only the latest 5 from any of those insightscats, then there are 3 possibilities.
Plan A
What Developer90 says in his Comment.
Plan B
( SELECT ... ORDER BY ORDER BY submitedon DESC LIMIT 5)
UNION ALL
( SELECT ... ORDER BY ORDER BY submitedon DESC LIMIT 5)
UNION ALL
( SELECT ... ORDER BY ORDER BY submitedon DESC LIMIT 5)
UNION ALL
( SELECT ... ORDER BY ORDER BY submitedon DESC LIMIT 5)
ORDER BY ORDER BY submitedon DESC LIMIT 5;
Each SELECT is very fast with my index. Then, the 20 rows of the UNION get sorted again and LIMIT 5 is applied. Again, very fast.
Using the IN as suggested by Developer90, may or may not effectively use my index. What version are you using?
Plan C
This option is hit or miss. That is, its performance depends heavily on the distribution of the data as to whether it will be very fast or very slow: Developer90 + INDEX(submitedon) (not including insightscat).

mySQL UNION clause with two ORDER BY clause

here's the problem
I need to fetch rows from my table where date created is less than 24 hours ago and ORDER them by likes and then UNION them with the rest of rows in the same table but I want the remaining rows to be ORDERED by date created.
in other words I need separate ORDER BY clause for each SELECT.
to be more clear, it's something like this:
SELECT *
FROM (SELECT *
FROM 'table'
WHERE 'date_created' > timestampadd(hour, -24, now())
ORDER BY 'likes' DESC
UNION
SELECT *
FROM 'table'
WHERE
ORDER BY date_created DESC ) AS Results
LIMIT 10 OFFSET 0
thanks for this awesome community :)
Try this:
SELECT *, (date_created > timestampadd(hour, -24, now()) AS recent
FROM table
ORDER BY recent DESC, IF(recent, likes, 0) DESC, IF(!recent, date_created, 0) DESC
LIMIT 10 OFFSET 0

Select with 'negative' offset

I need to select 40 rows with date from today and 10 records with older date, ordered by date.
If MySQL supported negative offset, it would look like this:
SELECT * FROM `mytable` WHERE `date` >= '2013-10-29' ORDER BY date LIMIT -10, 40;
Negative offset is not supported. How can I solve the problem? Thanks!!!
Use UNION to combine two queries:
(
SELECT *
FROM mytable
WHERE date < '2013-10-29'
ORDER BY date DESC
LIMIT 10
) UNION ALL (
SELECT *
FROM mytable
WHERE date >= '2013-10-29'
ORDER BY date
LIMIT 40
)
ORDER BY date -- if results need to be sorted

Count SQL results form first query in second query in single statement

Here's my SQL statement:
(select * from items
where items.created > curdate() - interval 2 week
order by items.created desc limit 0,10000000000)
union all
(select * from items
where items.created < curdate() - interval 2 week
order by items.popularity desc limit 0,15)
I'm trying to figure out a way to limit the entire result of the query to a certain number (say 25). As it is now, this result returns an unlimited number for the first result (which is what I want), then returns 15 for the second result. I want to be able to limit the whole query so that even if the first result returns 8, the second result returns 17, total 25.
I believe to do this, I have to use count() somehow in the first query, then subtract that from the total I want and use that number as the 2nd query's limit. I have no idea how this is done.
Thanks in advance!
Here is the required query -
select *
from
((select * from items
where items.created > curdate() - interval 2 week
order by items.created desc limit 0,10000000000)
union all
(select * from items
where items.created < curdate() - interval 2 week
order by items.popularity desc)) t
limit 0,25
Another select:
select * from
(
(select * from items
where items.created > curdate() - interval 2 week
order by items.created desc limit 0,10000000000)
union all
(select * from items
where items.created < curdate() - interval 2 week
order by items.popularity desc)
) uniond_tables_alias
limit 25
The uniond_tables_alias is an alias for the uniond section, you can choose any name you want.
No need for nested queries, simply do:
(select * from items
where items.created > curdate() - interval 2 week
order by items.created desc) # remove LIMIT here
UNION ALL
(select * from items
where items.created < curdate() - interval 2 week
order by items.popularity desc) # remove LIMIT here
LIMIT 25; # add LIMIT here
This'll return the 25 first results from the first SELECT if there's at least 25. Otherwise it will fill up the remaining results with the second SELECT results until the limit of 25 is reached.
SELECT 1 AS sortkey, * from items ....
UNION ALL
SELECT 2 AS sortkey, * from items ....
ORDER BY sortkey, etc.
LIMIT 25

MySQL: Getting "busiest" or "most popular" hour from a datetime field?

Consider the following table which has the fields - id (int) and date_created (datetime):
id date_created
1 2010-02-25 12:25:32
2 2010-02-26 13:40:37
3 2010-03-01 12:02:22
4 2010-03-01 12:10:23
5 2010-03-02 10:10:09
6 2010-03-03 12:45:03
I want to know the busiest/most popular hour of the day for this set of data. In this example, the result I'm looking for would be 12.
Ideas?
To get just the most popular hour, use this query
select date_format( date_created, '%H' ) as `hour`
from [Table]
group by date_format( date_created, '%H' )
order by count(*) desc
limit 1;
If you want to look at all the data, go with this one
select count(*) as num_records
, date_created
, date_format( date_created, '%H' ) as `hour`
from [Table]
group by `hour`
order by num_records desc;
If you want something a little more flexible, perhaps to the half hour, or quarter hour, you can do the following:
SELECT floor(time_to_sec(date_created)/3600),count(*) AS period
FROM table GROUP BY period ORDER BY c DESC
If you want the most popular 2 hour interval, use 7200. The most popular 15 minute interval, use 900. You just need to remember you are dealing with seconds (3600 seconds in an hour).
Use the hour() function to extract the hour, then do the usual aggregation:
SELECT count(hour(date_created)) AS c, hour(date_created) AS h FROM table GROUP BY h ORDER BY c DESC;
I like both Simon and Peter's answers, but I can't select both as accepted. I combined the 2 to make a cleaner query that only returned the popular hour (I don't need the counts).
SELECT hour(date_created) AS h
FROM my_table
GROUP BY h
ORDER BY count(*) DESC
LIMIT 1
You could try this:
SELECT
DATE_FORMAT(date,'%H') as hours,
count(*) as count
FROM
myTable
GROUP BY
hours
ORDER BY
count DESC