1.问题分析
华为gaussdb 慢sql了很多这个慢sql,执行很慢,把数据库连接对象都占了,导致其他连接不上
分析原因是没有走对索引,执行慢堆积了
select count(0) from `M_Mercadolibre_Notiftransfer_Site` where status=1 -- 5544213
select count(0) from `M_Mercadolibre_Notiftransfer_Site` where status=0 -- 1125
select count(0) from `M_Mercadolibre_Notiftransfer_Site` where status=3 -- 222
select count(0) from `M_Mercadolibre_Notiftransfer_Site` where status=9 -- 323906
select count(0) from `M_Mercadolibre_Notiftransfer_Site` -- 302093746 302216446
2.解决方案
DBA 测试后给的方案是指定索引比较快,先添加一下
`M_Mercadolibre_Notiftransfer_Site` force index(idx_UserId_Status_CreateTime_id)
1.是否可以去掉ORDER BY ,同步每次取200条,同步完成后修改status=0 到status=1,下次取200条就可以了;
2.是否归档这些状态!=0的数据;
原始sql:
SELECT `Id` AS `Id` , `UserId` AS `UserId` , `ApplicationId` AS `ApplicationId` , 2 AS `Type` , `SiteId` AS `Resource` , `Status` AS `Status` , `SentTime` AS `Sent` , `SentTime` AS `Received` FROM `M_Mercadolibre_Notiftransfer_Site` WHERE (((( `UserId` = '678105364' ) AND ( `CreateTime` > timestamp('2023-07-21 13:33:48.975197') )) AND ( `Status` = 0 )) AND NOT( `SiteId` IS NULL OR `SiteId`='')) ORDER BY Id DESC LIMIT 0,200