Search code examples
mysqllockingmysql-slow-query-log

Mysql Lock times in slow query log


I have an application that has been running fine for quite awhile, but recently a couple of items have started popping up in the slow query log. All the queries are complex and ugly multi join select statements that could use refactoring. I believe all of them have blobs, meaning they get written to disk. The part that gets me curious is why some of them have a lock time associated with them. None of the queries have any specific locking protocols set by the application. As far as I know, by default you can read against locks unless explicitly specified.

so my question: What scenarios would cause a select statement to have to wait for a lock (and thereby be reported in the slow query log)? Assume both INNODB and MYISAM environments.

Could the disk interaction be listed as some sort of lock time? If yes, is there documentation around that says this?

thanks in advance.


Solution

  • MyISAM will give you concurrency problems, an entire table is completely locked when an insert is in progress.

    InnoDB should have no problems with reads, even while a write/transaction is in progress due to it's MVCC.

    However, just because a query is showing up in the slow-query log doesn't mean the query is slow - how many seconds, how many records are being examined?

    Put "EXPLAIN" in front of the query to get a breakdown of the examinations going on for the query.

    here's a good resource for learning about EXPLAIN (outside of the excellent MySQL documentation about it)