原sql,使用or的情况
SELECT * FROM coin_trade_history WHERE (buyer_uid = ${userId} OR seller_uid = ${userId}) order by id desc 

sql查询优化or篇

在有索引的情况可以使用UNION ALL函数,优化后的sql

SELECT * FROM coin_trade_history WHERE buyer_uid = ${userId}  UNION ALL

SELECT * FROM coin_trade_history WHERE  seller_uid = ${userId})  order by id desc

sql查询优化or篇

 

如果查询上有索引,union all比or快,因为前者会利用索引查找,or会使索引失效;

如果查询上没有索引,or比union快,因为前者查询引擎会一次性完成指令分析。

 

 

 

 

 

相关文章:

  • 2022-01-23
  • 2021-08-04
猜你喜欢
  • 2022-12-23
  • 2021-11-20
  • 2022-03-01
  • 2021-07-11
相关资源
相似解决方案