-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_shard_table.sql
37 lines (32 loc) · 1.07 KB
/
create_shard_table.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
delimiter //
drop table if exists shard_seq_tbl //
create table shard_seq_tbl ( nextval bigint not null primary key auto_increment ) engine = MyISAM //
alter table shard_seq_tbl AUTO_INCREMENT = 10000 //
drop function if exists shard_nextval //
create function shard_nextval()
returns bigint
begin
insert into shard_seq_tbl values (NULL) ;
set @R_ObjectId_val=LAST_INSERT_ID() ;
delete from shard_seq_tbl ;
return @R_ObjectId_val ;
end//
drop function if exists now_msec//
CREATE FUNCTION now_msec RETURNS STRING SONAME "now_msec.so"//
drop function if exists next_sharded_id //
CREATE function next_sharded_id ()
RETURNS bigint
BEGIN
DECLARE our_epoch bigint DEFAULT 1325419260000;
DECLARE seq_id bigint;
DECLARE now_millis bigint;
DECLARE shard_id int DEFAULT 0;
DECLARE result bigint UNSIGNED;
SELECT MOD(shard_nextval(),1024) INTO seq_id;
SELECT now_msec() INTO now_millis;
set result := (now_millis - our_epoch) << 23;
set result := result | (shard_id << 10);
set result := result | (seq_id);
RETURN result;
END//
delimiter ;