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 596151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:08:27+00:00 2026-05-13T16:08:27+00:00

I want to use a database table as a queue. I want to insert

  • 0

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 thousands of these transactions each second. So I want to use a SQL query that gives me the first element without searching the whole table. I do not remove a row when I read it.
Does SELECT TOP 1 ….. help here?
Should I use any special indexes?

  • 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-05-13T16:08:27+00:00Added an answer on May 13, 2026 at 4:08 pm

    I’d use an IDENTITY field as the primary key to provide the uniquely incrementing ID for each queued item, and stick a clustered index on it. This would represent the order in which the items were queued.

    To keep the items in the queue table while you process them, you’d need a “status” field to indicate the current status of a particular item (e.g. 0=waiting, 1=being processed, 2=processed). This is needed to prevent an item be processed twice.

    When processing items in the queue, you’d need to find the next item in the table NOT currently being processed. This would need to be in such a way so as to prevent multiple processes picking up the same item to process at the same time as demonstrated below. Note the table hints UPDLOCK and READPAST which you should be aware of when implementing queues.

    e.g. within a sproc, something like this:

    DECLARE @NextID INTEGER
    
    BEGIN TRANSACTION
    
    -- Find the next queued item that is waiting to be processed
    SELECT TOP 1 @NextID = ID
    FROM MyQueueTable WITH (UPDLOCK, READPAST)
    WHERE StateField = 0
    ORDER BY ID ASC
    
    -- if we've found one, mark it as being processed
    IF @NextId IS NOT NULL
        UPDATE MyQueueTable SET Status = 1 WHERE ID = @NextId
    
    COMMIT TRANSACTION
    
    -- If we've got an item from the queue, return to whatever is going to process it
    IF @NextId IS NOT NULL
        SELECT * FROM MyQueueTable WHERE ID = @NextID
    

    If processing an item fails, do you want to be able to try it again later? If so, you’ll need to either reset the status back to 0 or something. That will require more thought.

    Alternatively, don’t use a database table as a queue, but something like MSMQ – just thought I’d throw that in the mix!

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

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The main difference is that InnoDB supports transactions, but MyISAM… May 16, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer You can use libraries like Apache AXIS or Apache CXF,… May 16, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer You can't change the page lifecycle. What you can do… May 16, 2026 at 2:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

ok here is the situation , I want to use a login form for
I have a fairly complicated join query that I use with my database. Upon
Hi currently have the problem, that I want to insert values in a postgres
I'm currently designing an application which I will ultimately want to move to Windows
We have a system that allows users interfacing data into the database to set
I'm developing a web service whose methods will be called from a dynamic banner
Basically, i want my site to aggregate a lot of rss feeds and store
I'm using the succinctly named googlechartseasyphpclass script to generate google charts from numbers I'm
I have an OLTP database, and am currently creating a data warehouse. There is
I have two servers, the first one with SQL Server 2005 and the second

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.