Thinkphp Association query N + 1. -Php Tutorial

Source: Internet
Author: User
Suppose we have two tables: The author table and the book table. One author has multiple books. They are a one-to-many relationship. Now we want to get a list of books, and the list should contain the author of the book. Generally, a newbie performs the following operations: {code...} the above case will execute a query to retrieve all databases...

Suppose we have two tables: The author table and the book table. One author has multiple books. They are a one-to-many relationship. Now we want to get a list of books, and the list should contain the author of the book. The procedure is as follows:

$bookList = $db->query('select * from books');foreach($bookList as &$v) {    $v['author'] = $db->query('select name from author where id =' . $v['author_id']);}

In the above case, a query is executed to retrieve the books on all database tables, but each book is executed to retrieve the author. If we have 25 books, we will perform 26 queries. This will bring a great deal of cost to the database. we should try to make a simple query.

The general solution isjoinOne SQL statement completes the above tasks.

I found that the ORM of this framework also made this low-level error when using the link query provided by thinkphp3.2.

class IndexController extends Controller {    public function index(){        D('book')->relation(true)->select();    }}

Open the debugger and find that each book uses an SQL statement to query the author.

Is the framework designed in this way, and can only be solved by hand join?

Reply content:

Suppose we have two tables: The author table and the book table. One author has multiple books. They are a one-to-many relationship. Now we want to get a list of books, and the list should contain the author of the book. The procedure is as follows:

$bookList = $db->query('select * from books');foreach($bookList as &$v) {    $v['author'] = $db->query('select name from author where id =' . $v['author_id']);}

In the above case, a query is executed to retrieve the books on all database tables, but each book is executed to retrieve the author. If we have 25 books, we will perform 26 queries. This will bring a great deal of cost to the database. we should try to make a simple query.

The general solution isjoinOne SQL statement completes the above tasks.

I found that the ORM of this framework also made this low-level error when using the link query provided by thinkphp3.2.

class IndexController extends Controller {    public function index(){        D('book')->relation(true)->select();    }}

Open the debugger and find that each book uses an SQL statement to query the author.

Is the framework designed in this way, and can only be solved by hand join?

In this case, it is best to use join to solve multiple-to-one queries. Furthermore, it is really not good to use tp-related queries...

The method provided by the framework for operations on a single database is relatively simple, but it is recommended to write it yourself if complicated SQL queries are involved.

Is there a PHP framework or DB class that supports better Association queries.

Isn't it possible to create a view ??

M('book')->join('user on book.auther_id = user.id')->select();

You can perform the combined query in this way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.