0

I have a mysql 8.0 stored procedure which uses a start_date parameter in the logic.

If start_date is "set" (e.g., set @start_date = '2023-01-01', the query executes in a fraction of a second.

However, if I declare a local variable (i.e., declare start_date date default '2023-01-01', or if I hard-code the start_date , the query takes almost 350 seconds.

Why might there be such a dramatic difference in performance? The output is correct and as expected in both cases -- but one just takes an enormous amount of time.

Below is the EXPLAIN output when "setting" the variable:

enter image description here

and the output when "declaring" the local variable is below. Why is the row count so dramatically different?

enter image description here

3
  • 2
    Seems like '2023-0-01' is an invalid date?? Commented Mar 3, 2024 at 19:30
  • @Rick James -- thanks, it was a typo when posting the question. In actuality it was correctly formatted to '20230101' or '2023-01-01'; I'll edit the post. Commented Mar 3, 2024 at 23:01
  • Now we need to see the query. Better yet, let's see the Stored Proc. Commented Mar 4, 2024 at 2:52

1 Answer 1

1
select date('2023-0-01') is null; --> 1

That is, your string is treated as a NULL.

Nulls often work in surprising ways.

If you like help in optimizing the query, let's see it. (The EXPLAIN is not enough.)

Sign up to request clarification or add additional context in comments.

1 Comment

Need to see the query, stored procedure and TEXT results of SHOW CREATE TABLE tbl_name' for each table involved, please.

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.