MySQL left join query Experience Summary

Source: Internet
Author: User

MySQL left join query is a method in connection query. The following describes some questions about MySQL left join query. If you are interested, take a look.

The primary table I mentioned here refers to the table in the connection query where MySQL mainly queries. For example, in MySQL left join queries, the left table is generally the primary table, but this is just an empirical statement. In many cases, experience is unreliable. To illustrate the problem, let's give an example first, create two tables for Demonstration: categories and posts:

 
  1. CREATE TABLE IF NOT EXISTS `categories` (  
  2. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  3. `name` varchar(15) NOT NULL,  
  4. `created` datetime NOT NULL,  
  5. PRIMARY KEY (`id`),  
  6. KEY `name` (`name`)  
  7. );  
  8.  
  9. CREATE TABLE IF NOT EXISTS `posts` (  
  10. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  11. `category_id` int(10) unsigned NOT NULL,  
  12. `title` varchar(100) NOT NULL,  
  13. `content` varchar(200) NOT NULL,  
  14. `created` datetime NOT NULL,  
  15. PRIMARY KEY (`id`),  
  16. KEY `category_id` (`category_id`),  
  17. KEY `created` (`created`),  
  18. KEY `category_id_created` (`category_id`, `created`)  
  19. );  
  20.  

Pay attention to the index situation of each table, which will be used later. Remember to insert a little test data without too much, but you have to have more than two rows, and then execute the following

 
  1. SQL:  
  2.  
  3. EXPLAIN SELECT *  
  4. FROM posts  
  5. LEFT JOIN categories ON posts.category_id = categories.id  
  6. WHERE categories.name LIKE 'foobar%'  
  7. ORDER BY posts.created DESC  
  8.  

The result is as follows:

 
  1. table      key         Extra  
  2. categories name        Using where; Using temporary; Using filesort  
  3. posts      category_id  
  4.  

In the explain results of the join query, the table in the first row is the primary table. Therefore, in this query, categories is the primary table. In our experience, in left join queries, the LEFT table posts should be the primary table, which produces a fundamental contradiction, the reason why MySQL does this is that in our WHERE section, the query conditions are filtered Based on the fields in the categories table, and the categories table has a suitable index, therefore, using the categories table as the primary table during query is more conducive to reducing the result set.

Why is the Using temporary; Using filesort in the explain result invalid? This is because the main table is a categories table, the slave table is a posts table, and we use the field from the table to go to order by. This is usually not a good choice. It is best to change it to the main table field. However, if it cannot be changed in many cases, it will be useless.

Let's look at a strange example:

 
  1. Explain select *
  2. FROM posts
  3. Left join categories ON posts. category_id = categories. id
  4. WHERE categories. id = 'an existing id'
  5. Order by posts. created DESC
  6.  

In this example, the posts table is still a slave table, but no File Sorting or temporary table is displayed in the result of sorting by slave table, because categories has been determined. id. Therefore, the primary table is equivalent to a constant table with only one row of data. The sorted results are obtained from the table when the table is connected according to the category_id_created index. But from another perspective, since categories. IDs are all determined, so we generally do not use left join queries, but will be divided into two independent queries to retrieve categories and posts.

Subjective, once the primary table is wrong, how to adjust the index will not be able to get efficient SQL statements, so when writing SQL statements, such as when writing MySQL left join queries, if you want the left table to be a master table, make sure that the query conditions in the WHERE statement use as many left table fields as possible. Once the master table is determined, it is also best to use the primary table field to go to order.

Note: In most cases, sorting from table fields is inefficient. My first example misled everyone and has corrected it.


 

How to modify the table structure in mysql

MySQL memory table heap usage Summary

MySQL connection Query

How to Implement MySQL multi-Table insertion

Two tips for MySQL table repair

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.