sql query SELECT statement is not executed first

Many of the SQL query SELECT statements are based on the keywords begin, so we could easily think that SQL query SELECT statement is executed first. However, when I try to explain to people what is the window function of time, as to whether this issue can filter the results returned by the window function, and ultimately concluded that the implementation of the window function must be after the WHERE clause and a GROUP BY clause , it is not the result returned by the function to filter window again. So I thought of another question: What is the order of execution of SQL queries what?

This seems to be a very good answer, after all he has written thousands of SQL queries, and some of which are very complex. But the fact is I'm still very difficult to say exactly what its order.

The order of execution of SQL queries

So I researched it and found in order of execution of a SQL statement, SELECT is not executed first, but in the fifth.

The above image and semantics of SQL queries about, let you know what a query returns, and answer the following questions:

1. WHERE it can be used after GROUP BY? (No, WHERE is before the GROUP BY!)

2. You can filter the results window function returns do? (No, the window function in a SELECT statement, which is after the WHERE and SELECT GROUP BY)

3. You can be based on GROUP BY ORDER BY do the stuff? (Can, basically in the last ORDER BY executed, it can be based on anything ORDER BY)

4.LIMIT is executed at what time? (At the end!)

But the database engine is not necessarily in strict accordance with the implementation of this order SQL queries, because they are faster to execute a query, will perform optimization to make some order. In other words, when it comes to performance or query and index-related things, this chart does not apply.

Mixed factors: column aliases

There are many SQL implementations allow you to use this syntax:

SELECT CONCAT(first_name, ' ', last_name) AS full_name, count(*)
FROM table
GROUP BY full_name

From the point of view of this statement, as if GROUP BY is performed after the SELECT, because it refers to an alias in the SELECT. But in fact need not be this way, the database engine query can be rewritten like this:

SELECT CONCAT(first_name, ' ', last_name) AS full_name, count(*)
FROM table
GROUP BY CONCAT(first_name, ' ', last_name)

This is still the first implementation GROUP BY, another database engine will do a series of checks to ensure stuff SELECT and GROUP BY is valid, so the query will do a check before generating the overall implementation plan.

Database may not execute the query sequence (Optimization)

In practice, the database will not necessarily be executed in the order JOIN, WHERE, GROUP BY query, because they will conduct a series of optimization, to upset the order of execution, so that the query executes faster, they do not alter query results.

The following example illustrates why you need to perform a query in a different order:

SELECT *
FROM owners 
    LEFT JOIN cats
        ON owners.id = cats.owner
WHERE cats.name = 'tommy'

If you only need to find the name tommy cat, then you need to perform two tables left all the data connections, but should first be filtered before the connection, so that queries are much faster. And for this query, the first implementation of the filter does not change the results. So here the best database engine will be optimized according to the order of execution efficiency, generate corresponding implementation plan program.

 

"Do not be a red eyes, feel the world is not worth it."

Guess you like

Origin www.cnblogs.com/yanggb/p/12204499.html