P
P
Puma Thailand2013-07-09 08:36:50
MySQL
Puma Thailand, 2013-07-09 08:36:50

How to do quick grouping by day and hour in mysql?

Here is the standard path.
accross.su/blog/view/131
And it would work if there were not a million records.
How to do quick temporary groupings in mysql with a large number of records?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Savostin, 2013-07-09
@savostin

Add columns with calculated values ​​(can be triggers when inserting) into int and indexes on them.

N
Nikita Gusakov, 2013-07-09
@hell0w0rd

indices?

S
script88, 2013-07-09
@script88

Might be worth looking into partitions? dev.mysql.com/doc/refman/5.5/en/partitioning-pruning.html

P
Push_Ok, 2013-07-09
@Push_Ok

quickly possible only if you make storage separate
date | count
and increment with a common insert. (update order_counter set count=count+1 where date=date_format(date, 'and what type of grouping do you need'), you need to check what replace or insert will do with on duplicate key for the very first insert, ideally instead of update replace then record you won't need to check what is there,
or describe the task in more detail, if there is still some kind of filter in the selection,
then the solution with a separate counter will not help.

K
KEKSOV, 2013-07-09
@KEKSOV

Try the following option - 1. This is for grouping by day
SELECT ...
FROM ...
GROUP BY UNIX_TIMESTAMP ( field with date and time ) DIV 24 * 3600
2. This is for grouping by hour
SELECT ...
FROM ...
GROUP BY UNIX_TIMESTAMP ( field with date and time ) DIV 3600
If it still slows down (most likely it will), then these two fields need to be added to the table and an index built on them

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question