SQL on&where&having

  1. Among the three clauses that can be conditional on, where, and having, on is executed first, where is second, and having is last. Sometimes the end result is the same if the sequence doesn't affect the intermediate result. However, because on filters the records that do not meet the conditions before performing statistics, it can reduce the data to be processed by intermediate operations, which should be the fastest.
  2. where should be faster than having, because it filters data before sum, so having is the slowest. But it does not mean that having is useless, because sometimes it is necessary to use having when you do not know which record meets the requirements before step 3 comes out.
  3. On is only used when two tables are joined, so when there is one table, there is only a comparison between where and having. In the case of this single table query statistics, if the conditions to be filtered do not involve the fields to be calculated, then their results are the same, but the rushmore technique can be used where, but having cannot, the latter is slower in speed.
  4. If you want to involve a calculated field, it means that the value of this field is uncertain before it is calculated. According to the workflow written in the previous article, the action time of where is completed before the calculation, and having is after the calculation. only works, so in this case the results of the two will be different.
  5. In multi-table join query, on works earlier than where. The system first combines multiple tables into a temporary table according to the join conditions between each table, then filters by where, and then calculates, and then filters by having. It can be seen that in order for the filter condition to work correctly, we must first understand when the condition should work, and then decide where to put it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348130&siteId=291194637