thinkphp5中数据库⽔平分表与增删改查
thinkphp是国内⾮常流⾏的⼀个PHP语⾔开发框架,但是在项⽬开发中随着数据量的不断增⼤,数据库已经成为影响平台发展的瓶颈问题之⼀,所以本⽂波波将简单分享thinkphp5下数据库的⽔平分表,以及分表后对数据的增删改查。以提升整体性能。
⼀、数据库分表:
1、我们⾸先创建数据表system_history。
2、⼿动添加数据表system_history_1,system_history_2,system_history_3,system_history_4字段与数据类型与system_history保持⼀致。
⼆、数据库增删改查:
1、插⼊数据。
2、查询数据。
3、修改数据。
4、删除数据。
删除数据与查询数据雷同,⽅法修改为delete()即可。CREATE TABLE `system_history` (  `id` int(10) NOT NULL AUTO_INCREMENT,  `member_id` int(10) NOT NULL DEFAULT '0',  `history` int(10) DEFAULT NULL,  `sex` smallint(1) DEFAULT NULL,  `age` smallint(3) DEFAULT NULL,  `type` smallint(1) DEFAULT NULL,  `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  `is_deleted` smallint(1) DEFAULT '0' COMMENT '1已删0正常',  PRIMARY KEY (`id`,`member_id`),  KEY `member_id` (`member_id`),  KEY `age` (`age`),  KEY `sex` (`sex`)) ENGINE=InnoDB DEFAULT CHARSET=utf8/*!50100 PARTITION BY LINEAR KEY (member_id)PARTITIONS 4 */
1
2
3
4
国内php空间5
6
7
8
9
10
11
1213
14
15
16$rule = array('type' => 'LINEAR KEY','num' =>4);$temparr = array('member_id'=>$uid);$time = time();Db::name("SystemHistory")->partition($temparr,'member_id',$rule)    ->insert(array('member_id'=>$uid,'history'=>$view_id,'type'=>$type,'datetime'=>date("Y-m-d  H:i:s",$ti
me)));
1
2
3
45$rule = array('type' => 'LINEAR KEY','num' =>4);$temparr = array('member_id'=>$uid);$time = time();$arr = Db::name("SystemHistory")->partition($temparr,"member_id",$rule)    ->where(array('member_id'=>$uid,'history'=>$view_id,'type'=>$type))    ->whereTime('datetime','today')->find();
1
2
3
4
5
6$rule = array('type' => 'LINEAR KEY','num' =>4);$temparr = array('member_id'=>$uid);$time = time();Db::name("SystemHistory")->partition($temparr,'member_id',$rule)        ->where(array('member_id'=>$uid,'history'=>$view_id,'type'=>$type))    ->whereTime('datetime','today')->update(array('datetime'=>date("Y-m-d  H:i:s",$time),'is_deleted'=>0));
1
2
3
4
5
6
关于数据库⽔平分表,其实不⽌这⼀种⽅法。但是不管⽤哪种⽅法,其⽤法都是⼤同⼩异,故本⽂内容仅⽤于参考。不作为实际项⽬开发中的应⽤。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。