Hi,
I want to explain my new idea about ActiveRecord, It maybe like samdark’s idea apart. My idea is about an active record’s design with joiningless. Because some reasons:
-
Problem with big data
-
Cross storage relations
-
MySQL Replication & Sharding
[size="4"]Problem with big data[/size]
First, as of my experience, with big data, the SQL join is a problem.
For example, if I have 2 tables: person and team with a relation : person.team_id = team.id
Normally, whenever we need to get some people including their team, we use :
SELECT * FROM person p INNER JOIN team t ON p.team_id = t.id WHERE {some conditions on person}
That’s fine, but if the person table has about 2 milions of records, and about 500,000 records in team, this will be a problem for MySQL. The joining progress will kill the server.
My solution is use 2 SQL queries, the first get people, the second get team and then join them by PHP code.
SELECT * FROM person WHERE {conditions}
PHP: get all team id from the results
SELECT * FROM team WHERE id IN (...)
[size="4"]Cross storage relations[/size]
At my project, I use Service Oriented design. So, our product is splited into some services, each services has its own data (model) and business. But, in general business some services need to access other services’ models, I use RESTful to exchange the data. That means I can do find, findAll, save (insert, update) active records through RESTful.
Sometimes I need to make a relation between MySQL ActiveRecord and RESTModel, I used component’s getter to solve it, but it will be better if I use relations.
And some other storages which we can apply find, findAll, save actions on them : file, memcache, sqlite … (samdark has told about that).
[size="4"]MySQL Replication & Sharding[/size]
The biggest disadvantage point of MySQL is no multi-master at replication. With a high scale product, we can use 1 master server only, and many slave servers. With high rate of write query, the master server will die.
If you want to join some tables, they have to be at same server, same connection.
We can not use MySQL sharding with current related active record design.
[size="4"]Summary[/size]
The SQL with joining is very pretty at theory, so we can use it in relations declaration.
But when we implement it, should we use a joiningless finding, and the above 4 problems can be solved.
What do you think?