SQL联合表统计查询慢,十四条记录要查询十到二十分钟
select count(distinct bl.read_card),count(distinct bs.b_no),ws.schoolname from Web_School ws,B_Status bs,B_Lend bl
where bl.schoolId=bs.schoolId and bs.schoolId=ws.schoolId and ws.schoolType='01'
and ws.schoolName like '%杭州%' group by ws.schoolName
这条sql语句,查询要20分钟才能返回查询结果,显示出来也就14条数据,为什么这么慢啊?我在数据库中建索引了,要怎么才能查询快点?求解答,因为我要在java后台用到这条语句。。。小弟新手,有觉得问的不清楚的加我873708974 谢谢大侠
------解决方案--------------------select count(distinct bl.read_card),count(distinct bs.b_no),ws.schoolname from Web_School ws,B_Status bs,B_Lend bl
where bl.schoolId=bs.schoolId and bs.schoolId=ws.schoolId and ws.schoolType='01'
and ws.schoolName like '%杭州%' group by ws.schoolName
schoolName like '%杭州%' 字段的索引是无效的。
最好修改为 子查询方式统计:
select
(select count(distinct bl.read_card) from B_Lend bl where bl.schoolId=ws.schoolId ),
(select count(distinct bs.b_no) from B_Status bs where bs.schoolId=ws.schoolId ),
ws.schoolname from Web_School ws
where ws.schoolType='01'
and ws.schoolName like '%杭州%'