ifwelove / blog Goto Github PK
View Code? Open in Web Editor NEWblog
blog
CREATE TABLE `bigtable_p` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
PRIMARY KEY (`id`,`created_at`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
PARTITION BY RANGE (unix_timestamp(`created_at`)) (
PARTITION p201801 VALUES LESS THAN (UNIX_TIMESTAMP('2018-02-01 00:00:00')) ENGINE = InnoDB,
PARTITION p201802 VALUES LESS THAN (UNIX_TIMESTAMP('2018-03-01 00:00:00')) ENGINE = InnoDB,
.
.
.
PARTITION pmax VALUES LESS THAN MAXVALUE ENGINE = InnoDB
);
CREATE TABLE `bigtable_p` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`,`created_at`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
PARTITION BY RANGE (TO_DAYS(`created_at`)) (
PARTITION p201801 VALUES LESS THAN (TO_DAYS('2018-02-01 00:00:00')) ENGINE = InnoDB,
PARTITION p201802 VALUES LESS THAN (TO_DAYS('2018-03-01 00:00:00')) ENGINE = InnoDB,
.
.
.
PARTITION pmax VALUES LESS THAN MAXVALUE ENGINE = InnoDB
);
range
id
+ created_at
datatime
注意SQL查詢要用等式查詢, 或者開發須注意搜尋的分區, 可用explain
查詢, 時間欄位型態為timestamp
則不影響explain select * from `bigtable` where `created_at` <= '2020-01-31' and `created_at` > '2020-01-01' ;
select * from `bigtable` where `created_at` < '2020-02-01' and `created_at` > '2020-01-01';
換
select * from `bigtable` where `created_at` <= '2020-01-31' and `created_at` > '2020-01-01';
或
select * from `bigtable` where `created_at` BETWEEN '2020-01-01' AND '2020-02-01';
RENAME TABLE `bigtable` TO `bigtable_old`;
insert into `bigtable_old` select * from `bigtable_p`;
RENAME TABLE `bigtable_p` TO `bigtable`;
SELECT * FROM information_schema.partitions WHERE `TABLE_SCHEMA` = 'db' AND `TABLE_NAME` = 'bigtable' AND `PARTITION_NAME` IS NOT NULL;
use DB;
DB::connection('master')
->table('information_schema.partitions')
->where('TABLE_SCHEMA', 'db')
->where('TABLE_NAME', 'bigtable')
->whereNotNull('PARTITION_NAME')
->get();
use DB;
DB::connection('master')->select('call alter_partition()');
對應的SQL
ALTER TABLE `bigtable` REORGANIZE PARTITION pMax INTO (
PARTITION p201803 VALUES LESS THAN (UNIX_TIMESTAMP('2018-04-01 00:00:00')),
PARTITION pMax VALUES LESS THAN (MAXVALUE)
);
use DB;
DB::connection('master')->select('call drop_partition()');
對應的SQL
ALTER TABLE bigtable DROP PARTITION p201801;
class service
{
public function save()
{
$repository->save();
$repository->saveToOld();
}
public function delete()
{
$repository->delete();
$repository->deleteToOld();
}
public function update()
{
$repository->update();
$repository->updateToOld();
}
}
RENAME TABLE `bigtable` TO `bigtable_error`;
RENAME TABLE `bigtable_old` TO `bigtable`;
https://xyz.cinc.biz/2017/07/mysql-copy-table.html
https://ithelp.ithome.com.tw/users/20124671/articles
https://ithelp.ithome.com.tw/articles/10231451
https://medium.com/17media-tech/mysql-partitioning-%E5%84%AA%E5%8C%96%E4%B9%8B%E8%B7%AF-fd8e8480789b
https://www.itread01.com/content/1522648617.html
https://blog.csdn.net/fdipzone/article/details/79769524
use DB;
DB::connection('master')->select('call example()');
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.