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

  • Home
  • SEARCH
  • 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 9157107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:55:57+00:00 2026-06-17T12:55:57+00:00

I have two mysql tables: tbl_jobs ( id INTEGER PRIMARY KEY, status ENUM(runnable, running,

  • 0

I have two mysql tables:

tbl_jobs (
    id INTEGER PRIMARY KEY,
    status ENUM("runnable", "running", "finished"),
    server_id INTEGER
)

tbl_servers (
    is INTEGER PRIMARY KEY,
    name VARCHAR(50)
)

I have a lot of clients that connect, take one job for a particular server that is runnable and set it to running.

Here is how I currently do it (in stored procedure):

DELIMITER $$
CREATE PROCEDURE GET_JOB(serverName VARCHAR(50))
BEGIN
    DECLARE jobId INTEGER DEFAULT NULL;
    SELECT id FROM tbl_jobs j INNER JOIN tbl_servers s on j.server_id=s.id
              WHERE j.state='runnable' AND s.server_name=serverName
              ORDER BY job_id ASC LIMIT 1
              INTO jobId FOR UPDATE;
    IF IFNULL(jobId, 0) = 0 THEN
        SELECT 0;
    END IF;
    UPDATE tbl_jobs SET state='running' WHERE job_id=jobId;
    SELECT jobId;
END $$

That works just fine, but when number of concurrent clients become large (hundreds), I see that there is a big lock congestion going on tbl_servers table. I understand that FOR UPDATE statement locks all of the tables, but I’m actually using tbl_servers as read-only table.

Question: How to avoid lock congestion (i.e. locking at all) on tbl_servers?

The one thing I can think of is to separate my query into two – first convert server name to id and then query just tbl_jobs, but in my real application one server name can have many ids (I know it sounds odd, but I’ve just simplified my application here for illustration). So the second query on tbl_jobs would require prepared statement.

I’m sure there is a more elegant solution.

The system:

  • MySQL 5.1.67 on Amazon RDS
  • All tables are InnoDb
  • I do have an index on tbl_jobs.server_id
  • 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-17T12:55:59+00:00Added an answer on June 17, 2026 at 12:55 pm

    This should avoid the locking on table tbl_servers

    DELIMITER $$
    CREATE PROCEDURE GET_JOB(serverName VARCHAR(50))
    BEGIN
        DECLARE jobId INTEGER DEFAULT NULL;
    
        -- get the server id's
        CREATE TEMPORARY TABLE tmp_srvID 
        SELECT id
        INTO srvID 
        FROM tbl_servers
        WHERE name = serverName;
    
        SELECT id 
        INTO jobId 
        FROM tbl_jobs 
        WHERE state='runnable'
    
          AND server_id IN ( SELECT id FROM tmp_srvID ) 
    
        ORDER BY job_id ASC 
        LIMIT 1
        FOR UPDATE;
    
        DROP TABLE tmp_srvID;
    
        IF IFNULL(jobId, 0) = 0 THEN
            SELECT 0;
        ELSE
            UPDATE tbl_jobs SET state='running' WHERE job_id=jobId;
            SELECT jobId;
        END IF;
    END $$
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two MySQL tables, tblloanRegistry LoanID EMPNumber Date Amount Status 1 1111 2012-10-01
I have two mysql tables both with primary keys (the records 'id' field). The
I have two MySQL tables like this: User Id (PK, auto-increment integer) Subscriber Id
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
I have two MySQL tables, j and t, with a third normalising table, jt.
I have two MySQL tables structed as follows; advert +-------------------+--------------+ | Field | Type
I have two MySQL tables: attributes (attributeid, name) productsattributes (productid, attributeid, displayvalue) The required
i have two mysql tables tableA colA1 colA2 1 whatever 2 whatever 3 whatever
I have two MySQL tables, and I want to find and replace text strings

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.