日期:2014-05-18  浏览次数:20587 次

select distinct的效率问题
SQL code
select distinct d.*,cast(box_no as int),e.customer_code,....  from .... order by customer_code,cast(box_no as int)



其中select 后面总共列出了24个字段,共有6张表联查,基本都是通过“left outer join ... on ...”这样的条件来连接的,感觉查询很慢,只查询出来10万条记录都需要大约7秒钟,不知道如何改进?

另外,我吧最后的那个排序由“order by customer_code,cast(box_no as int)”改为“order by d.id” 之后,速度快了大约一秒,但是我并不需要d.id这个排序,怎么办?这其中的奥妙是什么?怎样提高速度?


------解决方案--------------------
cast(box_no as int)是函数 利用不了索引。

DISTINCT效率不高,如果用GROUP BY 代替 GROUP BY 的字段加索引

效率也许会高些
------解决方案--------------------
cast(box_no as int)--Box_no显示0001格式不用转换

在连接表条件列上建上索引
------解决方案--------------------
D.ID估计是主键 默认是聚集索引。
------解决方案--------------------
探讨
cast(box_no as int) 会不会影响速度?这个函数仅仅是用来排序用的,并不是非要用这个函数的


引用:
cast(box_no as int)--Box_no显示0001格式不用转换

在连接表条件列上建上索引