What is MySQL left link query how to use?

Source: Internet
Author: User
Tags datetime join key sql mysql query sort first row

MySQL left connection query is a way to connect the query, the following is to introduce you to the MySQL left connection query some of the questions to talk about, if you are interested, may wish to see.

The main Table I'm talking about here refers to which table MySQL is querying in the connection query. For example, in the MySQL left-link query, generally speaking, the left table is the main table, but this is just experience, often empirical is unreliable, in order to illustrate the problem, first of all, build two demo table categories and posts:

 
 
  1. CREATE TABLE IF not EXISTS ' categories ' (
  2. ' ID ' int (a) unsigned not NULL auto_increment,
  3. ' Name ' varchar not NULL,
  4. ' Created ' datetime not NULL,
  5. PRIMARY KEY (' id '),
  6. KEY ' name ' (' name ')
  7. );
  8. CREATE TABLE IF not EXISTS ' posts ' (
  9. ' ID ' int (a) unsigned not NULL auto_increment,
  10. ' category_id ' int (a) unsigned not NULL,
  11. ' title ' varchar Not NULL,
  12. ' content ' varchar not NULL,
  13. ' Created ' datetime not NULL,
  14. PRIMARY KEY (' id '),
  15. KEY ' category_id ' (' category_id '),
  16. KEY ' created ' (' created '),
  17. KEY ' category_id_created ' (' category_id ', ' created ')
  18. );

First of all, notice the index of each table, later will use, remember to insert a bit of test data, not too much, but how to get more than two lines, and then perform the following

 
  
  
  1. Sql:
  2. EXPLAIN SELECT *
  3. From posts
  4. Left JOIN categories on posts.category_id = Categories.id
  5. WHERE categories.name like ' foobar% '
  6. ORDER BY posts.created DESC

The results are as follows:

 
  
  
  1. Table Key Extra
  2. Categories name Using where; Using temporary; Using Filesort
  3. Posts category_id

In the result of the explain of the join query, the first row represents the table that is the primary table. So in this query categories is the primary table, and in our experience, the left table (posts table) should be the primary table in the left-hand join query, which creates a fundamental paradox, because in our where part, MySQL The query criteria are filtered according to the fields of the Categories table, and the Categories table has exactly the right index, so it is more advantageous to reduce the result set when querying the Categories table as the primary table.

The using temporary in the explain result; Using Filesort is why, why is created or category_id_created index invalid? This is because the primary table is the Categories table, from the table is the posts table, and we use the fields from the table to order by, This is usually not a good choice, and it's best to change to a main table field. But a lot of times can not change, then there is no recruit.

Let's look at a more bizarre example:

 
  
  
  1. EXPLAIN SELECT *
  2. From posts
  3. Left JOIN categories on posts.category_id = Categories.id
  4. WHERE categories.id = ' An already existing ID '
  5. ORDER BY posts.created DESC

In this case the posts table is still from the table, but the results sorted from the table do not appear in the file sort and temp table, because the categories.id is already defined, so the primary table is equivalent to a constant table with only one row of data, from the table based on the category_id_ The created index will naturally get sorted results when connected. But to put it another way, since categories.id are OK, that kind of demand, we generally will not use the left JOIN query, and will be divided into two separate queries to retrieve categories and posts.

Subjectively, once the master table is mistaken, it is possible to adjust the index without efficient SQL, so when writing SQL, for example, when writing a MySQL left-join query, if you want the left table to be the primary table, make sure that the query conditions in the WHERE statement use as much of the left table field as possible, and then, once the master table is determined, It's also best to go through the Main table field only.

Note: In most cases, using a sort from table field is inefficient, and my original example misled you and corrected it.




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.