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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:57:08+00:00 2026-06-05T12:57:08+00:00

In MySQL+InnoDB, suppose I have a single table, and two threads which both do

  • 0

In MySQL+InnoDB, suppose I have a single table, and two threads which both do "SELECT … FOR UPDATE". Suppose that both of the SELECT statements end up selecting multiple rows, e.g. both of them end up selecting rows R42 and R99. Is it possible that this will deadlock?

I’m thinking of this situation: the first thread tries to lock R42 then R99, the second thread tries to lock R99 then R42. If I’m unlucky, the two threads will deadlock.

I read in the MySQL Glossary for "deadlock" that

A deadlock can occur when the transactions lock rows in multiple tables (through statements such as UPDATE or SELECT … FOR UPDATE), but in the opposite order. …

To reduce the possibility of deadlocks, … create indexes on the columns used in SELECT … FOR UPDATE and UPDATE … WHERE statements.

This hints that in my situation (single table) I won’t deadlock, maybe because MySQL automatically tries to lock rows in the order of the primary key, but I want to be certain, and I can’t find the proper place in the documentation that tells me exactly what’s going on.

  • 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-05T12:57:09+00:00Added an answer on June 5, 2026 at 12:57 pm

    From MySQL documentation

    InnoDB uses automatic row-level locking. You can get deadlocks even in the case of 
    transactions that just insert or delete a single row. That is because these operations    
    are not really “atomic”; they automatically set locks on the (possibly several) index 
    records of the row inserted or deleted.
    

    http://dev.mysql.com/doc/refman/5.1/en/innodb-deadlocks.html

    So generally, deadlocking is not fatal, you just need to try again, or add the appropriate indexes so that fewer rows are scanned and thus fewer rows are locked.

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

Sidebar

Related Questions

I have two jobs pulling from a mysql table. They both want to get
I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have the following MySQL/InnoDB table. I added a compound index as the primary
I have a MySQL-InnoDB table with 350,000+ rows, containing a couple of things like
I have a MySQL innodb database at 1.9GB, showed by following command. SELECT table_schema
I have a MySQL InnoDB table on a RedHat Enterprise Linux 4 server, and
I have a MySQL InnoDB table with a status column. The status can be
I have a MySQL InnoDB table with 238 columns. 56 of them are TEXT
I'm using MySQL InnoDB. I'd like to update a table and control the sequence
I have a MySQL InnoDB table laid out like so: id (int), run_id (int),

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.