mysql表根據時間戳字段建立表分割槽的儲存過程

2021-07-09 07:32:11 字數 2265 閱讀 2896

這篇文章與mysql儲存過程按月建立表分割槽 方式一 寫的是相同的內容,只是在表分割槽建立方式上不一樣。 標題已經寫的很清楚了,直接貼sql語句啦。

-- 普通新增第乙個表分割槽語句

alter

table iot_divice_info partition by range(unix_timestamp(createtime))(partition par0 values less than (unix_timestamp('2016-02-01 00:00:00')));

-- 普通新增表分割槽語句

alter

table iot_divice_info add partition (partition par1 values less than (unix_timestamp('2016-03-01 00:00:00')));

--通過時間戳型別的字段對錶分割槽儲存過程

create

procedure pro_sys_logbymonthsjc(in tablename varchar(20),in timecolname varchar(20))

comment '每月按時新增表分割槽的儲存過程,由定時任務呼叫'

begin

declare p_id int;

declare nextdate date;

declare lastdate long;

--獲取表中的現有的分割槽數量數量

select

count(partition_name) into p_id from information_schema.partitions

where table_schema = schema() and table_name=tablename;

if p_id=0 then

--獲取下個月第一天的時間值,根據此值設定時間分割槽

select date_add(curdate()-day(curdate())+1,interval

1month) into nextdate from dual;

set @v_add=concat('alter table ',tablename,' partition by range(unix_timestamp(',timecolname,'))

(partition ',concat('par',p_id),' values less than (unix_timestamp(\'',nextdate,'\')))');

else

--獲取表中現有的最大的分割槽日期

select

max(partition_description) des into lastdate from information_schema.partitions

where table_schema = schema() and table_name=tablename;

--獲取下個月第一天的時間值,根據此值設定時間分割槽

select date_add(from_unixtime(lastdate),interval

1month) into nextdate from dual;

set @v_add=concat('alter table ',tablename,' add partition (partition ',concat('par',p_id),

' values less than (unix_timestamp(\'',nextdate,'\')))');

endif;

prepare stmt from @v_add;

execute stmt;

deallocate prepare stmt;

end;

-- 測試儲存過程

call pro_sys_logbymonthsjc('iot_divice_info','createtime');

-- 刪除表分割槽

alter

table iot_divice_info remove partitioning;

--每月建立乙個分割槽的定時任務

create event event_syslog on schedule every 1

month starts current_timestamp

on completion preserve

enable

docall pro_sys_logbymonthsjc('sys_log_storage','createtime');

乙個剛公升級為奶爸碼農的個人整理。

mysql根據時間戳查詢資料

比如我們要查詢每天的註冊使用者數量,這裡我們的註冊時間是時間戳的話。我們寫的sql語句就得把時間轉換為日期進行查詢。sql語句如下 函式 from unixtime select count user regnumber,from unixtime reg time y m d as group d...

mysql欄位裡面時間戳和時間的轉化

mysql當中因為儲存原因經常會將時間採用時間戳的方式進行儲存,但是這種方式不適合我們直接閱讀字段,往往需要轉化為時間,便於我們直接閱讀檢視。1 將時間戳轉化為日期時間,使用from unixtime 函式 2 相應時間轉化為時間戳為 當前時間戳 select unix timestamp 時間轉化...

VB更新時間戳字段

option explicit private sub command1 click dim objcnn as adodb.connection dim strtmp as string dim objrst as new adodb.recordset dim strsql as string ...