首页 > web前端 > js教程 > 正文

Cron 作业中聚合的力量和成本效益

碧海醫心
发布: 2024-09-24 08:03:41
转载
765人浏览过

cron 作业中聚合的力量和成本效益

在开发我的 saas 产品时,我发现,对于 10k 用户,您每天需要 10,001 次查询,并定期进行数据库查询来重置积分或免费提示。通过智能聚合,无论您有 10k 还是 100k 用户,您都只需要 2 次查询!

首先,让我给您一些 mongodb 生产数据库的成本审查(10k 和 1 年):

正常方式,每日查询:10,001
年度查询:10,001 x 365 = 3,650,365 次查询
年度费用:3,650,365 x 0.001 美元 = 3,650.37 美元

聚合方式,每日查询:2
年度查询:2 x 365 = 730 次查询
年度费用:730 x 0.001 美元 = 0.73 美元

节省:3,650.37 - 0.73 = 3,649.64 美元(近 40 万泰铢)

太棒了,现在看看传统的查询方法(为每个用户进行一个查询)

const resetlimitsforusers = async () => {
  const users = await user.find({ /* conditions to select users */ });

  for (const user of users) {
    if (user.plan.remaining_prompt_count < 3 || user.plan.remaining_page_count < 3) {
      user.plan.remaining_prompt_count = 3;
      user.plan.total_prompt_count = 3;
      // save updated plan
      await user.plan.save();
    }
  }
};

登录后复制

这里,如果您有 10,000 个用户,这会导致 10,001 个查询(每个用户 1 个,加上用于获取用户的初始查询) - 这是巨大的..

现在是英雄条目,[看起来有点困难,但它可以节省大量的钱]

const resetPlanCounts = () => {
  cron.schedule('* * * * *', async () => {
    try {
      const twoMinutesAgo = new Date(Date.now() - 2 * 60 * 1000); // 2 minutes ago

      const usersWithRegisteredPlan = await User.aggregate([
        {
          $match: {
            createdAt: { $lte: twoMinutesAgo },
            plan: { $exists: true }
          }
        },
        {
          $lookup: {
            from: 'plans',
            localField: 'plan',
            foreignField: '_id',
            as: 'planDetails'
          }
        },
        {
          $unwind: '$planDetails'
        },
        {
          $match: {
            'planDetails.name': 'Registered',
            $or: [
              { 'planDetails.remaining_prompt_count': { $lt: 3 } },
              { 'planDetails.remaining_page_count': { $lt: 3 } }
            ]
          }
        },
        {
          $project: {
            planId: '$planDetails._id'
          }
        }
      ]);

      const planIds = usersWithRegisteredPlan.map(user => user.planId);

      if (planIds.length > 0) {
        const { modifiedCount } = await Plan.updateMany(
          { _id: { $in: planIds } },
          { $set: { remaining_prompt_count: 3, total_prompt_count: 3, remaining_page_count: 3, total_page_count: 3 } }
        );

        console.log(`${modifiedCount} plans reset for "Registered" users.`);
      } else {
        console.log('No plans to reset for today.');
      }
    } catch (error) {
      console.error('Error resetting plan counts:', error);
    }
  });
};
登录后复制

这就是您如何运行 cron 作业 [它在特定时间自动运行] 来更新所有 10k 用户积分或限制,这可以在一年内节省超过 3600 美元。

作者,
姓名:mahinur ra​​hman
联系方式:dev.mahinur.rahman@gmail.com

以上就是Cron 作业中聚合的力量和成本效益的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号