针对以下sql进行解析
EXPLAIN SELECT
dauk.id AS daukId,
dasm.mailbox AS storeAccount,
dau.id,
dau.id AS userId,
das.score AS score,
das.sell_num AS sellNum,
dapa.product_link AS productLink,
dapa.able_category_ids AS ableCategoryIds,
dac.parent_name AS parentName,
dau.phone,
dau.login_time AS loginTime,
dau.create_time AS createTime,
dau.total_count AS totalCount,
dauk.date_limit AS dateLimit,
dauk.open_time AS openTime,
dau.try_date_num AS tryDateNum,
dauk.kamil AS kamil,
dauk.machine AS machine,
adminuser.username AS salesmanName,
serviceuser.username AS serviceTeacherName,
beforeserviceuser.username AS beforeServiceTeacherName,
dau.salesman_id as salesmanId,
dau.login_count AS loginCount,
dap.product_name AS productName,
dap.commission_rate AS commissionRate,
dap.price,
das.store_name storeName,
(SELECT count(dasmr.id) FROM dig_anchor_shop_message_record dasmr
WHERE dasmr.dig_user_id = dauk.dig_user_id AND dasmr.type = 1
) AS type1TotalCount,
(SELECT count(dasmr.id) FROM dig_anchor_shop_message_record dasmr
WHERE dasmr.dig_user_id = dauk.dig_user_id AND dasmr.type = 2
) AS type2TotalCount,
(SELECT count(daspr.id) FROM dig_anchor_shop_private_record daspr
WHERE daspr.dig_user_id = dauk.dig_user_id AND daspr.type = 4
) AS productCard,
dauk.message_state AS messageId,
dauk.sign_state AS signId
FROM
dig_anchor_user_kimil dauk
LEFT JOIN dig_anchor_user AS dau ON dau.id = dauk.dig_user_id and dau.deleted = 0
LEFT JOIN admin_user adminuser ON adminuser.id = dau.salesman_id AND adminuser.deleted = 0
LEFT JOIN admin_user serviceuser ON serviceuser.id = dau.service_teacher_id AND serviceuser.deleted = 0
LEFT JOIN admin_user beforeserviceuser ON beforeserviceuser.id = dau.before_service_teacher_id AND beforeserviceuser.deleted = 0
LEFT JOIN dig_anchor_store das ON das.dig_user_id = dauk.dig_user_id and das.deleted = 0
LEFT JOIN dig_anchor_product_activity dapa ON dapa.store_id = das.id AND
dapa.state = 1 and dapa.deleted = 0
LEFT JOIN dig_anchor_product dap ON dap.id = dapa.product_id and dap.deleted = 0
LEFT JOIN shop_product_category dac ON dac.id = dapa.able_category_ids and dac.deleted = 0
LEFT JOIN dig_anchor_service_mailbox dasm ON dasm.id = dauk.store_account and dasm.deleted = 0
WHERE
dauk.deleted = 0
结果:
EXLPLAIN:参数分析
id:执行序列号,先根据id大的优先执行,再根据相同id从上至下的执行。
select_type:查询类型,primary(外层查询),DEPENDENT SUBQUERY(子查询),
partitions:分区,(暂未用到)
type:连接类型,system > const(主键或唯一) > eq_ref(主键或唯一索引相连接) > ref (普通索引相连或者联合索引)> range > index > all。
possible_keys:sql可能用到的索引
key:实际用到的索引
key_len:索引长度
ref:显示索引的哪一列或常量被用于查找索引列上的值,如db_seekanchor.dauk.dig_user_id表示dauk的dig_user_id用来做索引查询,const表示常量,type=4这个常量作为索引
rows就是mysql认为必须要逐行去检查和判断的记录的条数,值越大越不好。
extra:出现Using filesort需要优化
extra
Using filesort: 说明MySQL会对数据使用一个外部的索引排序,而不是按照表内的索引顺序进行读取。MySQL中无法利用索引完成的排序操作称为“文件排序” 。出现这个就要立刻优化sql。
Using temporary: 使用了临时表保存中间结果,MySQL在对查询结果排序时使用临时表。常见于排序 order by 和 分组查询 group by。 出现这个更要立刻优化sql。
Using index: 表示相应的select 操作中使用了覆盖索引(Covering index),避免访问了表的数据行,效果不错!如果同时出现Using where,表明索引被用来执行索引键值的查找。如果没有同时出现Using where,表示索引用来读取数据而非执行查找动作。
覆盖索引(Covering Index) :也叫索引覆盖,就是select 的数据列只用从索引中就能够取得,不必读取数据行,MySQL可以利用索引返回select 列表中的字段,而不必根据索引再次读取数据文件。
Using index condition: 会先条件过滤索引,过滤完索引后找到所有符合索引条件的数据行,随后用 WHERE 子句中的其他条件去过滤这些数据行。
Using where: 表示不能从索引中得到结果(覆盖索引),需要通过索引回表查询数据。
Using join buffer: 表明使用了连接缓存。
impossible where: where 语句的值总是false,不可用,不能用来获取任何元素。
distinct: 优化distinct操作,在找到第一匹配的元组后即停止找同样值的动作。