使用内部联接更改此查询?

时间:2010-11-03 21:16:42

标签: sql mysql

我在使用内连接时遇到了一些问题:

select count(distinct id) 
  from svn1,
       svn2 
 where svn1.revno = svn2.revno 
   and svn1.type = 'Bug' 
   and svn2.authors IN (select authors 
                          from svn2 
                         where revno = '33')

如何使用内连接更快地加快速度?我对内部联接的查询给出了奇怪的结果。

svn1的表格信息: 专栏:id revno type 数据:
1 22 Bug
1 23 Change
1 24 Bug
2 33 Bug
2 34 Bug

svn2的表格信息: 专栏:revno authors 数据:
22 A
22 B
22 C
33 D
33 A
33 C

I want ids of type bug which have a common author with authors of revno 33. i.e ids which also have revno with authors A,D or C in it

一般来说,我也想要一个查询来回答一个id,找到其他有共同作者的id。

6 个答案:

答案 0 :(得分:2)

select count(distinct svn1.id) 
 from svn1
 inner join svn2 on svn1.revno = svn2.revno 
where
   svn1.type='Bug' 
   and svn2.authors IN (select authors 
                          from svn2 
                         where revno='33')

revno和作者是否有索引?

答案 1 :(得分:2)

你需要这样的东西:

select count(distinct id)
from svn1 inner join svn2 on svn1.revno = svn2.revno
      inner join svn2 second on svn2.authors = second.authors
where svn1.type='Bug' and and second.revno='33'

答案 2 :(得分:1)

一般来说,我也想要一个查询来回答一个id,找到其他有共同作者的id。

select distinct svn1.id 
from svn2
join svn2 link on svn2.author = link.author
join svn1 on link.revno = svn1.revno
where svn2.revno = '33'

以下

这有用吗?

select count(distinct id) 
from svn1
inner join svn2 on svn1.revno = svn2.revno and svn1.authors = svn2.authors
where sv1.type = 'bug' and sv2.revno = '33'  

答案 3 :(得分:0)

select count(distinct id)
  from svn1 s1
  inner join svn2 s2 on s1.revno = s2.revno
  inner join svn2 s2b on s2.authors=sb2.authors and s2b.revno='33'
where
  and svn1.type = 'Bug' 

我认为这相当于您现有查询的功能。它可能会提高你的速度;但是,如果没有看到DB,我无法分辨。

比重构查询更重要的是,您的数据库表是否在revno字段上有索引?如果没有,请添加一个 - 这比任何修改查询的效果要大得多。

答案 4 :(得分:0)

你说你想要的实际ID不是数,所以我这样做了。

   select distinct s1.id 
      from svn1 s1
      join svn2  s2
     ON s1.revno = s2.revno 
    Where s1.type = 'Bug'  
    and s2.authors IN (select authors  
                              from svn2  
                             where revno = '33') 

现在将来,你需要停止使用隐式连接,它们是一种非常糟糕的做法。它们很难正确维护,受到显式连接不受影响的错误的影响,因此比使用显式连接更具风险,它们使开发人员不能很好地理解连接以正确查询,并且当您需要使用左连接时它们会产生困难内部联合,他们已经过时了18年。

答案 5 :(得分:-1)

我不是100%清楚你要做什么,但这是我的尝试:

select count(distinct id)
from svn1
inner join svn2 on svn1.revno = svn2.revno
where svn1.type = 'Bug'
and svn2.revno = '33'
相关问题