select id from A where id in (select id from B) 怎样提高执行速度?

chenam 2008-11-10 02:46:07
mysql 语句
select id from A where id in (select id from B)
A,B 表很大时,上万记录,速度会很慢,怎样提高执行速度....
...全文
209 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dobear_0922 2008-11-10
  • 打赏
  • 举报
回复
SELECT a.* FROM ta a
INNER JOIN
(SELECT id FROM tb group by id) b
ON a.id=b.id
ChinaJiaBing 2008-11-10
  • 打赏
  • 举报
回复

select id from a where exists(select 1 from b where a.id=b.id)
Liyingyue_FFS 2008-11-10
  • 打赏
  • 举报
回复
主要还是对A B表的ID加索引
幸运的意外 2008-11-10
  • 打赏
  • 举报
回复
给B表加个索引,并且尽量限制子查询的结果集.
水族杰纶 2008-11-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chenam 的回复:]
你们用的是join
和我上面的应该不一样的吧.
A表中id是主键,B表中有id,xyrid ,id 是主键

select id from A where id in (select xyrid from B)
[/Quote]
select id from A where id in (select id from B) 
--> 變成下面怎麼不一樣了?
select id from A
inner join (select distinct id from B) t
on A.id=t.id

select id from A where id in (select xyrid from B)
-->
select id from A
inner join (select distinct xyrid from B) t
on A.id=t.id
mustudent 2008-11-10
  • 打赏
  • 举报
回复
聽說Exists比in的效率高
chenam 2008-11-10
  • 打赏
  • 举报
回复
你们用的是join
和我上面的应该不一样的吧.
A表中id是主键,B表中有id,xyrid ,id 是主键

select id from A where id in (select xyrid from B)
fcuandy 2008-11-10
  • 打赏
  • 举报
回复
SELECT a.* FROM ta a
INNER JOIN
(SELECT DISTINCT id FROM tb) b
ON a.id=b.id
csdyyr 2008-11-10
  • 打赏
  • 举报
回复

--在id上建立索引
select a.id from a join b on a.id=b.id
hyde100 2008-11-10
  • 打赏
  • 举报
回复
--id上建立索引
select
*
from A
inner join B on A.id=B.id
水族杰纶 2008-11-10
  • 打赏
  • 举报
回复
create index index_name on a(id)
create index index_name on b(id)
select id from A
inner join (select distinct id from B) t
on A.id=t.id
等不到来世 2008-11-10
  • 打赏
  • 举报
回复

--A,B表分别在id上建立索引

select id from A
join (select distinct id from B) t
on A.id=t.id
-晴天 2008-11-10
  • 打赏
  • 举报
回复
select t1.id from A t1 inner join b t2 on t1.id=t2.id
liujuns 2008-11-10
  • 打赏
  • 举报
回复
select *
from A a join B b on a.id =b.id
where b.id =??

在Id上建立索引

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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