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,35 +147,40 @@ public class WechatFollowServiceImpl implements IWechatFollowService { ...@@ -143,35 +147,40 @@ 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()) {
String[] openIds = opendIdList.toArray(new String[0]); List<List<String>> openIdLists = Lists.partition(opendIdAllList, groupSize);
String openIdStrs = String.join("\',\'", openIds); for (List<String> opendIdList : openIdLists) {
openIdStrs = "\'" + openIdStrs + "\'"; String[] openIds = opendIdList.toArray(new String[0]);
// 查询微信信息 String openIdStrs = String.join("\',\'", openIds);
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); openIdStrs = "\'" + openIdStrs + "\'";
List<Map<String, Object>> wxInfoList = jdbcTemplate.queryForList(wxSql); // 查询微信信息
ArrayList<Object[]> objectArrayList = new ArrayList<>(); 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);
if (!wxInfoList.isEmpty()) { System.out.println(wxSql);
for (Map<String, Object> wxInfo : wxInfoList) { List<Map<String, Object>> wxInfoList = jdbcTemplate.queryForList(wxSql);
String userId = String.valueOf(wxInfo.get("user_id")); ArrayList<Object[]> objectArrayList = new ArrayList<>();
String openId = String.valueOf(wxInfo.get("open_id")); if (!wxInfoList.isEmpty()) {
String unionId = String.valueOf(wxInfo.get("union_id")); for (Map<String, Object> wxInfo : wxInfoList) {
Object[] objects = new Object[]{userId, openId, unionId, WECHAT_ACCOUNT_STR, period}; String userId = String.valueOf(wxInfo.get("user_id"));
objectArrayList.add(objects); String openId = String.valueOf(wxInfo.get("open_id"));
String unionId = String.valueOf(wxInfo.get("union_id"));
Object[] objects = new Object[]{userId, openId, unionId, WECHAT_ACCOUNT_STR, period};
objectArrayList.add(objects);
}
// 添加公众号数据
String sql = "INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `task_period`) values (?,?,?,?,?)";
long stime = System.currentTimeMillis();
jdbcTemplate.batchUpdate(sql, objectArrayList);
long etime = System.currentTimeMillis();
System.out.printf("执行时间:%d 毫秒", (etime - stime));
} }
// 添加公众号数据
String sql = "INSERT INTO wechat_info_relation(`user_id`, `open_id`, `union_id`, `type`, `task_period`) values (?,?,?,?,?)";
long stime = System.currentTimeMillis();
jdbcTemplate.batchUpdate(sql, objectArrayList);
long etime = System.currentTimeMillis();
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