SQL优化

SQL优化

  • 查询SQL尽量不要使用select *,而是使用具体字段

    1. select * 进行查询时,很可能不会用到索引,避免造成全表扫描
    2. 节省资源,减少网络开销
  • 避免在where子句中使用or来连接条件

    • 反例
    1
    
    select * from employee where id = 1 or salary = 3000;
    
    • 正例

    使用union all

    1
    2
    3
    
    select * from employee where id=1 
    union all
    select * from employee where salary = 3000;
    

    分开两条SQL写

    1
    2
    
    select * from employee where id=1;
    select * from employee where salary=3000;
    
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计