SQL where in 数据量很大时用循环代替?

wyc9296 2017-11-20 10:26:10
举个例子(实际情况更复杂些):我搭建了一个网页,用户会输入几千个数据查询数据库: select product_name, deliver from product where product_name in (用户输入的几千个name)。

这时我用foreach循环代替可以不?即在select外部加一个循环,每次查询一个product_name,循环几千次,这样效率会不会比用in更高点?谢谢~
...全文
1217 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdsnhan 2017-11-21
  • 打赏
  • 举报
回复
引用 6 楼 qq646748739 的回复:

--1.创建一个临时表,存储用户输入的几千个name
create table tmp as select 用户输入的几千个name as product_name from dual;

--2.SQL实现
select a.product_name, a.deliver
  from product a
 where exists (select null from tmp b where b.product_name = a.product_name);
create table tmp as select 用户输入的几千个name as product_name from dual; 这样的话,超过4000个字符,就会报错,提示超长。 建议楼主把应用场景描述的更具体一些
zbdzjx 2017-11-21
  • 打赏
  • 举报
回复
引用 9 楼 minsic78 的回复:
[quote=引用 8 楼 zbdzjx 的回复:] 类似这样: select product_name, deliver from product where product_name in ( select name1 from dual union all select name2 from dual union all select name3 from dual union all select name4 from dual union all select name5 from dual union all ...... select name1000 from dual ) name1、name2、name3……name1000,是用户输入的值,括号中的内容是通过输入的值,拼出来的SQL语句。
这是为了规避in列表太长?[/quote] 是的,好像是in里面的值过多,会报错。
minsic78 2017-11-21
  • 打赏
  • 举报
回复
引用 8 楼 zbdzjx 的回复:
类似这样: select product_name, deliver from product where product_name in ( select name1 from dual union all select name2 from dual union all select name3 from dual union all select name4 from dual union all select name5 from dual union all ...... select name1000 from dual ) name1、name2、name3……name1000,是用户输入的值,括号中的内容是通过输入的值,拼出来的SQL语句。
这是为了规避in列表太长?
zbdzjx 2017-11-21
  • 打赏
  • 举报
回复
类似这样: select product_name, deliver from product where product_name in ( select name1 from dual union all select name2 from dual union all select name3 from dual union all select name4 from dual union all select name5 from dual union all ...... select name1000 from dual ) name1、name2、name3……name1000,是用户输入的值,括号中的内容是通过输入的值,拼出来的SQL语句。
wyc9296 2017-11-20
  • 打赏
  • 举报
回复
引用 1 楼 听雨停了的回复:
[quote=引用 楼主 wyc9296 的回复:] 举个例子(实际情况更复杂些):我搭建了一个网页,用户会输入几千个数据查询数据库: select product_name, deliver from product where product_name in (用户输入的几千个name)。 这时我用foreach循环代替可以不?即在select外部加一个循环,每次查询一个product_name,循环几千次,这样效率会不会比用in更高点?谢谢~

--首先把用户输入的几千个product_name插入一个临时表,然后用join去连接,这样的话很快,比你用in,循环快多了
select a.product_name, a.deliver from product a
INNER JOIN #tab b ON a.product_name=b.product_name
--where product_name in (用户输入的几千个name)
[/quote] 具体怎么写sql呢?
minsic78 2017-11-20
  • 打赏
  • 举报
回复
这种情况下,前台应该是分页的吧?如果是分页的话那么可能还好点,name上的索引可以给你带来一些帮助,当然可能需要在这索引或者这个字段上上做点小手脚。 如果把情况想的更简单点:就只有这么单张表的查询,这张表总数据量大概多少?这几千个name进去后,能从这张表上获取到多少条记录?如果少的话,比如和name是1:1的关系,也就几千条,而总数据量可能是几十万几百万甚至更多,那么name上的索引就能解决问题。 当然你说情况要更复杂,不知道复杂到什么程度,所以也不下定论。
听雨停了 2017-11-20
  • 打赏
  • 举报
回复
引用 楼主 wyc9296 的回复:
举个例子(实际情况更复杂些):我搭建了一个网页,用户会输入几千个数据查询数据库: select product_name, deliver from product where product_name in (用户输入的几千个name)。 这时我用foreach循环代替可以不?即在select外部加一个循环,每次查询一个product_name,循环几千次,这样效率会不会比用in更高点?谢谢~

--首先把用户输入的几千个product_name插入一个临时表,然后用join去连接,这样的话很快,比你用in,循环快多了
select a.product_name, a.deliver from product a
INNER JOIN #tab b ON a.product_name=b.product_name
--where product_name in (用户输入的几千个name)
碧水幽幽泉 2017-11-20
  • 打赏
  • 举报
回复

--1.创建一个临时表,存储用户输入的几千个name
create table tmp as select 用户输入的几千个name as product_name from dual;

--2.SQL实现
select a.product_name, a.deliver
from product a
where exists (select null from tmp b where b.product_name = a.product_name);

  • 打赏
  • 举报
回复
用 where exists 做只查询吧
听雨停了 2017-11-20
  • 打赏
  • 举报
回复
引用 3 楼 wyc9296 的回复:
[quote=引用 1 楼 听雨停了的回复:][quote=引用 楼主 wyc9296 的回复:] 举个例子(实际情况更复杂些):我搭建了一个网页,用户会输入几千个数据查询数据库: select product_name, deliver from product where product_name in (用户输入的几千个name)。 这时我用foreach循环代替可以不?即在select外部加一个循环,每次查询一个product_name,循环几千次,这样效率会不会比用in更高点?谢谢~

--首先把用户输入的几千个product_name插入一个临时表,然后用join去连接,这样的话很快,比你用in,循环快多了
select a.product_name, a.deliver from product a
INNER JOIN #tab b ON a.product_name=b.product_name
--where product_name in (用户输入的几千个name)
[/quote] 具体怎么写sql呢?[/quote] 不是说的很清楚了吗?把你in里面的内容单独插入一个表,然后用上面的连接方式连接一下

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧