当前位置:网站首页>laravel 8 实现 订单表按月份水平分表
laravel 8 实现 订单表按月份水平分表
2022-06-29 08:17:00 【amateur12】
实现思路:
1.设计基础表orders
2.通过后台代码创建今年6月份订单表:order_202206,今年7月份订单表:order_202207...
创建表的时候需要进行判断,如果表存在,则不需要创建
这个后台代码会被多次使用并可以重复使用,选择写成trait
3.实现分表后的增删改查操作和分表前一样,但默认是对当前月份进行增删改查
trait:
<?php
namespace App\Traits;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
trait OrderTraits
{
//是否分表,默认false,即不分表
protected $isSplitTable = true;
//原表
public $originTable;
//表
public $endTable;
/**
* 后缀参数
* @var string
*/
protected $suffix = null;
/**
* 年月参数:202104
* @var string
*/
public $ym;
public function init(array $attributes = [], $suffix = null)
{
//默认原表
$this->originTable = $this->table;
//默认最终表
$this->endTable = $this->table;
$this->ym = Carbon::now()->format('Ym');
//isSplitTable参数为true时进行分表,否则不分表
if ($this->isSplitTable) {
//初始化后缀,未传则默认年月分表
$this->suffix = $suffix ?: $this->ym;
}
//初始化分表表名并创建
$this->setSuffix();
}
/**
* 设置表后缀, 如果设置分表后缀,可在service层调用生成自定义后缀表名,
* 但每次操作表之前都需要调用该方法以保证数据表的准确性
* @param $suffix
*/
public function setSuffix($suffix = null)
{
//isSplitTable参数为true时进行分表,否则不分表
if ($this->isSplitTable) {
//初始化后缀,未传则默认年月分表
$this->suffix = $suffix ?: $this->ym;
}
if ($this->suffix !== null) {
//$this->endTable = $this->getTable() . '_' . $suffix;
$this->endTable = $this->originTable . '_' . $this->suffix;
//最终表替换模型中声明的表作为分表使用的表
$this->table = $this->endTable;
}
//调用时,创建分表,格式为 table_{$suffix}
//未传自定义后缀情况下,,默认按年月分表格式为:orders_202205
//无论使用时是否自定义分表名,都会创建默认的分表,除非关闭该调用
$this->createTable();
}
/**
* 提供一个静态方法设置表后缀
* @param string $suffix
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function suffix($suffix = null)
{
$instance = new static;
$instance->setSuffix($suffix);
return $instance->newQuery();
}
/**
* 创建新的"table_{$suffix}"的模型实例并返回
* @param array $attributes
* @param bool $exists
* @return object $model
*/
public function newInstance($attributes = [], $exists = false)
{
$model = parent::newInstance($attributes, $exists);
$model->setSuffix($this->suffix);
return $model;
}
/**
* 创建分表,没有则创建,有则不处理
*/
protected function createTable()
{
info("createTable===============", [Schema::hasTable($this->endTable)]);
//初始化分表,,按年月分表格式为:orders_202205
if (!Schema::hasTable($this->endTable)) {
info("创建表==========", [$this->endTable]);
DB::update("create table {$this->endTable} like {$this->originTable}");
}
}
}
在order模型使用trait:(任何模型都可使用这个trait进行按月分表)
use OrderTraits;
protected $table = "orders";
protected $primaryKey = 'id';
protected $guarded = [];
protected $columns;
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
// 初始化分表处理
$this->init();
}效果:

按月分表适用场景:
按月分表作为分库分表的策略之一,可以做到冷热数据。
比如,本月的数据一般就是热数据,而之前月份的数据就是冷数据。
但是,应注意一件事情:如果本月写入数据很多,而上个月或者下个月数据写入很少,并且整个业务里根本不需要做一些月份数据的统计,就背离了因数据量多而进行分表的出发点,所以,分库分表应结合场景选用较为合适的策略,相比之下,取模适用场景比较广,可以较为均匀分担表数据。
边栏推荐
- 操作系统产品密钥查看方法
- 打印服务IP设置方案
- dcase_ Util tutorial
- Does the SQL server run with administrator privileges? Or run it as a normal user?
- Feature selection: maximum information coefficient (MIC) [used to measure the degree of correlation between two variables X and y, linear or nonlinear strength, commonly used for feature selection of
- Transformer details
- PaddleNLP通用信息抽取模型:UIE【信息抽取{实体关系抽取、中文分词、精准实体标。情感分析等}、文本纠错、问答系统、闲聊机器人、定制训练】
- VMware vcenter/esxi series vulnerability summary
- Swift中@dynamicMemberLookup和callAsFunction特性实现对象透明代理功能
- 二手交易平台碳减排,有了评估标准
猜你喜欢
随机推荐
Oracle-子查询
Dialogue | prospects and challenges of privacy computing in the digital age
Feature selection: maximum information coefficient (MIC) [used to measure the degree of correlation between two variables X and y, linear or nonlinear strength, commonly used for feature selection of
名企实习一年要学会的15件事,这样你就省的走弯路了。
Wallpaper applet source code double ended wechat Tiktok applet
NLP标注工具:Label Studio实现多用户协作打标
华为设备配置小型网络WLAN基本业务
《乔布斯传》英文原著重点词汇笔记(七)【 chapter five】
闭关修炼(二十五)基础web安全
通过ELO机制衡量各类对弈活动水平
关于父母离婚后子女姓名变更有关问题的批复
Differences between x86 and x64
开户买基金,通过网上基金开户安全吗?-
The @dynamicmemberlookup and callasfunction features in swift implement the object transparent proxy function
批量处理实验接触角数据-MATLAB分析
Np5 formatted output (III)
Application of mediastreamer2 and GStreamer in embedded field
2022第六季完美童模 清远赛区 海选赛圆满落幕
【Redis】Redis6学习框架思路和细节
Debugging nocturnal simulator with ADB command






