Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将数据统计优化成批量查询 #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,23 @@ public PageInfo<Article> findPageBreakByCondition(ArticleConditionVO vo) {

List<Article> boList = new LinkedList<>();
Article article = null;
ArrayList<Long> idArr = new ArrayList<Long>();
for (BizArticle bizArticle : list) {
idArr.add(bizArticle.getId());
}
Map<Long, Long> lookMap = this.subLookNumMap(idArr.toArray(new Long[0]));
Map<Long, Long> commentMap = this.subCommentNumMap(idArr.toArray(new Long[0]));
Map<Long, Long> loveMap = this.subLoveNumMap(idArr.toArray(new Long[0]));


for (BizArticle bizArticle : list) {
BizArticle tagArticle = tagMap.get(bizArticle.getId());
if (null == tagArticle) {
log.warn("文章[{}] 未绑定标签信息,或者已绑定的标签不存在!", bizArticle.getTitle());
} else {
bizArticle.setTags(tagArticle.getTags());
}
this.subquery(bizArticle);
this.subquery(lookMap, commentMap, loveMap, bizArticle);
article = new Article(bizArticle);
article.setPassword(null);
boList.add(article);
Expand Down Expand Up @@ -379,28 +388,63 @@ public Article getByPrimaryKey(Long primaryKey) {
if (null == entity) {
return null;
}
this.subquery(entity);
ArrayList<Long> idArr = new ArrayList<Long>();
idArr.add(entity.getId());
Map<Long, Long> lookMap = this.subLookNumMap(idArr.toArray(new Long[0]));
Map<Long, Long> commentMap = this.subCommentNumMap(idArr.toArray(new Long[0]));
Map<Long, Long> loveMap = this.subLoveNumMap(idArr.toArray(new Long[0]));

this.subquery(lookMap, commentMap, loveMap, entity);
return new Article(entity);
}

private void subquery(BizArticle entity) {
Long primaryKey = entity.getId();
private Map<Long, Long> subLookNumMap(Long[] idArr) {
List<BizGroupCount> countList = bizArticleLookMapper.countGroupNumByIdArr(idArr);
Map<Long, Long> map = new HashMap<>();
for (BizGroupCount entity : countList) {
if (entity.getId() != null) {
map.put(entity.getId(), entity.getNum());
}
}
return map;
}

private Map<Long, Long> subCommentNumMap(Long[] idArr) {
List<BizGroupCount> countList = commentMapper.countGroupNumByIdArr(idArr);
Map<Long, Long> map = new HashMap<>();
for (BizGroupCount entity : countList) {
if (entity.getId() != null) {
map.put(entity.getId(), entity.getNum());
}
}
return map;
}

private Map<Long, Long> subLoveNumMap(Long[] idArr) {
List<BizGroupCount> countList = bizArticleLoveMapper.countGroupNumByIdArr(idArr);
Map<Long, Long> map = new HashMap<>();
for (BizGroupCount entity : countList) {
if (entity.getId() != null) {
map.put(entity.getId(), entity.getNum());
}
}
return map;
}

private void subquery(Map<Long, Long> lookMap, Map<Long, Long> commentMap, Map<Long, Long> loveMap, BizArticle entity) {
// 查看的次数
BizArticleLook look = new BizArticleLook();
look.setArticleId(primaryKey);
entity.setLookCount(bizArticleLookMapper.selectCount(look));

// 评论数
Example example = new Example(BizComment.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("sid", primaryKey);
criteria.andEqualTo("status", CommentStatusEnum.APPROVED.toString());
entity.setCommentCount(commentMapper.selectCountByExample(example));

// 喜欢的次数
BizArticleLove love = new BizArticleLove();
love.setArticleId(primaryKey);
entity.setLoveCount(bizArticleLoveMapper.selectCount(love));
if (lookMap.containsKey(entity.getId())){
entity.setLookCount(lookMap.get(entity.getId()).intValue());
}
if (commentMap.containsKey(entity.getId())){
// 评论数
entity.setCommentCount(commentMap.get(entity.getId()).intValue());
}

if (loveMap.containsKey(entity.getId())){
// 喜欢的次数
entity.setLoveCount(loveMap.get(entity.getId()).intValue());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

package com.zyd.blog.persistence.beans;

import com.zyd.blog.framework.object.AbstractDO;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

/**
*
*
* @author generate by HouTu Generator
* @version 1.0
* @date 2021/10/27 16:31
* @since 1.8
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class BizGroupCount extends AbstractDO{
private Long id;
private Long num;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.zyd.blog.business.vo.ArticleLookConditionVO;
import com.zyd.blog.business.vo.CommentConditionVO;
import com.zyd.blog.persistence.beans.BizArticleLook;
import com.zyd.blog.persistence.beans.BizGroupCount;
import com.zyd.blog.plugin.BaseMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
Expand All @@ -27,5 +29,13 @@ public interface BizArticleLookMapper extends BaseMapper<BizArticleLook>{
*/
List<BizArticleLook> findPageBreakByCondition(ArticleLookConditionVO vo);


List<ArticleLookDto> findPageRecentLook(CommentConditionVO vo);

/**
* 根据id批量获取总数
* @param idArr
* @return
*/
List<BizGroupCount> countGroupNumByIdArr(Long[] idArr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.zyd.blog.business.vo.ArticleLoveConditionVO;
import com.zyd.blog.persistence.beans.BizArticleLove;
import com.zyd.blog.persistence.beans.BizGroupCount;
import com.zyd.blog.plugin.BaseMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
Expand All @@ -24,4 +26,11 @@ public interface BizArticleLoveMapper extends BaseMapper<BizArticleLove>{
* @return
*/
List<BizArticleLove> findPageBreakByCondition(ArticleLoveConditionVO vo);

/**
*
* @param idArr
* @return
*/
List<BizGroupCount> countGroupNumByIdArr(Long[] idArr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.zyd.blog.business.vo.CommentConditionVO;
import com.zyd.blog.persistence.beans.BizComment;
import com.zyd.blog.persistence.beans.BizGroupCount;
import com.zyd.blog.plugin.BaseMapper;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -45,4 +46,12 @@ public interface BizCommentMapper extends BaseMapper<BizComment> {
* @param id
*/
BizComment getById(Long id);


/**
* 根据id批量获取总数
* @param idArr
* @return
*/
List<BizGroupCount> countGroupNumByIdArr(Long[] idArr);
}
29 changes: 20 additions & 9 deletions blog-core/src/main/resources/mybatis/BizArticleLookMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<result property="updateTime" jdbcType="TIMESTAMP" column="update_time"/>
</resultMap>

<resultMap id="groupCount" type="com.zyd.blog.persistence.beans.BizGroupCount">
<result property="id" jdbcType="BIGINT" column="id"/>
<result property="num" jdbcType="BIGINT" column="num"/>
</resultMap>

<select id="findPageBreakByCondition" parameterType="com.zyd.blog.business.vo.ArticleLookConditionVO" resultMap="rm">
SELECT
t.*
Expand All @@ -21,14 +26,20 @@
1 = 1
</select>

<select id="findPageRecentLook" resultType="com.zyd.blog.business.dto.ArticleLookDto">
SELECT ba.id AS articleId,
ba.title AS articleName,
bal.user_ip AS userIp,
bal.look_time AS looktime
FROM biz_article_look bal
LEFT JOIN biz_article ba ON ba.id = bal.article_id
ORDER BY bal.look_time DESC
</select>

<select id="countGroupNumByIdArr" parameterType="Long[]" resultMap="groupCount">
SELECT
COUNT(id) as num,
id
FROM
biz_article_look t
WHERE
id IN
<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
GROUP BY id
</select>

</mapper>

20 changes: 20 additions & 0 deletions blog-core/src/main/resources/mybatis/BizArticleLoveMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<result property="updateTime" jdbcType="TIMESTAMP" column="update_time"/>
</resultMap>

<resultMap id="groupCount" type="com.zyd.blog.persistence.beans.BizGroupCount">
<result property="id" jdbcType="BIGINT" column="id"/>
<result property="num" jdbcType="BIGINT" column="num"/>
</resultMap>

<select id="findPageBreakByCondition" parameterType="com.zyd.blog.business.vo.ArticleLoveConditionVO" resultMap="rm">
SELECT
t.*
Expand All @@ -20,5 +25,20 @@
WHERE
1 = 1
</select>


<select id="countGroupNumByIdArr" parameterType="Long[]" resultMap="groupCount">
SELECT
COUNT(id) as num,
id
FROM
biz_article_love t
WHERE
id IN
<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
GROUP BY id
</select>
</mapper>

19 changes: 19 additions & 0 deletions blog-core/src/main/resources/mybatis/BizCommentMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
</association>
</resultMap>

<resultMap id="groupCount" type="com.zyd.blog.persistence.beans.BizGroupCount">
<result property="id" jdbcType="BIGINT" column="id"/>
<result property="num" jdbcType="BIGINT" column="num"/>
</resultMap>
<select id="findPageBreakByCondition" parameterType="com.zyd.blog.business.vo.CommentConditionVO" resultMap="rm">
SELECT
t.*,
Expand Down Expand Up @@ -115,5 +119,20 @@
<update id="doOppose" parameterType="Long">
UPDATE `biz_comment` SET oppose = oppose + 1 WHERE `id`=#{id}
</update>


<select id="countGroupNumByIdArr" parameterType="Long[]" resultMap="groupCount">
SELECT
COUNT(id) as num,
id
FROM
biz_comment t
WHERE
id IN
<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
GROUP BY id
</select>
</mapper>