在文檔中主要介紹了kingshard的Hash和Range方式的分表,最近又開發了按時間維度的分表方式。按時間維度分表的場景很是廣泛,下面介紹一下kingshard的時間分表功能node
kingshard中的分表字段支持MySQL中三種類型的時間格式mysql
kingshard支持MySQL中三種格式的時間類型git
kingshard的配置文件以下所示:github
# server listen addr addr : 0.0.0.0:9696 # server user and password user : kingshard password : kingshard # if set log_path, the sql log will write into log_path/sql.log,the system log # will write into log_path/sys.log #log_path : /Users/flike/log # log level[debug|info|warn|error],default error log_level : debug # if set log_sql(on|off) off,the sql log will not output log_sql: on # only log the query that take more than slow_log_time ms #slow_log_time : 100 # the path of blacklist sql file # all these sqls in the file will been forbidden by kingshard #blacklist_sql_file: /Users/flike/blacklist # only allow this ip list ip to connect kingshard #allow_ips: 127.0.0.1 # the charset of kingshard, if you don't set this item # the default charset of kingshard is utf8. #proxy_charset: gbk # node is an agenda for real remote mysql server. nodes : - name : node1 # default max conns for mysql server max_conns_limit : 32 # all mysql in a node must have the same user and password user : kingshard password : kingshard # master represents a real mysql master server master : 127.0.0.1:3306 # slave represents a real mysql salve server,and the number after '@' is # read load weight of this slave. #slave : 192.168.59.101:3307@2,192.168.59.101:3307@3 down_after_noalive : 32 - name : node2 # default max conns for mysql server max_conns_limit : 32 # all mysql in a node must have the same user and password user : kingshard password : kingshard # master represents a real mysql master server master : 192.168.59.103:3307 # slave represents a real mysql salve server slave : # down mysql after N seconds noalive # 0 will no down down_after_noalive: 32 # schema defines sharding rules, the db is the sharding table database. schema : db : kingshard nodes: [node1,node2] default: node1 shard: - table: test_shard_year key: ctime type: date_day nodes: [node1,node2] date_range: [2015-2016,2017-2018]
按年分表的配置項設置以下:sql
table: test_shard_year key: ctime type: date_year nodes: [node1,node2] date_range: [2015-2016,2017-2018]
該配置表示:後端
date_year
。test_shard_year_2015, test_shard_year_2016
兩個子表落在node1上,test_shard_year_2017,test_shard_year_2018
兩個子表落在node2上。date_range[2015,2017-2018]
。注意:子表的命名格式必須是:shard_table_YYYY,shard_table是分表名,後面接具體的年。架構
在node1上建立兩張子表test_shard_year_2015, test_shard_year_2016
,在node2上建立兩種子表test_shard_year_2017,test_shard_year_2018
。建表SQL以下this
CREATE TABLE `test_shard_year_2016` ( `id` int(10) NOT NULL, `name` varchar(40) DEFAULT NULL, `ctime` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入數據:spa
mysql> insert into test_shard_year(id,name,ctime) values(12,"hello","2015-02-22 13:23:45"); Query OK, 1 row affected (0.01 sec) mysql> insert into test_shard_year(id,name,ctime) values(13,"world","2016-03-22"); Query OK, 1 row affected (0.00 sec) mysql> select * from test_shard_year where ctime < "2016-03-23"; +----+-------+---------------------+ | id | name | ctime | +----+-------+---------------------+ | 12 | hello | 2015-02-22 13:23:45 | | 13 | world | 2016-03-22 00:00:00 | +----+-------+---------------------+ 2 rows in set (0.00 sec)
對應的SQL log信息是:架構設計
2016/03/05 12:06:32 - OK - 1.2ms - 127.0.0.1:56597->127.0.0.1:3306:insert into test_shard_year_2015(id, name, ctime) values (12, 'hello', '2015-02-22 13:23:45') 2016/03/05 12:06:59 - OK - 2.0ms - 127.0.0.1:56597->127.0.0.1:3306:insert into test_shard_year_2016(id, name, ctime) values (13, 'world', '2016-03-22') 2016/03/05 12:08:30 - OK - 1.6ms - 127.0.0.1:56597->127.0.0.1:3306:select * from test_shard_year_2015 where ctime < '2016-03-23' 2016/03/05 12:08:30 - OK - 0.3ms - 127.0.0.1:56597->127.0.0.1:3306:select * from test_shard_year_2016 where ctime < '2016-03-23'
固然若是你把id做爲一個unix時間戳,來分表的話,kingshard也是支持的。具體配置就是這樣的:
table: test_shard_year key: id type: date_year nodes: [node1,node2] date_range: [2015-2016,2017-2018]
插入數據:
mysql> insert into test_shard_year(id,name,ctime) values(1457410310,"world","2018-03-22"); Query OK, 1 row affected (0.01 sec) mysql> select * from test_shard_year where id = 1457410310; +------------+-------+---------------------+ | id | name | ctime | +------------+-------+---------------------+ | 1457410310 | world | 2018-03-22 00:00:00 | +------------+-------+---------------------+ 1 row in set (0.00 sec)
1457410310 這個unix時間戳對應的日期是:2016-3-8 12:11:50。kingshard準確地將這條記錄路由到了test_shard_year_2016
這張子表中了。
對應的SQL log是:
2016/03/08 12:12:49 - OK - 1.0ms - 127.0.0.1:56669->127.0.0.1:3306:insert into test_shard_year_2016(id, name, ctime) values (1457410310, 'world', '2018-03-22') 2016/03/08 12:13:23 - OK - 0.4ms - 127.0.0.1:56669->127.0.0.1:3306:select * from test_shard_year_2016 where id = 1457410310
按月分表的配置項設置以下:
table: test_shard_month key: ctime type: date_month nodes: [node1,node2] date_range: [201512-201602,201609-2016010]
該配置表示:
date_month
。test_shard_month_201512, test_shard_month_201601, test_shard_month_201602
兩個子表落在node1上,test_shard_month_201609,test_shard_month_201610
兩個子表落在node2上。date_range[201501,201609-201610]
。注意:子表的命名格式必須是:shard_table_YYYYMM,shard_table
是分表名,後面接具體的年和月。
功能演示參考按年分表的操做。
按月分表的配置項設置以下:
table: test_shard_day key: ctime type: date_day nodes: [node1,node2] date_range: [20151222-20151224,20160901-20160902]
該配置表示:
date_day
。test_shard_day_20151222, test_shard_day_20151223, test_shard_day_20151224
兩個子表落在node1上,test_shard_day_20160901,test_shard_day_20160902
兩個子表落在node2上。date_range[20150101,20160901-20161010]
。注意:子表的命名格式必須是:shard_table_YYYYMMDD,shard_table
是分表名,後面接具體的年,月和日。
功能演示參考按年分表的操做。
kingshard開源地址:https://github.com/flike/king...
歡迎關注後端技術快訊公衆號,有關kingshard的最新消息與後端架構設計類的文章,都會在這個公衆號分享。