The insert is going to acquire a lock on the page it is inserting into, and then attempt to acquire the key lock. The delete is going to hold a key-range lock for datetime, to prevent rows matching its where clause from being added in the middle of the transaction, and as it finds rows to delete it will attempt to acquire a lock on each page it is modifying. Theory about the deadlocks: I don't have a lot of background in MySQL but here goes. ![]() This avoids the dependency on the cron job and allows you to reschedule it to run less often. Select userid from onlineusers where datetime <= now - interval 900 second ĭelete from onlineusers where userid in (select userid from deletetemp) īreaking it up like this is less efficient but it avoids the need to hold a key-range lock during the delete.Īlso, modify your select queries to add a where clause excluding rows older than 900 seconds. You might try having that delete job operate by first inserting the key of each row to be deleted into a temp table like this pseudocode create temporary table deletetemp (userid int) If table lock happen, the lock contention is higher and the likelihood of deadlock increases. Be sure then to always have index on the foreign keys (and of course primary keys), otherwise it could result in a table lock instead of a row lock. It might result in other lock being acquired than the row that is modified. To do so, the database needs to check the foreign keys in the related tables. When a row is inserted/update/delete, the database needs to check the relational constraints, that is, make sure the relations are consistent. always table A first, then table B).Īnother reason for deadlock in database can be missing indexes. Generally speaking, try to acquire lock always in the same order even in different transaction (e.g. To avoid deadlock, you must then make sure that concurrent transactions don't update row in an order that could result in a deadlock. Each time you insert/update/or delete a row, a lock is acquired. There are numerous questions and answers about deadlocks. ![]() (Say, 3 retries on this particular error before giving up).ĭeadlock happen when two transactions wait on each other to acquire a lock. you can add this logic to your client code. WHERE datetime <= now() - INTERVAL 900 SECOND order by id) u Īnother thing to keep in mind is that mysql documentation suggest that in case of a deadlock the client should retry automatically. To DELETE FROM onlineusers WHERE id IN (SELECT id FROM onlineusers if you do (and I suspect you do), order their WHERE in (k1,k2.kn) in ascending order.įix your delete statement to work in ascending order:Ĭhange DELETE FROM onlineusers WHERE datetime <= now() - INTERVAL 900 SECOND Make sure you have no other queries that lock access more than one key at a time except for the delete statement. connection 2: locks key( 1), locks key( 2).Now, if you changed your queries such that the connections would lock the keys at the same order, ie: 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. connection 2: locks key(2), locks key(1).connection 1: locks key(1), locks key(2).You get a deadlock when two transactions are trying to lock two locks at opposite orders, ie: Select `COUNT` from `information_schema`.One easy trick that can help with most deadlocks is sorting the operations in a specific order. Set global innodb_monitor_enable=`p_name` For example: drop procedure if exists `track_metric` Ĭreate procedure `track_metric`(`p_name` varchar(193) charset utf8)ĭeclare `v_count` bigint unsigned default 0 ĭeclare `v_old_count` bigint unsigned default 0 We could also create some stored routine that checks the INNODB_METRICS table every few seconds and prints the value when it changed. To see a once-off answer, you can run this query: mysql> select * from information_schema.innodb_metrics where name='lock_deadlocks'\G So, we’ll use standard SQL queries instead. However, that is not useful for monitoring via a program or script. To log all deadlock information to the mysql error log, you can enable the global variable “ innodb_print_all_deadlocks”. Question: How to Monitor Number of InnoDB Deadlocks?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |