20

How do analyse and use EXPLAIN for my stored procedure calls ? I need to optimize the query time, however seems like there is no where i can do a EXPLAIN call proc_name() ?

3 Answers 3

31

You can try

set profiling=1;
call proc_name();
show profiles;
Sign up to request clarification or add additional context in comments.

1 Comment

That does not reveal the execution plan, but it does show how long each query in the procedure took.
11

at present you can't explain stored procedures in mysql - but you could do something like this:

drop procedure if exists get_user;
delimiter #
create procedure get_user
(
in p_user_id int unsigned,
in p_explain tinyint unsigned
)
begin
  if (p_explain) then
    explain select * from users where user_id = p_user_id;
  end if;
  select * from users where user_id = p_user_id;
end#

delimiter ;

call get_user(1,1);

2 Comments

What does this do?
It does explains the querry inside the procedure if you call it
3

Prior to MySQL 5.7:

EXPLAIN worked only on SELECT statements, except when you use EXPLAIN tablename which is an alias of DESCRIBE tablename

after 5.7:

EXPLAIN works with SELECT DELETE INSERT REPLACE UPDATE statements

4 Comments

EXPLAIN works with SELECT DELETE INSERT REPLACE UPDATE statements, as stated in MySQL 5.7 doc
@Xenos did you... see the date of my answer?
Yes, that's why it's worth a mention, so others readers are up-to-date and not sticking to old obsolete answers
@Xenos in a case like this, it's better to edit the answer, as long as it's in line with the original answer, and is no longer a dupe. If it's a dupe-after-edit you can flag to remove to avoid confusion. If you didn't have the rep at the time to edit, then agree that commenting is a good way to help others until the edit is approved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.