mysql查询计划
我有一个选择语句的列表,我希望找出他们的查询计划。 有没有更简单的方法,而不必每个解释前缀?
在Sybase中,你可以通过设置一个标志来做到这一点 - 只是想知道是否有相同的标志?
您可以滥用这个slow query log
:
设置long_query_time = 0
,这将强制MySQL在慢查询日志中记录所有查询。
现在设置您希望日志出现的位置,请参阅:http://dev.mysql.com/doc/refman/5.1/en/log-destinations.html请确保您让慢查询日志将查询放入表中,这是5.1+中的默认值
现在编写一个存储过程,如下所示:
DELIMITER $$
CREATE PROCEDURE run_explain_on__the_slow_query_log(IN which_db VARCHAR(512))
BEGIN
DECLARE sql VARCHAR(10000);
DECLARE done INTEGER DEFAULT 0;
DECLARE cursor1 CURSOR FOR SELECT sql_text FROM mysql.slow_log
WHERE sql_text LIKE 'SELECT%'
AND (db = which_db OR which_db IS NULL);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursor 1;
WHILE NOT (done) DO
FETCH cursor1 INTO sql;
SET sql = CONCAT('EXPLAIN EXTENDED ',sql);
PREPARE stmt FROM sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END WHILE;
END $$
DELIMITER ;
您可能需要稍微调整一下,这里是slow_log
表的create语句。
CREATE TABLE mysql.slow_log(
start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
user_host MEDIUMTEXT NOT NULL,
query_time TIME NOT NULL,
lock_time TIME NOT NULL,
rows_sent INT(11) NOT NULL,
rows_examined INT(11) NOT NULL,
db VARCHAR(512) NOT NULL,
last_insert_id INT(11) NOT NULL,
insert_id INT(11) NOT NULL,
server_id INT(10) UNSIGNED NOT NULL,
sql_text MEDIUMTEXT NOT NULL
)
ENGINE = CSV
CHARACTER SET utf8
COLLATE utf8_general_ci
COMMENT = 'Slow log';
链接地址: http://www.djcxy.com/p/9353.html
上一篇: mysql query plans