【发布时间】:2014-10-26 12:26:22
【问题描述】:
我的数据库中有近 10000 条记录。当查询未加入时,通过 PHP 发送到 mysql 的查询运行速度非常快。但是当我们使用左外连接来连接表时,速度非常慢。
这是我的查询。
$sql = "select
rollno,
stuname,
total,
ifnull(sum(fpaid),0) as feepaid,
total-COALESCE(sum(fpaid),0) as balance,
status,
comments,
category
from
(select
studet.rollno as rollno,
studet.stuname as stuname,
studet.course as course,
studet.branch as branch,
studet.category as category,
studet.year as year,
studet.total as total,
studet.status as status,
studet.comments as comments,
studet.academic as academic ,
stufeeref.feepaid as fpaid
from studet
left outer join stufeeref
on studet.rollno = stufeeref.rollno
and studet.year = stufeeref.year
) as T
where branch='$branch'
and year='$year'
and academic='$academic'
and course='$course'
group by rollno,year";
请告诉我如何优化我的查询。 提前致谢。
【问题讨论】:
-
您可以为
studet和stufeeref表添加表定义吗?使用SHOW CREATE TABLE studet获取表定义。 -
你能把
show create table studet;和show create table stufeeref的结果贴出来吗?查看表格将有助于确定是否有任何索引可能有帮助。此外,可能会删除子查询
标签: mysql