数据库教程:SQL Server 使用join all优化 or 查询速度

比如:,master,test, 表示 该用户为 test 的下级代码,test登录后可以看到 test名下的业务和所有下级代理的业务。相关表的结构如下:user表 大约10万条记录|-uid-|-u

比如:,master,test, 表示 该用户为 test 的下级代码,test登录后可以看到 test名下的业务和所有下级代理的业务。相关表的结构如下:

  user表 大约10万条记录  |-uid-|-user-|----site------|  | 1 | test | ,master,   |  | 2 | user | ,master,test,|     product表 大约30万条记录  |-pid-|-product-|-puser-|  | 1 | order01 | test |  | 2 | order02 | user |  | 3 | order03 | user |

优化前的sql语句如下:

  select * from product as p left join user as u on p.puser=u.user where user='test' or site like '%,test,%'

不使用 or 单独查询时,都不超过100毫秒,排除索引的问题。既然单个没问题,两种结果不存在重复记录,可以考虑 join all 优化。

优化后的语句如下:

  select * from product where pid in (    select pid from product where user='test' join all (      select pid from product as p left join user as u on p.puser=u.user where site like '%,test,%'    )  )

用户a名下共有4000+个业务,优化前 优化前11359毫秒,优化后621毫秒
用户b名下共有12个业务,优化前 优化前10359毫秒,优化后78毫秒

到此这篇关于sql server 使用join all优化 or 查询速度的文章就介绍到这了,更多相关sql server join all优化内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多数据库技术:SQL Server 使用join all优化 or 查询速度,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/630710.html

(0)
上一篇 2021年5月30日
下一篇 2021年5月30日

精彩推荐