Commit d5f001a8 authored by 李健华's avatar 李健华

Merge branch 'hotfix/wechat-job-sql-optimize-20221024' into 'master'

优化微信定时任务SQL慢查询

See merge request !101
parents fe0d68d5 b1f1b7c4
...@@ -7,6 +7,7 @@ import cn.quantgroup.xyqb.repository.IWeChatInfoRelationRepository; ...@@ -7,6 +7,7 @@ import cn.quantgroup.xyqb.repository.IWeChatInfoRelationRepository;
import cn.quantgroup.xyqb.service.http.IHttpService; import cn.quantgroup.xyqb.service.http.IHttpService;
import cn.quantgroup.xyqb.service.wechat.IWechatFollowService; import cn.quantgroup.xyqb.service.wechat.IWechatFollowService;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -96,6 +97,9 @@ public class WechatFollowServiceImpl implements IWechatFollowService { ...@@ -96,6 +97,9 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
// 游标值 // 游标值
private String cursorValue = ""; private String cursorValue = "";
// 分组数
private Integer groupSize = 1000;
@PostConstruct @PostConstruct
private void init() { private void init() {
// 公众号 // 公众号
...@@ -143,13 +147,16 @@ public class WechatFollowServiceImpl implements IWechatFollowService { ...@@ -143,13 +147,16 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
WechatUserListResponse wechatUserListResponse = JSONObject.parseObject(response, WechatUserListResponse.class); WechatUserListResponse wechatUserListResponse = JSONObject.parseObject(response, WechatUserListResponse.class);
if (wechatUserListResponse.getErrcode() == null && wechatUserListResponse.getCount() > 0) { if (wechatUserListResponse.getErrcode() == null && wechatUserListResponse.getCount() > 0) {
nextOpenId = wechatUserListResponse.getNext_openid(); nextOpenId = wechatUserListResponse.getNext_openid();
List<String> opendIdList = wechatUserListResponse.getData().get("openid"); List<String> opendIdAllList = wechatUserListResponse.getData().get("openid");
if (!opendIdList.isEmpty()) { if (!opendIdAllList.isEmpty()) {
List<List<String>> openIdLists = Lists.partition(opendIdAllList, groupSize);
for (List<String> opendIdList : openIdLists) {
String[] openIds = opendIdList.toArray(new String[0]); String[] openIds = opendIdList.toArray(new String[0]);
String openIdStrs = String.join("\',\'", openIds); String openIdStrs = String.join("\',\'", openIds);
openIdStrs = "\'" + openIdStrs + "\'"; openIdStrs = "\'" + openIdStrs + "\'";
// 查询微信信息 // 查询微信信息
String wxSql = String.format("SELECT `user_id`, `open_id`, `union_id` from wechat_userinfo where app_name='%s' and user_id is not null and `open_id` IN (%s)", WECHAT_XYQB, openIdStrs); String wxSql = String.format("SELECT `user_id`, `open_id`, `union_id` from wechat_userinfo where app_name='%s' and user_id is not null and `open_id` IN (%s)", WECHAT_XYQB, openIdStrs);
System.out.println(wxSql);
List<Map<String, Object>> wxInfoList = jdbcTemplate.queryForList(wxSql); List<Map<String, Object>> wxInfoList = jdbcTemplate.queryForList(wxSql);
ArrayList<Object[]> objectArrayList = new ArrayList<>(); ArrayList<Object[]> objectArrayList = new ArrayList<>();
if (!wxInfoList.isEmpty()) { if (!wxInfoList.isEmpty()) {
...@@ -166,12 +173,14 @@ public class WechatFollowServiceImpl implements IWechatFollowService { ...@@ -166,12 +173,14 @@ public class WechatFollowServiceImpl implements IWechatFollowService {
jdbcTemplate.batchUpdate(sql, objectArrayList); jdbcTemplate.batchUpdate(sql, objectArrayList);
long etime = System.currentTimeMillis(); long etime = System.currentTimeMillis();
System.out.printf("执行时间:%d 毫秒", (etime - stime)); System.out.printf("执行时间:%d 毫秒", (etime - stime));
}
} }
} }
if (nextOpenId != "") { if (nextOpenId != "") {
executeWechatFollowStatus(nextOpenId, period); executeWechatFollowStatus(nextOpenId, period);
} }
return JsonResult.buildSuccessResult("", opendIdList); return JsonResult.buildSuccessResult("", opendIdAllList);
} else { } else {
return null; return null;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment