SQL优化count方法

如果只是要查询数量,比如下面的代码

@GetMapping("/AdminNum")
public BaseResponse<List<Integer>> getAdmnNum() {
    QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("canReview",0);
    List<Article> list1 = articleService.list(queryWrapper);

    List<Article> list2 = articleService.list();

    List<User> list3 = userFeignClient.list();

    ArrayList<Integer> list = new ArrayList<>();
    list.add(list1.size());
    list.add(list2.size());
    list.add(list3.size());

    return ResultUtils.success(list);
}

 

那就可以优化为

@GetMapping("/AdminNum")
public BaseResponse<List<Long>> getAdmnNum() {
    // 查询 canReview 为 0 的文章数量
    Long articleCount1 = articleService.count(new QueryWrapper<Article>().eq("canReview", 0));

    // 查询所有文章数量
    Long articleCount2 = articleService.count();

    // 查询所有用户数量
    Long userCount = userFeignClient.count();

    // 将数量添加到列表中
    ArrayList<Long> list = new ArrayList<>();
    list.add(articleCount1);  // canReview == 0 的文章数量
    list.add(articleCount2);  // 所有文章数量
    list.add(userCount);      // 用户数量

    return ResultUtils.success(list);
}

 

效果很明显,效率起码快了好几十倍

 

 

暂无评论

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇