Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9063335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:58:20+00:00 2026-06-16T15:58:20+00:00

We have a system that has a database based queue for processing items in

  • 0

We have a system that has a database based queue for processing items in threads instead of real time. It’s currently implemented in Mybatis calling a this stored procedure in mysql:

DROP PROCEDURE IF EXISTS pop_invoice_queue;
DELIMITER ;;
CREATE PROCEDURE pop_invoice_queue(IN compId int(11), IN limitRet int(11)) BEGIN

   SELECT LAST_INSERT_ID(id) as value, InvoiceQueue.* FROM InvoiceQueue 
      WHERE companyid = compId 
      AND (lastPopDate is null OR lastPopDate < DATE_SUB(NOW(), INTERVAL 3 MINUTE)) LIMIT limitRet FOR UPDATE;
   UPDATE InvoiceQueue SET lastPopDate=NOW() WHERE id=LAST_INSERT_ID(); 

END;;

DELIMITER ;

The problem is that this pops N items from the queue but only updates the lastPopDate value for the last item popped off the queue. So if we call this stored procedure with limitRet = 5, it will pop five items off the queue and start working on them but only the fifth item will have a lastPopDate set so when the next thread comes and pops off the queue it will get items 1-4 and item 6.

How can we get this to update all N records ‘popped’ off the database?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:58:22+00:00Added an answer on June 16, 2026 at 3:58 pm

    If you are willing to add a BIGINT field to the table via:

    ALTER TABLE InvoiceQueue
    ADD uuid BIGINT NULL DEFAULT NULL,
    INDEX ix_uuid (uuid);
    

    then you can do the update first, and select the records updated, via:

    CREATE PROCEDURE pop_invoice_queue(IN compId int(11), IN limitRet int(11))
    BEGIN
       SET @uuid = UUID_SHORT();
    
       UPDATE InvoiceQueue
       SET    uuid = @uuid,
              lastPopDate = NOW()
       WHERE  companyid = compId
       AND    uuid IS NULL 
       AND    (lastPopDate IS NULL OR lastPopDate < NOW() - INTERVAL 3 MINUTE)
       ORDER BY
              id
       LIMIT  limitRet;
    
       SELECT * 
       FROM   InvoiceQueue 
       WHERE  uuid = @uuid
       FOR    UPDATE;
    END;;
    

    For the UUID_SHORT() function to return unique values, it should be called no more than 16 million times a second per machine. Visit here for more details.

    For performance, you may want to alter the lastPopDate field to be NOT NULL as the OR clause will cause your query to not use an index, even if one is available:

    ALTER TABLE InvoiceQueue
    MODIFY lastPopDate DATETIME NOT NULL DEFAULT '0000-00-00';
    

    Then, if you do not already have one, you could add an index on the companyid/lastPopDate/uuid fields, as follows:

    ALTER TABLE InvoiceQueue
    ADD INDEX ix_company_lastpop (companyid, lastPopDate, uuid);
    

    Then you can remove the OR clause from your UPDATE query:

       UPDATE InvoiceQueue
       SET    uuid = @uuid,
              lastPopDate = NOW()
       WHERE  companyid = compId 
       AND    lastPopDate < NOW() - INTERVAL 3 MINUTE
       ORDER BY
              id
       LIMIT  limitRet;
    

    which will use the index you just created.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a system that supports multiple products. Each product has its own database
We have a system that has some Bash scripts running besides Java code. Since
I have a system that has many components interacting with each other. It occured
I have a messaging system (very basic) that has a table like this: **MESSAGE_ID**
I have a system of comments, and each comment has a button that normally
I have a huge database that has several tables that hold several million records.
I have a class (built by EF from my database) that has a field
I currently have a listing of data that has a randomly generated order listing.
Consider we have a database that has a table, which is a record of
I have a unique situation where I am building a DDD based system that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.