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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:10:45+00:00 2026-05-13T23:10:45+00:00

I have a innoDB table which records online users. It gets updated on every

  • 0

I have a innoDB table which records online users. It gets updated on every page refresh by a user to keep track of which pages they are on and their last access date to the site. I then have a cron that runs every 15 minutes to DELETE old records.

I got a ‘Deadlock found when trying to get lock; try restarting transaction’ for about 5 minutes last night and it appears to be when running INSERTs into this table. Can someone suggest how to avoid this error?

=== EDIT ===

Here are the queries that are running:

First Visit to site:

INSERT INTO onlineusers SET
ip = 123.456.789.123,
datetime = now(),
userid = 321,
page = '/thispage',
area = 'thisarea',
type = 3

On each page refresh:

UPDATE onlineusers SET
ips = 123.456.789.123,
datetime = now(),
userid = 321,
page = '/thispage',
area = 'thisarea',
type = 3
WHERE id = 888

Cron every 15 minutes:

DELETE FROM onlineusers WHERE datetime <= now() - INTERVAL 900 SECOND

It then does some counts to log some stats (ie: members online, visitors online).

  • 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-13T23:10:45+00:00Added an answer on May 13, 2026 at 11:10 pm

    One easy trick that can help with most deadlocks is sorting the operations in a specific order.

    You get a deadlock when two transactions are trying to lock two locks at opposite orders, ie:

    • connection 1: locks key(1), locks key(2);
    • connection 2: locks key(2), locks key(1);

    If both run at the same time, connection 1 will lock key(1), connection 2 will lock key(2) and each connection will wait for the other to release the key -> deadlock.

    Now, if you changed your queries such that the connections would lock the keys at the same order, ie:

    • connection 1: locks key(1), locks key(2);
    • connection 2: locks key(1), locks key(2);

    it will be impossible to get a deadlock.

    So this is what I suggest:

    1. Make sure you have no other queries that lock access more than one key at a time except for the delete statement. if you do (and I suspect you do), order their WHERE in (k1,k2,..kn) in ascending order.

    2. Fix your delete statement to work in ascending order:

    Change

    DELETE FROM onlineusers 
    WHERE datetime <= now() - INTERVAL 900 SECOND
    

    To

    DELETE FROM onlineusers 
    WHERE id IN (
        SELECT id FROM onlineusers
        WHERE datetime <= now() - INTERVAL 900 SECOND 
        ORDER BY id
    ) u;
    

    Another thing to keep in mind is that MySQL documentation suggest that in case of a deadlock the client should retry automatically. you can add this logic to your client code. (Say, 3 retries on this particular error before giving up).

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

Sidebar

Related Questions

I have a users table in which there's a column called 'nickname', utf-8 encoded,
I have a PHP script which selects a code from an InnoDB table and
We have a (currently InnoDB) table which contains roughly 500,000 rows. This represents a
I have a table in my MySQL ( InnoDB ) full with user items.
I have a big MySQL InnoDB table (about 1 milion records, increase by 300K
I have table named as contacts which has nearly 1.2 million records we use
I have an existing InnoDB table which already has foreign keys pointing to different
We have a large InnoDB table which is gigabytes in size. We're now clearing
I have an InnoDB table in MySQL which used to contain about 600k rows.
I have an InnoDB table of about 500 000 rows which is growing and

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.