记录一次调优经历
事由
重构会员域的代码,重写了注册的接口,当测试进行压测时发现TPS只有20左右(mysql 2C/4G/SSD), 实在不满足需要,进行优化吧。
arthas发现慢的方法
使用trace命令尝试发现执行慢的方法
[arthas@6]$ `---ts=2021-04-28 10:01:28;thread_name=http-nio-8000-exec-8;id=18f;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@2371aaca
`---[169.650847ms] xxx.xxx.domain.person.handler.WechatPersonCalculateHandler$$EnhancerBySpringCGLIB$$63ffac4f:register() [throws Exception]
+---[169.627127ms] org.springframework.cglib.proxy.MethodInterceptor:intercept() #95 [throws Exception]
| `---[10.236216ms] xxx.xxx.domain.person.handler.AbstractPersonCalculateHandler:register() [throws Exception]
| +---[4.791701ms] xxx.xxx.domain.person.handler.AbstractPersonCalculateHandler:validate() #102
| +---[2.749295ms] xxx.xxx.domain.person.handler.AbstractPersonCalculateHandler:analysisCalculateType() #106
| +---[0.005623ms] xxx.xxx.domain.person.handler.AbstractPersonCalculateHandler$AnalysisResult:getCalculateTypeEnum() #107
| +---[0.004135ms] xxx.xxx.domain.person.PersonStatusEnum:getStatus() #109
| +---[0.004008ms] xxx.xxx.domain.person.Person:setMemberStatus() #95
| +---[0.006392ms] xxx.xxx.domain.person.Person:setCreateTime() #111
| +---[0.003577ms] xxx.xxx.domain.person.Person:setUpdateTime() #112
| +---[0.016866ms] xxx.xxx.domain.person.Person:setMemberRegisterTime() #113
| +---[0.00373ms] xxx.xxx.domain.person.handler.AbstractPersonCalculateHandler$CalculateTypeEnum:ordinal() #115
| +---[2.390998ms] xxx.xxx.domain.person.handler.AbstractPersonCalculateHandler:create() #117 [throws Exception]
| `---throw:java.util.concurrent.RejectedExecutionException #2063 [Task xxx.xxx.domain.person.handler.AbstractPersonCalculateHandler$$Lambda$927/1584878773@74b30230 rejected from java.util.concurrent.ThreadPoolExecutor@1b21cf63[Running, pool size = 2, active threads = 2, queued tasks = 1024, completed tasks = 95833]]
发现 validate() 和 analysisCalculateType()这两个方法慢(当时,上面展示的已经是优化后的),就继续将追踪的粒度细到这两个方法(具体耗时就不贴出来了)。最终定位到一个查询语句。结合mysql中的slow_log表(我们的设置是大于1秒就会记录,记录方式是TABLE)找到具体的sql_text(三个用or关联的一个查询语句)。
案例来说这个查询应该是能走索引的,后来通过explan分析,走了另一个索引,并没有走为他而设置的索引。经过几次尝试仍旧没能成功,就将几个用or关联的查询拆成了多个sql语句,分开查询,成功用上期望的索引。查询性能也随之提升。之后的压测已经提高到了350,基本满足了产品的需求。