Connection query in SQL and its optimization principle

Source: Internet
Author: User

Connection query is the main task of SQL, only a good grasp of the connection query and its optimization method is mastered the essence of SQL. Recently encountered in the interview about the connection query questions, feel that the answer is not very good, summed up.

For specific examples, refer to: http://www.w3school.com.cn/sql/sql_join.asp

Summarize:

Connection query principle and code optimization: If you want to make a connection query to Table1 and table2 two tables, the DBMS first finds the first tuple in Table1 and then scans the Table2 table from the beginning, looking for the Table1 tuple that corresponds to the table2 first tuple, Once found, the first tuple of the Table1 and the second tuple of the table2 are stitched together, and if the table2 is scanned again and not found, it begins to match the second tuple of Table1. It was not until the Table1 scan was completed. Returns the connection table to the query. Through the connection query principle can be clear, two tables of connection query is a two-layer loop, if the record number of Table1 is m,table2 the number of records is n then the time to make a connection query is the complexity of O (m*n). A visible connection query is time consuming, and if the table has a very large number of records, the query efficiency becomes very low. So in the writing of SQL statements, especially complex connection query is to optimize the query at the code level, the principle of optimization is to put the connection operation as far as possible at the end, as far as possible to the single table query, select operations in the front so that will be to a certain extent reduce the number of records connected query table, improve execution efficiency. SQL statements are also optimized by the DBMS itself.

Inner connection, left outer link, right outer link:

Inner Join=join can be understood as a natural connection, and is the most commonly used connection query that returns a matching row (at least one match will be returned).

An out join typically has a left-out join and a right-out join is a connection query that is relative to the inner join, where an out-of-row join refers to a table that is all returned if there are rows for the table to be returned, otherwise it is set to null. The right-out join is the opposite. It is easy to see the specific examples in the above connection.

Connection query in SQL and its optimization principle

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.