赞
踩
先看一段sql:
SELECT
*
FROM
rank_user ASrankUser
LEFTJOINrank_user_levelASuserLevelONrankUser.id = userLevel.user_id
LEFTJOINrank_productASproductONuserLevel.new_level = product.level_id
LEFTJOINrank_product_feeASfeeONuserLevel.fee_id = fee.fee_id
LEFTJOINrank_user_login_statASuserLoginInfoONrankUser.id = userLoginInfo.user_id
ORDERBY
rankUser.create_time DESC
LIMIT 10 OFFSET 0SELECT
*
FROM
rank_user AS rankUser
LEFT JOIN rank_user_level AS userLevel ON rankUser.id = userLevel.user_id
LEFT JOIN rank_product AS product ON userLevel.new_level = product.level_id
LEFT JOIN rank_product_fee AS fee ON userLevel.fee_id = fee.fee_id
LEFT JOIN rank_user_login_stat AS userLoginInfo ON rankUser.id = userLoginInfo.user_id
ORDER BY
rankUser.create_time DESC
LIMIT 10 OFFSET 0
介绍一下这段sql的表的构成:一张主表:rank_user;两张跟rank_user直接关联(多张表通过同一字段最好是主键进行关联)的表:rank_user_level ,rank_user_login_stat ;两张跟rank_user非直接关联的表:rank_product ,rank_product_fee 。这段sql看似简单,但是执行时间却很长,我们来看一下执行计划:
执行时间1.45s,可以看到,这段不仅仅扫描全表,而且使用了临时表,进行了文件排序。
为了找到原因,我们把排序去掉看一下:
SELECT
*
FROM<
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。