试一下:
select field1
from db1.table1 where exists (
select 1 from  db2.table2 
where  db1.table1.field1 = db2.table2.field2
and  condition)

解决方案 »

  1.   

    select t1.field1 
    from db1.table1 t1 
    where exists(select * 
                 from db2.table2 t2 
                 where t1.field1=t2.field2 and ...)
      

  2.   

    我的数据库有个表也有百万条记录,
    如果查找几十条记录,一点都不慢。
    不知你为何如此?
    如果用的是SQL Server2000,
    可以分析出时间都花在什么地方了,
    哦,字段类型,
    db1.table1.field1 = db2.table2.field2
    数字比字符串快,
    此外,没有必要给每个字段加索引,
    不然,添加删除会慢点。
      

  3.   

    对where子句进行优化,主要在对索引的利用。。当然还有些算法优化,,,
    少用in,还有把有索引的字段的where条件放在离where最近的地方。
      

  4.   

    影响性能的硬件因素:cpu,内存,磁盘
    围绕它们,您可以试试(比如双cpu什么的,呵呵)
      

  5.   

    根据“where xxxxx”的组合建立索引,则能将全部字段建立索引呢?比如字段a上重复的值特别多,那么建立索引不一定比不建立索引快。这样建立索引是错误的方法。如果是“where a=... and b=... and c=...”,那么就将“a,b,c”建在同一个索引中,这样这个索引才能提高查询效率。
      

  6.   

    你的field1和field2如果是上述“不适合建立索引”的字段,那么建立索引没有用。还是先用“select distinct”之类的语句生成精简的中间结果,然后再join吧。
      

  7.   

    另外要知道,“select db1.table1.field1 from db1.table1 join db2.table2 on db1.table1.field1 = db2.table2.field2 [where condition]”等价于“select db1.table1.field1 from db1.table1,db2.table2 where db1.table1.field1 = db2.table2.field2 and [condition]”。
    如果用:
    select t1.field1 
    from db1.table1 t1 
    where exists(select * 
                 from db2.table2 t2 
                 where t1.field1=t2.field2 and ...)
    则要保证t1的记录书很少,否则就用db2和db1对调更好,总之外层表的记录述要最少。
      

  8.   

    数据库处理 exists 子查询不能优化必须设计时注意。