• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

两张百万级的数据表,联合查询的有关问题

php 搞代码 3年前 (2022-01-23) 18次浏览 已收录 0个评论

两张百万级的数据表,联合查询的问题
两张数据表A,B。
A、B表有两个字段:EXP_CODE ,CODE;
现要找出A、B表中EXP_CODE字段等于 'R8_002_XZ,和EXP_CODE不相同的记录,用下面的语句,如果两表EXP_CODE记录上万的情况下,基本上就死机了,何况到百万级。请问如何改进。

select * from a where EXP_CODE= 'R8_002_XZ ' and code not in (select code from b)

——解决方案——————–
select * from a where EXP_CODE= 'R8_002_XZ ' and code not in (select code from b)

=======================================================

select * from a left join b on a.code=b.code
where a.EXP_CODE= 'R8_002_XZ ' and b.code is null

再看你的code上有无索引,如无,建索引, exp_code 上也建

——解决方案——————–
楼上正解,但是对于百万条记录数据库还是慢的,可以用limit限定
——解决方案——————–
select * from a where exp_code= 'r8_002_xz ' and code not exist(select code from b where exp_code!= 'r8_002_xz ')
上面的语法可能有点问题,但是逻辑关系应该是对的.
要求:version> 4.1,支持子查询.
——解决方案——————–
这个是你要的吗
select * from a left join b on a.code=b.code and a.EXP_CODE=b.EXP_CODE
where a.EXP_CODE= 'R8_002 ' and b.code is null

——解决方案——————–
嵌套的select效率比较低,尽量用inner join, left join代替
——解决方案——————–
上面写错了.改成以下:
select * from a where exp_code= 'r8_002_xz ' and code not in(select code from b where exp_code!= 'r8_002_xz ');
或者
select * from a where exp_code= 'r8_002_xz ' and not exists(select code from b where exp_code!= 'r8_002_xz ' and a.code=b.code);

——解决方案——————–
select * from a left join b o来2源gaodaima#com搞(代@码&网n a.code=b.code and a.EXP_CODE=b.EXP_CODE and b.code is null

你只需要找出a中有而b中没有的记录,然后按b的结构追加就可以了!

辛苦了, “农普 “同行!

——解决方案——————–
哈哈哈…. 遇着了…..


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:两张百万级的数据表,联合查询的有关问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址