Question

The last few days, at random times my website has become very slow. I started to investigate the best I could. I saw that the MySQL process was using 85 - 95% of the available memory of my server (should I upgrade my memory also?).

I checked my MySQL process log, and I noticed a huge list of queries with:

Waiting for table level lock

But what I also noticed, what that ALL of these queries with "table level lock", was only queries which had something to do with my table called users.

I have 20 other tables, with constant queries, but I don't see them on the list.. So I guess the problem is with the users table?

I want to know how I can improve the table, and eventually remove the table level lock?

I also ran this:

SHOW VARIABLES LIKE 'query_cache%';

Which resulted in this:

query_cache_limit
1048576
query_cache_min_res_unit
4096
query_cache_size
33554432
query_cache_type
ON
query_cache_wlock_invalidate
OFF

Please let me know what I can do to improve my database/mysql.

This is a list of the processes:

   | 228 | db_user | localhost | db_db| Query          |    5 | Waiting for table level lock | SELECT count(*) FROM users WHERE createtime>'1396411200' OR createtime='1396411200'                  |
    | 229 | db_user | localhost | db_db| Query          |    4 | Waiting for table level lock | UPDATE users SET upline_clicks=upline_clicks+'1', upline_earnings=upline_earnings+'0.0000' WHERE use |
| 203 | db_user | localhost | db_db| Query          |    6 | Waiting for table level lock | SELECT SUM(cashedout) FROM users                                                                     |
| 204 | db_user | localhost | db_db| Query          |    4 | Waiting for table level lock | UPDATE users SET upline_clicks=upline_clicks+'1', upline_earnings=upline_earnings+'0.0000' WHERE use |
| 205 | db_user | localhost | db_db| Query          |    1 | Waiting for table level lock | SELECT * FROM users WHERE id='12055'                                                                 |
| 206 | db_user | localhost | db_db| Query          |    2 | Waiting for table level lock | SELECT * FROM users WHERE id='22530'                                                                 
| 197 | db_user | localhost | db_db| Query          |    3 | Waiting for table level lock | SELECT * FROM `users` WHERE `username` = 'ptc4life123' LIMIT 1                                       |
| 200 | db_user | localhost | db_db| Query          |    3 | Waiting for table level lock | UPDATE users SET upline_clicks=upline_clicks+'1', upline_earnings=upline_earnings+'0.0050' WHERE use |

This is basically what all the locked processes looks like.

Was it helpful?

Solution

During one of the slow periods, run this command:

show processlist;

This will show you the actual SQL commands that are running, and the places to look for in terms of adding indexes. Post the longest running SQL if possible.

Adding an index will look like this:

 ALTER TABLE MYTABLE ADD INDEX idx_columnname (COLUMN_NAME ASC) ;

But you want to be careful not to do that during a production period. At first glance, you want to do that on ID and username columns in the user table.

OTHER TIPS

Check the storage engine of the table .Change to Innodb if possible as it cause only row level locking. The queries which are running may be forcing a table lock even if you are using innodb tables if you are not using the index .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top