MySQL Query time optimization

Source: Internet
Author: User
Tags mysql query

Select COUNT (*) from table where buytime>= ' 2011-5-31 00:00:00 ' and buytime<= ' 2011-6-6 23:59:59 ' GROUP by ItemName

Select COUNT (*) from table where Buytime>=unix_stamp (' 2011-5-31 00:00:00 ') and Buytime<=unix_stamp (' 2011-6-6 23:59:59 ') group by ItemName.

In MySQL, there are swap functions for traditional time functions and Unix timestamps. Unix_timestamp (DateTime) and From_unixtime (Unixtime)

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------

Previously said MySQL Time field has date,time,datetime,timestamp, etc., in the actual query time period, we often store the entire field, for example, like 2011-03-10 00:00:00, there are separate storage year, month, Day Way, Or you can use the right () and other characters to intercept the function for query operation. Anyway, we are all going to query for a period of time. I recently in the game props purchase inquiries, as required, we have to follow the way of week, check the sales of props for a week. This is used in the following way:

Select COUNT (*) from table where buytime>= ' 2011-5-31 00:00:00 ' and buytime<= ' 2011-6-6 23:59:59 ' GROUP by ItemName

This is a more traditional way, we need to specify the start and end time, often use >,between,<,= to do time-period planning, but we sent here buytime is not the integer data, efficiency is low, especially after the data reached millions speed obviously down. So we take a second approach to solve:

Unix time stamp

Each time corresponds to a unique UNIX timestamp, time starts from 1970-1-1 00:00:00, every second, plus 1, in MySQL there is a traditional time function and the Unix time stamp interchange function. Unix_timestamp (DateTime) and From_unixtime (Unixtime)

Mysql> Select Unix_timestamp (' 2011-8-2 8:50:34 ');
+------------------------------------+
| Unix_timestamp (' 2011-8-2 8:50:34 ') |
+------------------------------------+
| 1312246234 |
+------------------------------------+
1 row in Set (0.03 sec)

Mysql> Select From_unixtime (1312246234);
+---------------------------+
| From_unixtime (1312246234) |
+---------------------------+
| 2011-08-02 08:50:34 |
+---------------------------+
1 row in Set (0.00 sec)

So pull, we replace the original Time field with the Unix integer field now, by comparing the integers, we can greatly improve the efficiency, when we query, we convert the start end time to the Unix timestamp.

Select COUNT (*) from table where Buytime>=unix_stamp (' 2011-5-31 00:00:00 ') and Buytime<=unix_stamp (' 2011-6-6 23:59:59 ') group by ItemName.

However, in this way, the trouble is that the query will need to flip the time once.

MySQL Query time optimization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.