Mysql 日誌表按照時間分割槽

2021-06-22 10:48:01 字數 3906 閱讀 6922

1 典型的時間分割槽

drop table pss_log;

create table `pss_log` (

`id` bigint(18) not null auto_increment,

`method_name` varchar(100) not null comment '方法名',

`input` text comment '方法輸入',

`output` text comment '方法輸出',

`client_ip` varchar(16) default null comment '呼叫ip',

`server_ip` varchar(16) default null comment '伺服器ip',

`invoked_by` varchar(80) default null comment '呼叫方',

`create_time` timestamp not null default current_timestamp on update current_timestamp comment '建立時間',

`ticket` varchar(36) not null comment '呼叫id',

`result_code` varchar(10) default null comment '方法結果碼,用於表示是否成功',

`version` varchar(20) default null comment '客戶端呼叫版本',

`time_used` int(10) default null comment '服務端耗時',

primary key (`id`,`create_time`),

key `idx_pss_log_create_time` (`method_name`,`create_time`,`invoked_by`)

) engine=innodb auto_increment=1 default charset=utf8

partition by range(unix_timestamp(`create_time`))

(partition  p20140701values less than (unix_timestamp('2014-07-01 00:00:00'))  engine = innodb,

partition  p20140801values less than (unix_timestamp('2014-08-01 00:00:00'))  engine = innodb,

partition  p20140901values less than (unix_timestamp('2014-09-01 00:00:00'))  engine = innodb,

partition  p20141001values less than (unix_timestamp('2014-10-01 00:00:00'))  engine = innodb,

partition  p20141101values less than (unix_timestamp('2014-11-01 00:00:00'))  engine = innodb,

partition  p20141201values less than (unix_timestamp('2014-12-01 00:00:00'))  engine = innodb,

partition  p20150101values less than (unix_timestamp('2015-01-01 00:00:00'))  engine = innodb,

partition  p20150201values less than (unix_timestamp('2015-02-01 00:00:00'))  engine = innodb,

partition  p20150301values less than (unix_timestamp('2015-03-01 00:00:00'))  engine = innodb,

partition  p20150401values less than (unix_timestamp('2015-04-01 00:00:00'))  engine = innodb,

partition  p20150501values less than (unix_timestamp('2015-05-01 00:00:00'))  engine = innodb,

partition  p20150601values less than (unix_timestamp('2015-06-01 00:00:00'))  engine = innodb,

partition  p20150701values less than (unix_timestamp('2015-07-01 00:00:00'))  engine = innodb

);2 檢驗分割槽

insert into pss_log (method_name,create_time,ticket) values('clyde1',  '2014-06-01 00:00:00' ,'ticket1');

insert into pss_log (method_name,create_time,ticket) values( 'clyde2', '2014-06-02 00:00:00' ,'ticket1');

explain partitions  select * from pss_log where create_time<'2014-07-05 00:00:00' 

從執行計畫可以看出, 由於create_time<'2014-07-05 00:00:00' ,索引只會在2個分割槽p20140701,p20140801中查詢,但用的2個partition的全表掃瞄

explain  select * from pss_log where create_time<'2014-07-05 00:00:00' 

idselect_type

partitions

type

possible_keys

keyrows

extra

1******

p20140701,p20140801

allnull

null

1using where

where 語句中增加method_name條件,會用到了索引

explain   partitions  select * from pss_log where  method_name='clyde1' and  create_time<'2014-07-05 00:00:00' 

idselect_type

partitions

type

possible_keys

keyrows

extra

1******

p20140701,p20140801

range

idx_pss_log_create_time

idx_pss_log_create_time

1using where

如果時間範圍更小的話,就會只在乙個分割槽中找

explain   partitions  select * from pss_log where  method_name='clyde1' and  create_time<'2014-07-05 00:00:00'  and create_time>'2014-07-02 00:00:00';

idselect_type

partitions

type

possible_keys

keyrows

extra

1******

p20140801

range

idx_pss_log_create_time

idx_pss_log_create_time

1using where

3 另外條件允許的話,partitions分得越小越好, 

scrapy按照時間輸出日誌

1 在settings檔案中設定import datetime to day datetime.datetime.now log file path log資料夾的絕對路徑 log log format to day.year,to day.month,to day.day log level de...

建立分割槽表(按照年份分割槽,自動新增分割槽)

建立分割槽表aaa,通過字段建立時間的年份來分割槽,分割槽表自動根據插入的資料新增對應的分割槽,不過此處自動建立的分割槽名稱為系統建立的,如 sys 24。create table aaa id number 8 createtime date,value number 8 partition by...

Mysql按照時間分組統計

select update time,count id as count from eemp track group by substring update time,1,10 select date format create time,y m d as days count as count f...