如何优化这个sql,是在mysql中执行,比较慢,每次项目加载时都要等待一分多钟左右数据才出来,求教啊!!

select m.*,a.agent_name from pos_merchant m,agent_info a WHERE m.agent_no=a.agent_no
and m.merchant_no not IN(SELECT i.merchant_no from trans_info i where i.agent_no=a.agent_no)
ORDER BY m.id DESC

select m.*,a.agent_name from pos_merchant m
inner join agent_info a
on m.agent_no=a.agent_no
where m.merchant_no not IN(SELECT i.merchant_no from trans_info i where i.agent_no=a.agent_no)
ORDER BY m.id DESC

尽量不要用not in,改成not exists追问

我直接改成用not exists,语法好像不对了

追答

not in要改成not exists不一定能改的,因为2个逻辑不一样,所以上面的语句没有改,你要根据实际数据看看能不能改
试试:
where not exists(SELECT1 from trans_info i where i.agent_no=a.agent_no
and i.merchant_no = m.merchant_no)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-03-27
大哥啊,,就你这语句能运行就不错了。

m是啥?a是啥?i是啥?怎么没看到在哪AS的啊。。。追问

我晕! 谁说别名一定要加as嘛,可以省略的好吧 这个sql语句我在数据库里都执行过的 语法没问题

追答

好吧,,没看清楚。。

楼下的说的对,不要同时from两个表,用jion。

第2个回答  2014-03-27
你这个sql语句执行后有结果集吗?追问

现在问题尚未解决,sql会进行拼接执行的,分页处理时会先执行一条统计count,然后再加上分页条件执行这个就比较快了, 主要是统计时,耗费时间太长了

以下是执行代码:controller---->service----->dao

相似回答