我在维护系统中遇到了一个查询问题,原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里面出了问题,请指导指导;
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里面出了问题,请指导指导;
--具体要明白查询的数据列
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
select a.* from a,b,c,d where c.aid=a.aid and c.bid=b.bid and a.id=d.id
但是我 的sql是要动态生成,页面需要查询的信息有十几项。
如果每加一项就加多一个表连接,很难生成动态sql呀。
之前对exist的使用是感觉到它的灵活性吧;举个例,我再xml中写到:select * from a,b,c
<if test="isNotEmpty(a)">
exist(.....)
</if>
如果用表连接,灵活性大受影响
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)把数据量大的表放在最右边。