PHP、MySQL 分库分表中间件需要依赖 PDO,PHP 分库分表支持协程(PHP, MySQL sharding library and sharding table middleware, support coroutines)。
PHP、MySQL 分库分表中间件需要依赖 PDO,PHP 分库分表支持协程(PHP, MySQL sharding library and sharding table middleware, support coroutines)。
PHP、MySQL分库分表中间件,需要依赖PDO,PHP分库分表,支持协程
You can install the package via composer:
composer require lys/php-sharding-pdo规则匹配到多库多表分页会比较慢,优化的方案是使用一个where条件来过滤掉非必要的查询结果集,增加查询分页效率比如 where id >= 1000000 或者 where id 规则匹配到单库单表分页与原来的速度一样
\Swoole\Runtime::enableCoroutine(); PHP Fatal error: Uncaught Swoole\Error: Socket#30 has already been bound to another coroutine#2,
reading of the same socket in coroutine#4 at the same time is not allowed .env文件
```php
[database]
host=localhost
username=root
password=testpassword
[shardingPdo]
#开启记录sql日志会影响性能
sqlLogOpen=false
sqlLogPath=sql.sql非协程
php vendor/bin/phpunit tests/IntegrationTest.php --filter testExecStart协程
php vendor/bin/phpunit tests/IntegrationCoroutineTest.php --filter testExecStarthttps://blog.csdn.net/weixin_38642740/article/details/81448762
详细请看tests目录
self::initDataResurce1(),
'db1' => self::initDataResurce2(),
'db2' => self::initDataResurce3(),
'db3' => self::initDataResurce4(),
];
}
protected function getShardingRuleConfiguration()
{
//article
$tableRule = new ShardingTableRuleConfig();
$tableRule->setLogicTable('article');
$tableRule->setDatabaseShardingStrategyConfig(
new InlineShardingStrategyConfiguration('db', [
'operator' => '%',
'data' => [ //具体的字段和相对运算符右边的数
'user_id', //字段名
4
]]));
$tableRule->setTableShardingStrategyConfig(
new InlineShardingStrategyConfiguration('article_', [ //插件自带使用 % 进行分片的规则
'operator' => '%',
'data' => [ //具体的字段和相对运算符右边的数
'cate_id', //字段名
2
]]));
$shardingRuleConfig = new ShardingRuleConfiguration();
$shardingRuleConfig->add($tableRule); //表1规则
//account
$tableRule = new ShardingTableRuleConfig();
$tableRule->setLogicTable('account');
$tableRule->setDatabaseShardingStrategyConfig(
new InlineShardingStrategyConfiguration('db',
[ //插件自带使用 % 进行分片的规则 由于要使用匿名函数自定义分片规则,所以这边是设置一个空数组
], function ($condition) { //匿名函数自定义分片规则
if (isset($condition['username']) && !is_array($condition['username'])) {
return crc32($condition['username']) % 4;
}
return null;
}));
$tableRule->setTableShardingStrategyConfig(
new InlineShardingStrategyConfiguration('account_', [], function ($condition) {
return 0;
}));
$shardingRuleConfig->add($tableRule); //表2规则
//user
$tableRule = new ShardingTableRuleConfig();
$tableRule->setLogicTable('user');
$tableRule->setDatabaseShardingStrategyConfig(
new InlineShardingStrategyConfiguration('db', [], function ($condition) {
if (isset($condition['id']) && !is_array($condition['id'])) {
return $condition['id'] % 4;
}
return null;
}));
$tableRule->setTableShardingStrategyConfig(
new InlineShardingStrategyConfiguration('user_', [], function ($condition) {
return 0;
}));
$shardingRuleConfig->add($tableRule); //表3规则
//auto_distributed
$tableRule = new ShardingTableRuleConfig();
$tableRule->setLogicTable('auto_distributed');
$tableRule->setDatabaseShardingStrategyConfig(
new InlineShardingStrategyConfiguration('db', [], function ($condition) {
if (isset($condition['stub']) && !is_array($condition['stub'])) {
return $condition['stub'] % 4;
}
return null;
}));
$tableRule->setTableShardingStrategyConfig(
new InlineShardingStrategyConfiguration('auto_distributed', [], function ($condition) {
return '';
}));
$shardingRuleConfig->add($tableRule); //表4规则
//category
$tableRule = new ShardingTableRuleConfig();
$tableRule->setLogicTable('category');
$tableRule->setDatabaseShardingStrategyConfig(
new InlineShardingStrategyConfiguration('db', [], function ($condition) {
return 0;
}));
$tableRule->setTableShardingStrategyConfig(
new InlineShardingStrategyConfiguration('category', [], function ($condition) {
return '';
}));
$shardingRuleConfig->add($tableRule); //表5规则
return $shardingRuleConfig;
}
protected static function initDataResurce1()
{
$dbms = 'mysql';
$dbName = DatabaseCreate::$databaseNameMap[0];
$servername = ConfigEnv::get('database.host', "localhost");
$username = ConfigEnv::get('database.username', "root");
$password = ConfigEnv::get('database.password', "");
$dsn = "$dbms:host=$servername;dbname=$dbName;port=3306;charset=utf8mb4";
try {
return self::connect($dsn, $username, $password);
} catch (\PDOException $e) {
if (ShardingPdoContext::getCid() > -1) {
\Swoole\Event::exit();
}else{
die();
}
}
}
protected static function initDataResurce2()
{
$dbms = 'mysql';
$dbName = DatabaseCreate::$databaseNameMap[1];
$servername = ConfigEnv::get('database.host', "localhost");
$username = ConfigEnv::get('database.username', "root");
$password = ConfigEnv::get('database.password', "");
$dsn = "$dbms:host=$servername;dbname=$dbName;port=3306;charset=utf8mb4";
try {
return self::connect($dsn, $username, $password);
} catch (\PDOException $e) {
if (ShardingPdoContext::getCid() > -1) {
\Swoole\Event::exit();
}else{
die();
}
}
}
protected static function initDataResurce3()
{
$dbms = 'mysql';
$dbName = DatabaseCreate::$databaseNameMap[2];
$servername = ConfigEnv::get('database.host', "localhost");
$username = ConfigEnv::get('database.username', "root");
$password = ConfigEnv::get('database.password', "");
$dsn = "$dbms:host=$servername;dbname=$dbName;port=3306;charset=utf8mb4";
try {
return self::connect($dsn, $username, $password);
} catch (\PDOException $e) {
if (ShardingPdoContext::getCid() > -1) {
\Swoole\Event::exit();
}else{
die();
}
}
}
protected static function initDataResurce4()
{
$dbms = 'mysql';
$dbName = DatabaseCreate::$databaseNameMap[3];
$servername = ConfigEnv::get('database.host', "localhost");
$username = ConfigEnv::get('database.username', "root");
$password = ConfigEnv::get('database.password', "");
$dsn = "$dbms:host=$servername;dbname=$dbName;port=3306;charset=utf8mb4";
try {
return self::connect($dsn, $username, $password);
} catch (\PDOException $e) {
if (ShardingPdoContext::getCid() > -1) {
\Swoole\Event::exit();
}else{
die();
}
}
}
protected static function connect($dsn, $user, $pass, $option = [])
{
//$dbh = new PhpShardingPdo\Core\SPDO($dsn, $user, $pass); //初始化一个PDO对象
//默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样:
//$dbh = new \PhpShardingPdo\Core\SPDO($dsn, $user, $pass, array(\PDO :: ATTR_TIMEOUT => 30,\PDO::ATTR_PERSISTENT => true));
$dbh = new \PhpShardingPdo\Core\SPDO($dsn, $user, $pass, $option);
$dbh->query('set names utf8mb4;');
return $dbh;
}
/**
* 获取事务sql执行日志路径,当事务提交失败的时候会出现该日志
* @return string
*/
protected function getExecTransactionSqlLogFilePath()
{
return './execTransactionSqlLogFilePath.log';
}
}
'0,1', //分表索引 index ,号分割
//'range' => [1,2] //范围
];
protected $shardingInitConfigClass = ShardingInitConfig4::class;
}
'0', //分表索引 index ,号分割
//'range' => [1,2] //范围
];
}where(['user_id' => 2, 'cate_id' => 1])->find();
var_dump($res);
$res = $model->renew()->where(['user_id' => 2, 'cate_id' => 1])->find();
var_dump($res);
$res = $model->renew()->where(['id' => 3])->findAll();
var_dump($res);
//order by
$res = $model->renew()->order('user_id desc')->limit(100)->findAll();
var_dump($res);
var_dump($model->find());
//group by
$res = $model->renew()->field('sum(id) as total,create_time,user_id')->group('user_id')->limit(100)->findAll();
var_dump($res);
$newObj = clone $model->renew();
var_dump($newObj === $model); //输出false
//count 查询
$count = $model->renew()->count();
var_dump($count);
$count = $model->renew()->where(['id' => ['gt', 100000]])->count('id'); //索引覆盖型查询
var_dump($count);
//in 查询
$list = $model->renew()->where(['id' => ['in', [1,2,3]]])->findAll();
var_dump($list);
//not in 查询
$list = $model->renew()->where(['id' => ['notIn', [1,2,3]]])->findAll();
var_dump($list);
//gt大于 egt大于等于 lt小于 elt小于等于
$list = $model->renew()->where(['id' => ['gt', 1]])->findAll();
var_dump($list);
//between 两者之间 相当于 id >= 100 and id renew()->where(['id' => ['between', [100, 10000]]])->findAll();
var_dump($list);
//同一个字段多条件查询 相当于 cate_id >= 1 and cate_id renew()->where([
'cate_id' => ['egt', 1]
])->where(['article_title' => '文章1'])
->where(['cate_id' => ['elt', 4]])
->count();
$this->assertEquals($count == 4, true);
//not between 不在两者之间 相当于 id 10000
$list = $model->renew()->where(['id' => ['notBetween', [100, 10000]]])->findAll();
var_dump($list);
//neq 不等于 可以是数组,也可以单个
$list = $model->renew()->where(['id' => ['neq', [1,2,3]]])->findAll();
var_dump($list);
$list = $model->renew()->where(['id' => ['neq', 1]])->findAll();
var_dump($list);
//like 查询
$list = $model->renew()->where(['article_title' => ['like','某网络科技%'],'type' => 1])->findAll();
var_dump($list);
//not like 查询
$list = $model->renew()->where(['article_title' => ['notLike','某网络科技%'],'type' => 1])->findAll();
var_dump($list);
//findInSet 查询
$count = $model->renew()->where([
'cate_id' => ['findInSet', 1]
])->where(['article_title' => '文章1'])
->count();
$this->assertEquals($count == 2, true);startTrans();
$model->startTrans(); //事务嵌套
$res = $user->renew()->insert(['id' => 2, 'create_time' => date('Y-m-d H:i:s')]);
$this->assertEquals(!empty($res), true);
$res =暂无开放 Issues,或尚未同步最近议题。