请大侠帮我看看这句sql的性能问题,跑起来总是很慢,要5-10秒

来源:百度知道 编辑:UC知道 时间:2024/06/23 02:13:38
select p.*,F_1ST_PRODUCT_ATTACH(p.product_id,2) as attach_file,
f_product_children_num(p.product_id) children_total,diff_elements(p.product_id) diff_elements
from tbl_product_base p
left outer join tbl_product_brand b
on p.brand_id=b.brand_id
left outer join tbl_ad_web_recommend r
on p.product_id=r.product_id and r.location_id=1 and (r.status is null or r.status='0')
where p.product_id in (select product_id from v_product_onsale)
and p.parent_id is null
and p.category_id in (
select category_id from tbl_product_category a
start with a.category_id=#category_id#
connect by a.parent_id=PRIOR a.category_id
)
<isNotNull property="queryWords">
and (
p.product_name_web like '%$queryWords$%'
or
b.brand_name like '%$queryWords$%'
)
</isNotNull>
<isEqual property="orderBy" com

对tbl_product_category的树状查询量大不大?
还有建议把r.location_id=1 and (r.status is null or r.status='0')
写到最下面,做过滤条件不要做关联条件
WHERE后面的LIKE和IN统统向上挪,把能确定并立即过滤的条件,像p.parent_id is null这样的条件写到最下面……
如果还是很慢,就检查索引和Analyze吧……
谁JOIN谁个人感觉没什么关系,楼主的写法是HashJoin,不是MergeJoin,不会有额外的效率损耗的……

如果你的tbl_product_base表数据量大的话,肯定会很慢的:
全部每条记录与tbl_product_brand表和tbl_ad_web_recommend的每条记录JOIN一次.然后再加tbl_product_base的条件.

当然应该先根据条件把需要的tbl_product_base表的数据筛选出来,再JOIN.
这样速度就会快很多

表里的索引 搞号没哦