Why can 'query end' thread state take long time?

Hi,
Threads can leave in ‘query end’ state for seconds.

Percona 5.5.16
Centos 6

That could happen when the system stalls. The cause could be a checkpoint flushing storm, or contention on query cache, or other reasons.

For innodb flushing storm, you can read about it from these blog posts :
[url]http://www.mysqlperformanceblog.com/2011/04/04/innodb-flushing-theory-and-solutions/[/url]
[url]http://www.mysqlperformanceblog.com/2011/09/18/disaster-mysql-5-5-flushing/[/url]

Percona 5.5 has improvements in stabilize IO and flushing activity. The related variables are talked about here:
[url]Percona Server for MySQL 8.0 - Documentation — Percona Server 8.0 Documentation

For query cache, you may check global status Qcache_% to see the usage info, and use Qcache_hits / (Com_select + Qcache_hits) x 100 to check efficiency. For most write heavy workload, it is better to disable query cache for improved overall performance.