NodeJS 如何加快Sequelize findAndCountAll的速度?

yxyvkwin  于 2023-01-30  发布在  Node.js
关注(0)|答案(1)|浏览(190)

我使用Sequelize进行分页
router.js

router.post('/getall', async (req, res) => {
  try {
    const { q, page, limit, order_by, order_direction } = req.query;
    const { candidate, position, filters } = req.body

      let include = [
        {
          model: SortedLvl,
          where: {
            lvl: filters.jobLvl.name,
            months: { [Op.gte]: filters.jobMinExp.value },
          },
        },
        {
          model: SortedLastJob,
          where: { jobposition: filters.jobType.name }
        },
        {
          model: SortedSkills,
        }
      ]
      let search = {};
      let order = [];

      let filterCandidate = {}
      if (candidate) {
        if (candidate != undefined) {
          t = candidate.split(/[ ,]+/)
          let arr = new Array()
          // console.log('t', t);
          t.map((el, index) => {
            console.log('el', el);
            if (typeof el == 'number') {
              arr.push({ first_name: { [Op.iLike]: `%` + `${el}` + `%` } }, { last_name: { [Op.iLike]: `%` + `${el}` + `%` } });
            } else {
              arr.push({ first_name: { [Op.iLike]: `%` + `%${el}%` + `%` } }, { last_name: { [Op.iLike]: `%` + `%${el}%` + `%` } });
            }
          });

          filterCandidate = {
            [Op.or]: arr
          };
        }
      }

      let filterPosition = {}
      if (position) {
        if (position != undefined) {
          filterPosition = { position: { [Op.iLike]: `%${position}%` } }
        }
      }

      if (filterCandidate.length > 0 || filterPosition.length > 0) {
        search = { where: { ...(filterCandidate || []), ...(filterPosition || []) } }
      }

      console.log('search', search);
      console.log('candidate', filterCandidate);
      console.log('position', filterPosition);

      if (order_by && order_direction) {
        order.push([order_by, order_direction]);
      }

      const transform = (records) => {
        return records.map(record => {
          return {
            id: record.id,
            name: record.name,
            date: moment(record.createdAt).format('D-M-Y H:mm A')
          }
        });
      }
      const products = await paginate(Candidate, page, limit, search, order, include);

      return res.json({
        success: true,
        // message: '',
        data: products
      })
    
  } catch (error) {
    console.log('Failed to fetch products', error);
    return res.status(500).send({
      success: false,
      message: 'Failed to fetch products'
    })
  }
});

paginate.js

const paginate = async (model, pageSize, pageLimit, search = {}, order = [], include, transform, attributes, settings) => {
  try {
    const limit = parseInt(pageLimit, 10) || 10;
    const page = parseInt(pageSize, 10) || 1;

    // create an options object
    let options = {
      offset: getOffset(page, limit),
      limit: limit,
      distinct: true,
      include: include,
    };

    // check if the search object is empty
    if (Object.keys(search).length) {
      options = { ...options, ...search };
    }

    if (attributes && attributes.length) {
      options['attributes'] = attributes;
    }

    if (order && order.length) {
      options['order'] = order;
    }

    let data = await model.findAndCountAll(options);
  
    if (transform && typeof transform === 'function') {
        data = transform(data.rows);
    }


      return {
        previousPage: getPreviousPage(page),
        currentPage: page,
        nextPage: getNextPage(page, limit, count),
        total: count,
        limit: limit,
        data: data.rows
      }
    
  } catch (error) {
    console.log(error);
  }
}

const getOffset = (page, limit) => {
  return (page * limit) - limit;
}

const getNextPage = (page, limit, total) => {
  if ((total / limit) > page) {
    return page + 1;
  }

  return null
}

const getPreviousPage = (page) => {
  if (page <= 1) {
    return null
  }
  return page - 1;
}

module.exports = paginate;

第一个问题-在查询包含的模型时,有时它会以错误的计数响应,而且速度很慢
请帮我解决这个问题
谢谢
我试过单独使用:真,必填:true和etc -我只得到包含的空数组

yqhsw0fo

yqhsw0fo1#

在你的sequelize请求中有很多json查询。Mysql不能直接创建索引你的json字段。你可以创建一个引用你的查询属性,如下所示,然后你可以添加一个引用列的索引。并改变你的引用列作为创建的引用。你也可以创建mysql索引另一个请求列。

ALTER TABLE [table_name] ADD COLUMN email VARCHAR(255)
  GENERATED ALWAYS as (properties->>"$.[jsonObjectName].[jsonProperty]");

ALTER TABLE [table_name] ADD INDEX email ([jsonProperty]) USING BTREE;

相关问题