建索引,把小表放在FROM的最后面,把WHERE条件中的多表连接条件放在后面,orderhist.status='1' and orderdet.prodid='901001' 放在where条件前,试试。 你看看SQL 优化方面的知识,就知道怎么做。
如果where 后面只有 orderhist.status='1' and orderdet.prodid='901001' 查询速度很快,但是有很多的重复记录,加上orderhist.orderid=orderdet.orderid and orderhist.contactid = address.contactid and orderhist.contactid = contact.contactid 虽然没有重复记录但是查询速度非常的慢
orderdet.contactid =orderhist.contactid and orderhist.contactid = address.contactid and contact.contactid=address.contactid and phone.contactid = orderhist.contactid and这里都连到contact.contactid去看看? .... orderhist.contactid = contact.contactid and contact.contactid = orderdet.contactidand contact.contactid = address.contactid and contact.contactid = phone.contactid and ....
, (case orderhist.status when '9' then '出库' when '8' then '审核' when '7' then '压单' when '6' then '拒收' when '5' then '完成' when '4' then '换货' when '3' then '退货' when '2' then '发货' when '1' then '订购' when '0' then '取消' end) as 订单状态 这种写法太丑了,应该加一个code表来处理
prodid 和 status都是varchar 类型 ,除非做了特殊说明 如:payment(订购金额 number类型) 否则都是 varchar类型
你看看SQL 优化方面的知识,就知道怎么做。
如果where 后面只有 orderhist.status='1' and orderdet.prodid='901001' 查询速度很快,但是有很多的重复记录,加上orderhist.orderid=orderdet.orderid and
orderhist.contactid = address.contactid and
orderhist.contactid = contact.contactid
虽然没有重复记录但是查询速度非常的慢
会报错 因为orderhist ,orderdet都在括号里面 外面无法引用
表结构已无法改变,联系人的信息室分拆在几张表里,orderhist(订单表) 和 orderdet(订单详情表)是一对多的关系 一张订单里 可能包含有多个产品 如:床上四件套里就有床单,枕头,被子。
orderhist.orderid=orderdet.orderid也很慢,去掉后就快多了 ,另外大侠们能否看一下这个
http://topic.csdn.net/u/20091024/16/2f893c0d-0ed5-4b15-91f9-dc76fe6a135e.html 40分,困扰已久了,顶一下也好啊
orderhist(订单表) 和 orderdet(订单详情表)是一对多的关系
orderhist.orderid=orderdet.orderidand(+)
orderdet.prodid 加个索引再把这两个条件放置在紧挨着where试试,应该比放在后面快
http://topic.csdn.net/u/20091024/16/2f893c0d-0ed5-4b15-91f9-dc76fe6a135e.html
http://topic.csdn.net/u/20091024/16/2f893c0d-0ed5-4b15-91f9-dc76fe6a135e.html
继续顶
我多表查询 得到的结果 有很多重复的记录,where条件后该如何写,一对多的关系是用外连接还是内连接 。?
orderdet.contactid =orderhist.contactid and
orderhist.contactid = address.contactid and
contact.contactid=address.contactid and
phone.contactid = orderhist.contactid and这里都连到contact.contactid去看看?
....
orderhist.contactid = contact.contactid and
contact.contactid = orderdet.contactidand
contact.contactid = address.contactid and
contact.contactid = phone.contactid and ....
估计还是where条件的问题
orderdetid+prodid组合pk索引
查出来的结果也有重复的 ,时间和 产品编号都应该作为where后面的条件 ,可以看看48楼的回复,不加phone表 查出来的结果是正确的,加了就有重复 ,估计还是where后面的条件问题
看看会不会重复啊?
when '9' then '出库'
when '8' then '审核'
when '7' then '压单'
when '6' then '拒收'
when '5' then '完成'
when '4' then '换货'
when '3' then '退货'
when '2' then '发货'
when '1' then '订购'
when '0' then '取消'
end) as 订单状态
这种写法太丑了,应该加一个code表来处理