Which functions of SQL will perform sorting, which reduces the code running speed

The running speed of SQL code affects code debugging and result display, which in turn affects work efficiency. For tens of millions or even hundreds of millions of data, the running speed is more important. There are two main aspects that affect the running speed: hardware and database performance, and the SQL statement itself. Compared with the former, the latter is easier to optimize for data analysts.

The sorting behavior in the code will seriously affect the running speed of SQL. The sorting mentioned here is not only for the ORDER BY function, but also includes the sorting functions performed inside the database. These should also be taken seriously. The functions that will sort are as follows:

  • ORDER BY
  • GROUP BY
  • DISTINCT
  • Aggregate functions (SUM, COUNT, AVG, MAX, MIN)
  • Set operators (UNION, INTERSECT, EXCEPT)
  • Window functions (RANK, DENSE_RANK, ROW_NUMBER, etc.)

SQL is a set-oriented language, and the set operator ALL is optional. For example, when some intermediate tables or views do not care about duplicate data, using UNION ALL is much faster than UNION.

SELECT col_1 FROM t1
UNION ALL
SELECT col_1 FROM t2;

Guess you like

Origin blog.csdn.net/weixin_43167662/article/details/129912030