在检查慢SQL时,发现一条统计SQL执行过慢,如下:原SQLSELECTplatform,channel,COUNT(DISTINCT(platformUserId))ascntFROM(SELECTplatform,channel,platformUser
在检查慢sql时,,发现一条统计sql执行过慢,如下:
原SQL
SELECT platform, channel, COUNT(DISTINCT(platformUserId)) as cnt FROM(
SELECT platform, channel, platformUserId, MIN(insertTimestamp) as rtime
系统功能强大、操作便捷并具有高度延续开发的内容与知识管理系统,并可集合系统强大的新闻、产品、下载、人才、留言、搜索引擎优化、等功能模块,为企业部门提供一个简单、易用、开放、可扩展的企业信息门户平台或电子商务运行平台。开发人员为脆弱页面专门设计了防刷新系统,自动阻止恶意访问和攻击;安全检查应用于每一处代码中,每个提交到系统查询语句中的变量都经过过滤,可自动屏蔽恶意攻击代码,从而全面防止SQL注入攻击
0
FROM tsz_user
GROUP BY platform, channel, platformUserId
) a where a.rtime >= 1392393600 and a.rtime
GROUP BY platform, channel;
执行时间:

耗时2分33秒
优化后SQL
SELECT platform, channel, COUNT(DISTINCT(platformUserId)) as cnt FROM(
SELECT platform, channel, platformUserId, MIN(insertTimestamp) as rtime
FROM tsz_user
GROUP BY platform, channel, platformUserId order by null
) a where a.rtime >= 1392393600 and a.rtime
GROUP BY platform, channel order by null;
执行时间:

耗时55.22秒
执行计划:

结论:
默认情况下,MySQL对所有GROUP BY col1,col2...的字段进行排序。如果查询包括GROUP BY,想要避免排序结果的消耗,则可以指定ORDER By NULL禁止排序。
参考手册:

本文出自 “贺春旸的技术专栏” 博客,请务必保留此出处
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号