【急】如何用yii处理分库分表的业务需求

由于产品数据量较大,设计上采用水平的分库和分表的结构(多个库位于多个机器上分布)。

比如,一个库建100个表,可以根据产品数据量的增长灵活的加入新的数据库来解决。

如下图这样的,DB1和DB10是结构完全一样的数据库,各有100个表(t1到t100,表结构完全相同).

t1的结构中有一个pid的字段(理解为产品编号)。

我的划分依据是利用pid来分。

首先是确定如何分库:

比如,我一个表存100个产品的数据,那么一个库可以容纳10000个产品,分库的依据可以这样,pid小于10000的数据库节点为1,即DB1;10001到20000的,节点为2 即DB2 … (如这样做 return "DB".ceil($pid/10000);得到数据库)。

确定表名:

在得到数据库后,表名可以利用pid对100取模数的方式,如 return ‘t’.$pid%100;得到表名;

本着这样的想法,我打算是写一个继承CActiveRecord 类的MyActiveRecord类 ,在这个类中,需要更改几个方法,像getTableName,model,getDbConnection等。

貌似这样分了后,使用yiic产生crud的页面程序要改动不少地方,

不知道这样的分库分表的思路对不对,大家对这样的需求是怎么做的,最好能给点实例代码。

欢迎回复!

为什么没人过问啊。

我也遇到这样的问题了,现在考虑分库和分表。不知道怎样处理,如果用AR,则会在默认的DB中。

关于分库, 你可以指定使用哪一个 CDbConnection 来连接到你需要的数据库. 至于表的名字, 就由你制定表名的规则来连接.

我在做类似项目时遇到的是处理relations时表名的问题.

我曾考虑过你的这种水平分法, 不过数据的查询和关联的问题好像会比较棘手,不知你有什么方法来处理这个问题的.

在中国有一个Java的开源项目叫做Amoeba是专门用于此类业务处理的。

另外还有一个在手机之家的人做过这样的类似项目。

参考网站:

http://amoeba.meidusa.com/wordpress/

http://www.longker.org/

不过我还没有发现PHP做数据库访问层(DAL)的案例,我最近在看Yii框架,但没有看到有这样的DAL计划。

不知到强哥同志是否也有计划考虑?

如果没有DAL,PHP在处理大数据量业务时,未来迟早是个问题。很期待看到类似的解决方案出现,最好是纯PHP DAL或者Yii框架+集成Java版本的Amoeba for MySQL。这样以后就少很多事了。

建议强哥可以看看Amoeba,如果开发来不及,最好先可以集成Amoeba,这样我们会很高兴。因为我们的项目中以后可能需要用到这个东东。

强烈支持Yii,这是我看到的最好的PHP框架,希望继续完善,也希望中国的同行能够借助强哥华人的优势,在中文普及上多花点功夫。

在DAL提供支持应该不是Yii的努力方向。关于分库分表,这里有一篇讨论文章和原型实现:http://www.yiiframework.com/forum/index.php?/topic/3960-sharding/page__hl__sharding__fromsearch__1

Thanks a lot!

我把你推荐的文章放在这里,希望中国的朋友不费劲可以看到。DAL是一个大型数据量系统的重要的底层架构,希望对大家有帮助。

I did temporarily give up on anti-together behaviour to study AR better. And took another task: sharding support. Result is attached as ZIP (several classes extending AR). Description following.

Main thing about sharding support was to give AR ability to decide on what connection to use dynamically. Depending on some object property or any environment condition. So we can switch to db-replica or concrete DB-node (where required entity is located). Unfortunately Yii can’t read our minds so we need a tool to let it know which connection (which server) we are going to use. Following Yii AR interface look choose() method was implemented.

To end-user It looks like:

$users = User::model()->choose($shard)->findAll();

To make it work we need logic to decide which connection to use depending on concrete $shard var (what’s inside is different in different cases). This logic is situated in DbConnectionManager. Sharding support library (or patch) includes CDbConnectionManagerAbstract defining it’s interface. In most cases you just need to implement

abstract protected function _createDbConnection($key);

That’s it: this function will get $shard as $key parameter. And supposed to return CDbConnection. To make it work DbConnectionManager should be defined in config next to ‘db’:

‘dbConnectionManager’ => array(

'class' => 'application.tests.classes.ConcreteDbConnectionManager',

)

Usage of described extensions doesn’t break standard way Yii works either. We only use DbConnectionManager if choose() called. And it’s object-visible. Next model you create will start from default connection from config.

Besides the BC it makes possible creating "System tables" which is common requirement for any sharding implementation.

Everything I described is way more wider described in unit-tests attached to implementation. Tests include some test Models and CShardingTestCase is full of examples of what you get in your code to work with it.

One more thing about it: it’s impossible to make Sharding work out-of-box in most cases. And our implementation is way more complex then default one we can suppose. BUT! Nothing stops us from creating default bindings to implement most basic and completely automatic sharding in extension to our extension. And that’s my current vector.

Could you please check all this stuff and tell me what’s the best way to make this lib accessible to community and improve it further?

Thanks.

到现在yii还不支持分库分表?

Doctrine提供PHP的DAL(http://www.doctrine-project.org/projects/dbal)

public function tableName(){

}

这里不能动态返回表名吗

我目前在一个查询系统中已经实现了分表 分库目前还没做 不过分表和分库我感觉和 YII 本身没什么关系 主要的还是你如何做分表 分库的方式 然后如何通过 yii来实现你的想法! 思路为一 其次再就是是用YII实现了!!