我在维护系统中遇到了一个查询问题,原sql查询时间历时2分~3分钟,我讲原sql的大概内容向各位简要地描述一下:select * from a,b,c
where
exists (
select 1 from d where a.id = d.id
)
and c.aid=a.aid and c.bid=b.bida表大概有六万多条数据,由于ORM需要Ibatis3生成动态sql,所以就采用了exists(方便拼接),但是查询时间太长了,我怀疑是exists里面出了问题,请指导指导;

解决方案 »

  1.   


    --具体要明白查询的数据列
    select a.* from a,b,c
    where
    exists (select 1 from d where a.id = d.id)
    and c.aid=a.aid and c.bid=b.bid
      

  2.   

    这个有设定的,提问的时候只是为了方便,打上*;其实当我去掉 exsts的时候,结果是很快出来的
      

  3.   

    直接把d表连接进来
    select a.* from a,b,c,d where c.aid=a.aid and c.bid=b.bid and a.id=d.id
      

  4.   

    直接把d表连接进来是 没问题的。
    但是我 的sql是要动态生成,页面需要查询的信息有十几项。
    如果每加一项就加多一个表连接,很难生成动态sql呀。
    之前对exist的使用是感觉到它的灵活性吧;举个例,我再xml中写到:select * from a,b,c
    <if test="isNotEmpty(a)">
    exist(.....)
    </if>
    如果用表连接,灵活性大受影响
      

  5.   

    --具体要明白查询的数据列
    select a.* from b,c,a
    where
    c.aid=a.aid and c.bid=b.bid
    and 
    exists (select 1 from d where a.id = d.id)把数据量大的表放在最右边。
      

  6.   

    呵呵,试过了,比之前快一点,但还是超级慢,我用select count(0)来查,都花了我几分钟
      

  7.   

    exists (select 1 from d where a.id = d.id)为何要用这个。。为何不用关联
      

  8.   

    请问大侠,如何关联呢;如果要动态地生成sql,不知道关联的表有多少呀