[Solved] Lock wait timeout exceeded with one query running
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

Lock wait timeout exceeded with one query running

I run the following query and it is the only query running on my large (2 vCPU, 7.5 GB RAM, 100GB SSD) RDS hosted database.

FROM books
WHERE book_type = '/type/edition'
AND json LIKE '%"languages":%'
AND json NOT LIKE '%/eng%';

But I get the following error.

Error Code: 1205. Lock wait timeout exceeded; try restarting transaction

I increased the timeout to 1200 seconds using SET innodb_lock_wait_timeout = 1200;.

However, I get that same error. There are no other queries running on the database, it's newly created and not in production. Here is the result of show processlist:

| 1 | rdsadmin |                     localhost:37959                      |             | Sleep | 10  |          |                                                                                                      |
| 5 | website  | host109-156-119-150.range109-156.btcentralplus.com:57923 | openlibrary | Sleep | 606 |          |                                                                                                      |
| 6 | website  | host109-156-119-150.range109-156.btcentralplus.com:57924 | openlibrary | Query | 599 | updating | DELETE FROM books WHERE book_type = '/type/edition' AND json LIKE '%"languages":%' AND json NOT LIKE |
| 8 | website  | host109-156-119-150.range109-156.btcentralplus.com:58021 | openlibrary | Sleep | 145 |          |                                                                                                      |
| 9 | website  | host109-156-119-150.range109-156.btcentralplus.com:58022 | openlibrary | Query |   0 | init     | show processlist                                                                                     |

Here is the schema for this table.

CREATE TABLE `books` (
  `book_type` varchar(50) DEFAULT NULL,
  `book_key` varchar(50) DEFAULT NULL,
  `revision` tinyint(4) DEFAULT NULL,
  `last_modified` varchar(50) DEFAULT NULL,
  `json` text,
  PRIMARY KEY (`id`),
  KEY `book_type` (`book_type`),
  KEY `book_key` (`book_key`),
  KEY `revision` (`revision`)

Please note, this table has about 100 million rows and contains 51GB of data.

Why am I getting a lock wait timeout? I thought this error could occur only when you are running multiple queries.

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `books` ADD INDEX `books_idx_book_type` (`book_type`);
The optimized query:
        books.book_type = '/type/edition' 
        AND books.json LIKE '%"languages":%' 
        AND books.json NOT LIKE '%/eng%'

Related Articles

* original question posted on StackOverflow here.