MySQL 常用命令统计
analyze
table
tb_name
ySQL执行命令delete语句时,如果包括where条件,并不会真正的把数据从表中删除,而是将数据转换成了碎片,通过下面的命令可以查看表中的碎片数量和索引等信息:
mysql > show table status like 'table_name'
查询结果中:
Index_length 代表索引的数量
Data_free 代表碎片数量
然后执行下面命令进行优化整理:
mysql > optimize table table_name
如果之前的碎片数量多,执行时间可能会久一点,执行结束后出现下面框内的数据则优化成功。
再通过之前的查看命令来看执行效果:
mysql > show table status like 'table_name'
注意事项:
由于命令optimize会进行锁表操作,所以进行优化时要避开表数据操作时间,避免影响正常业务的进行。
SELECT * FROM information_schema.INNODB_TRX
这个只能查询此刻正在进行中的事务,已经完成的是查不到的
针对mysql 5.6,查看更具体的信息:SELECT a.trx_id, a.trx_state, a.trx_started, a.trx_query, b.ID, b.USER, b.DB, b.COMMAND, b.TIME, b.STATE, b.INFO, c.PROCESSLIST_USER, c.PROCESSLIST_HOST, c.PROCESSLIST_DB, d.SQL_TEXT FROM information_schema.INNODB_TRX a LEFT JOIN information_schema.PROCESSLIST b ON a.trx_mysql_thread_id = b.id AND b.COMMAND = 'Sleep' LEFT JOIN PERFORMANCE_SCHEMA.threads c ON b.id = c.PROCESSLIST_ID LEFT JOIN PERFORMANCE_SCHEMA.events_statements_current d ON d.THREAD_ID = c.THREAD_ID;
针对mysql 5.5,查看更具体的信息:SELECT a.trx_id, a.trx_state, a.trx_started, a.trx_query, b.ID, b. USER, b. HOST, b.DB, b.COMMAND, b.TIME, b.STATE, b.INFO FROM information_schema.INNODB_TRX a LEFT JOIN information_schema.PROCESSLIST b ON a.trx_mysql_thread_id = b.id WHERE b.COMMAND = 'Sleep';
查看事务等待情况:SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, r.trx_query waiting_query, b.trx_id blocking_trx_id, b.trx_mysql_thread_id blocking_thread, b.trx_query blocking_query FROM information_schema.innodb_lock_waits w INNER JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id INNER JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id;