SQL语法顺序及优化点

  语法顺序:

    select   【distinct】 ....from ....【xxx  join】【on】....where....group by ....having....【union】....order by......

  执行顺序:

    from ....【xxx  join】【on】....where....group by ....avg()、sum()....having....select   【distinct】....order by......

优化点

  1、from 子句--执行顺序为从后往前、从右到左。

    因此:表名(最后面的那个表名为驱动表,执行顺序为从后往前, 所以数据量较少的表尽量放后)

  2、where子句--执行顺序为自下而上、从右到左。

    因此:将可以过滤掉大量数据的条件写在where的子句的末尾性能最优

  3、group by 和order by 子句执行顺序都为从左到右。

    因此:select子句少用*号,尽量取字段名称。 使用列名意味着将减少消耗时间。

相关文章:

  • 2022-12-23
  • 2021-10-01
  • 2021-09-25
  • 2022-01-07
  • 2021-04-07
  • 2022-01-12
  • 2022-12-23
  • 2022-02-02
猜你喜欢
  • 2021-12-02
  • 2023-03-30
  • 2021-08-28
  • 2022-12-23
  • 2021-07-18
  • 2022-01-06
相关资源
相似解决方案