Using a database table as a queue

前端 未结 9 2226
既然无缘
既然无缘 2020-12-02 07:45

I want to use a database table as a queue. I want to insert in it and take elements from it in the inserted order (FIFO). My main consideration is performance because I have

9条回答
  •  日久生厌
    2020-12-02 08:19

    I had the same general question of "how do I turn a table into a queue" and couldn't find the answer I wanted anywhere.

    Here is what I came up with for Node/SQLite/better-sqlite3. Basically just modify the inner WHERE and ORDER BY clauses for your use case.

    module.exports.pickBatchInstructions = (db, batchSize) => {
      const buf = crypto.randomBytes(8); // Create a unique batch identifier
    
      const q_pickBatch = `
        UPDATE
          instructions
        SET
          status = '${status.INSTRUCTION_INPROGRESS}',  
          run_id = '${buf.toString("hex")}',
          mdate = datetime(datetime(), 'localtime')
        WHERE
          id IN (SELECT id 
            FROM instructions 
            WHERE 
              status is not '${status.INSTRUCTION_COMPLETE}'
              and run_id is null
            ORDER BY
              length(targetpath), id
            LIMIT ${batchSize});
      `;
      db.run(q_pickBatch); // Change the status and set the run id
    
      const q_getInstructions = `
        SELECT
          *
        FROM
          instructions
        WHERE
          run_id = '${buf.toString("hex")}'
      `;
      const rows = db.all(q_getInstructions); // Get all rows with this batch id
    
      return rows;
    };
    

提交回复
热议问题