Mysql-Select中的子查询如何优化?

最近要做一个zipkin数据访问的统计相关表数据,SQL如下:但是执行效率非常低下,应该是子查询的问题,请问该如何进行优化?

SELECT 
gz.name AS 访问路径,
AVG(gz.duration) AS 平均耗时,
MAX(gz.duration) AS 最大耗时,
MIN(gz.duration) AS 最小耗时,
COUNT(*) AS 平均访问次数,
(SELECT COUNT(*) FROM zipkin_spans  zs
WHERE   zs.name LIKE "%service%" AND zs.trace_id IN (
SELECT  z.trace_id
FROM zipkin_spans z
WHERE z.name = gz.name
)) AS RPC调用次数,
(SELECT COUNT(*) FROM zipkin_spans  zs
WHERE   zs.name LIKE "%mapper%" AND zs.trace_id IN (
SELECT  z.trace_id
FROM zipkin_spans z
WHERE z.name = gz.name
)) AS 数据库调用次数
FROM zipkin_spans gz
WHERE gz.name LIKE "get%"
GROUP BY gz.name`

相关EXPLAIN分析结果如下,请高人解答,本人小白一枚!
Mysql.png

阅读 2.5k
1 个回答

前置%不走索引,其次统计字段建议用冗余字段累计

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏