mysql按章 mysql按時間範圍分割槽

2021-10-21 01:48:15 字數 2970 閱讀 2223

1、問題描述

mysql的開發人員經常按時間範圍分割槽不知道時間上怎麼寫,筆者把總結歸納下來,能幫助自已和分享成果。

2、解決問題

mysql對時間範圍分割槽,可以是按年,按月,按天,按分,按秒,如下案例,這裡注意點:範圍分割槽是乙個開始是開區間,結束是閉區間:[0,10),[10,20),[20,30).....

2.1、按年

create table sales_year

(date datetime

)engine = innodb

partition by range(year(date))

(partition p2020 values less than (2020),

partition p2021 values less than (2021),

partition p2022 values less than (2022),

partition pmax values less than (maxvalue)

2.1、按月create table sales_month

(date datetime

)engine = innodb

partition by range(month(date))

(partition p202001 values less than (202001),

partition p202002 values less than (202002),

partition p202003 values less than (202003),

partition pmax values less than (maxvalue)

2.2、按天create table sales_day

(date datetime

)engine = innodb

partition by range(to_days(date))

(partition p20200101 values less than (20200101),

partition p20200202 values less than (20200102),

partition p20200203 values less than (20200103),

partition pmax values less than (maxvalue)

2.3、按分

create table sales_second

(date timestamp

)engine = innodb

partition by range(to_seconds(date))

(partition p1 values less than (63769153669),

partition p2 values less than (63769153734),

partition p3 values less than (63769153752),

partition pmax values less than (maxvalue)

2.4、按秒

create table sales_timestamp

(id int,

date timestamp

partition by range ( unix_timestamp(date) )

(partition p1 values less than ( unix_timestamp('2020-10-02 00:00:00') ),

partition p2 values less than ( unix_timestamp('2020-10-03 00:00:00') ),

partition p3 values less than ( unix_timestamp('2020-10-04 00:00:00') ),

partition p4 values less than ( unix_timestamp('2020-10-05 00:00:00') ),

partition p5 values less than ( unix_timestamp('2020-10-06 00:00:00') ),

partition p6 values less than ( unix_timestamp('2020-10-07 00:00:00') ),

partition p7 values less than ( unix_timestamp('2020-10-08 00:00:00') ),

partition p8 values less than ( unix_timestamp('2020-10-09 00:00:00') ),

partition p9 values less than ( unix_timestamp('2020-10-10 00:00:00') ),

partition p10 values less than (unix_timestamp('2020-10-11 00:00:00') ),

partition pmax values less than (maxvalue)

insert into sales_timestamp(id,date) values( 1,current_timestamp());

insert into sales_timestamp(id,date) values( 2,current_timestamp());

insert into sales_timestamp(id,date) values(3,current_timestamp());

insert into sales_timestamp(id,date) values(4,current_timestamp());

insert into sales_timestamp(id,date) values( 5,current_timestamp());

insert into sales_timestamp(id,date) values( 5,'2020-10-05 00:00:00');

mysql 按時間搜尋 按日期搜尋mysql效能

我有乙個包含大約1億條記錄的大表,其字段為start date和end date,具有date型別.我需要檢查一些日期範圍的重疊次數,比如介於2013 08 20和2013 08 30之間,所以我使用了.select count from mytable where end date 2013 08...

MySQL按時間分組

select from unixtime time y m d as time from 表名 where 1 group by time 如果需要詳細資訊,再遍歷時間獲取 類似這種形式 這個是我在工作中的乙個頁面展示 from unixtime的語法 from unixtime unix time...

mysql時間查詢 MySQL按時間查詢

mysql 今天select from 表名 where to days 時間欄位名 to days now 昨天select from 表名 where to days now to days 時間欄位名 1 近7天select from 表名 where date sub curdate int...