请哪位仁兄给偶补补 IN 语句

海盗2019 2005-02-05 10:46:40
好像效率不高,执行起来会 LOOP 所有的 IN 数据。
不过有时候又必须得这么用,:(
如果 IN 数据非常多的话,应该怎么改造呢?
比如:
select A.*, B.chinese_name,B.singer
from resource_detail A, resource B
where A.resource_id = 2038
and A.filetype_id in (
43,141,142,144,153,207,232,306,331,383,408,460,485,537,562,614,639,691 )
and B.resource_id = A.resource_id;
...全文
111 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
海盗2019 2005-02-06
  • 打赏
  • 举报
回复
昨天的测试数据:
1. 联查+IN

11:27:17
11:30:22

2. 联查和IN分开

17:12:35
17:16:00

3. 建临时表
14:37:17
14:41:48

今天再次访问使用临时表的,只需要几秒,。难道是昨天网速太慢的缘由?不得而知,就用临时表了。
bisliu 2005-02-05
  • 打赏
  • 举报
回复
建表,然后用外连接
qiaozhiwei 2005-02-05
  • 打赏
  • 举报
回复
用or的效果会好一点,最好建表
fuxia 2005-02-05
  • 打赏
  • 举报
回复
select A.*, B.chinese_name,B.singer
from resource_detail A, resource B
where B.resource_id = A.resource_id and A.resource_id = 2038
and A.filetype_id in (
43,141,142,144,153,207,232,306,331,383,408,460,485,537,562,614,639,691 ) ;
hjx000 2005-02-05
  • 打赏
  • 举报
回复
select A.*, B.chinese_name,B.singer
from resource_detail A, resource B
where A.resource_id = 2038
and (
A.filetype_id=43 or
A.filetype_id=141 or
A.filetype_id=142 or
A.filetype_id= 44......
153,207,232,306,331,383,408,460,485,537,562,614,639,691
)
and B.resource_id = A.resource_id;
ORARichard 2005-02-05
  • 打赏
  • 举报
回复
建张表保存(43,141,142,144,153,207,232,306,331,383,408,460,485,537,562,614,639,691 )
然后做表关联
海盗2019 2005-02-05
  • 打赏
  • 举报
回复
数据提过方正在加索引。
海盗2019 2005-02-05
  • 打赏
  • 举报
回复
那个字段没有建索引,in里面的数据一般有20几条,本身数据就有几十万,查询一条数据都得花费3s左右的时间,一次要查询几十条数据就得花不少时间啦。
准备建个事务级的临时表,以为每个查询请求的这些id都是不一样的。
访问程序一次,这些id是一样的;下次再访问的话,就可能不是这些id了;id的值是根据用户来定的。

(转事务级临时表)
2)事务级临时表是指该临时表与事务相关,当进行事务提交或者事务回滚的时候,临时表中的数据将自行被截断,其他的内容和会话级的临时表的一致(包括退出SESSION的时候,事务级的临时表也会被自动截断)。事务级临时表的创建方法:Create Global Temporary Table Table_Name(Col1 Type1,Col2 Type2...) On Commit Delete Rows;举例:create global temporary table Classes(Class_id Number(5),Class_Name Varchar2(8),Class_Memo varchar2(200)) on Commit delete Rows ;

是不是只要一commit,insert到临时表中的数据就全部被删除了?
GerryYang 2005-02-05
  • 打赏
  • 举报
回复
就用in
因为有些时候是没有办法的.
in不用影响速度,
除非索引没有建立好
zlindong 2005-02-05
  • 打赏
  • 举报
回复
ORARichard 2005-02-05
  • 打赏
  • 举报
回复
根据你的具体情况来定,如果该表中的数据变化不很频繁,或维护方便的话,就不用临时表,就可以当作你数据库中的表对待。
海盗2019 2005-02-05
  • 打赏
  • 举报
回复
Rechard, tab是临时表?
ORARichard 2005-02-05
  • 打赏
  • 举报
回复
--e.g.

create table tb as
select 43 id from dual union all
select 141 id from dual union all
select 142 id from dual union all
select 144 id from dual union all
select 153 id from dual union all
select 207 id from dual union all
select 232 id from dual union all
select 306 id from dual union all
select 331 id from dual union all
select 383 id from dual union all
select 408 id from dual union all
select 460 id from dual union all
select 485 id from dual union all
select 537 id from dual union all
select 562 id from dual union all
select 614 id from dual union all
select 639 id from dual union all
select 691 id from dual;

select A.*, B.chinese_name,B.singer
from resource_detail A, resource B,tb C
where A.resource_id = 2038
and A.filetype_id = C.id
and B.resource_id = A.resource_id;

--如果A表中的filetype_id存在空值也就是NULL的时候,上面的and A.filetype_id = C.id
要改成
and (A.filetype_id = C.id or A.filetype_id is null)
当然这个改动完全是你原句写法的要求,因为原句中的in(...)是表示A.filetype_id为空也符合要求。
海盗2019 2005-02-05
  • 打赏
  • 举报
回复
各位大GUO,这个filetype_id是建在另外一张表中的,filetype_id_resource
查询语句是:select filetype_id from filetype_id_resource where id = ?
这个id是唯一的。

做表关联?就是将resource_detail A表中的filetype_id建外键,关联到filetype_id_resource.filetype_id 吗?

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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