Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 201309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:11:16+00:00 2026-05-11T17:11:16+00:00

NOTE: the original question is moot but scan to the bottom for something relevant.

  • 0

NOTE: the original question is moot but scan to the bottom for something relevant.

I have a query I want to optimize that looks something like this:

select cols from tbl where col = "some run time value" limit 1;

I want to know what keys are being used but whatever I pass to explain, it is able to optimize the where clause to nothing (“Impossible WHERE noticed…”) because I fed it a constant.

  • Is there a way to tell mysql to not do constant optimizations in explain?
  • Am I missing something?
  • Is there a better way to get the info I need?

Edit: EXPLAIN seems to be giving me the query plan that will result from constant values. As the query is part of a stored procedure (and IIRC query plans in spocs are generated before they are called) this does me no good because the value are not constant. What I want is to find out what query plan the optimizer will generate when it doesn’t known what the actual value will be.

Am I missing soemthing?

Edit2: Asking around elsewhere, it seems that MySQL always regenerates query plans unless you go out of your way to make it re-use them. Even in stored procedures. From this it would seem that my question is moot.

However that doesn’t make what I really wanted to know moot: How do you optimize a query that contains values that are constant within any specific query but where I, the programmer, don’t known in advance what value will be used? — For example say my client side code is generating a query with a number in it’s where clause. Some times the number will result in an impossible where clause other times it won’t. How can I use explain to examine how well optimized the query is?

The best approach I’m seeing right off the bat would be to run EXPLAIN on it for the full matrix of exist/non-exist cases. Really that isn’t a very good solution as it would be both hard and error prone to do by hand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:11:16+00:00Added an answer on May 11, 2026 at 5:11 pm

    For example say my client side code is generating a query with a number in it’s where clause.

    Some times the number will result in an impossible where clause other times it won’t.

    How can I use explain to examine how well optimized the query is?

    MySQL builds different query plans for different values of bound parameters.

    In this article you can read the list of when does the MySQL optimizer does what:

        Action                                      When
    
        Query parse                                 PREPARE
        Negation elimination                        PREPARE
        Subquery re-writes                          PREPARE
    
        Nested JOIN simplification                  First EXECUTE
        OUTER->INNER JOIN conversions               First EXECUTE
    
        Partition pruning                           Every EXECUTE
        COUNT/MIN/MAX elimination                   Every EXECUTE
        Constant subexpression removal              Every EXECUTE
        Equality propagation                        Every EXECUTE
        Constant table detection                    Every EXECUTE
        ref access analysis                         Every EXECUTE
        range/index_merge analysis and optimization Every EXECUTE
        Join optimization                           Every EXECUTE
    

    There is one more thing missing in this list.

    MySQL can rebuild a query plan on every JOIN iteration: a such called range checking for each record.

    If you have a composite index on a table:

    CREATE INDEX ix_table2_col1_col2 ON table2 (col1, col2)
    

    and a query like this:

    SELECT  *
    FROM    table1 t1
    JOIN    table2 t2
    ON      t2.col1 = t1.value1
            AND t2.col2 BETWEEN t1.value2_lowerbound AND t2.value2_upperbound
    

    , MySQL will NOT use an index RANGE access from (t1.value1, t1.value2_lowerbound) to (t1.value1, t1.value2_upperbound). Instead, it will use an index REF access on (t1.value) and just filter out the wrong values.

    But if you rewrite the query like this:

    SELECT  *
    FROM    table1 t1
    JOIN    table2 t2
    ON      t2.col1 <= t1.value1
            AND t2.col1 >= t2.value1
            AND t2.col2 BETWEEN t1.value2_lowerbound AND t2.value2_upperbound
    

    , then MySQL will recheck index RANGE access for each record from table1, and decide whether to use RANGE access on the fly.

    You can read about it in these articles in my blog:

    • Selecting timestamps for a time zone – how to use coarse filtering to filter out timestamps without a timezone
    • Emulating SKIP SCAN – how to emulate SKIP SCAN access method in MySQL
    • Analytic functions: optimizing LAG, LEAD, FIRST_VALUE, LAST_VALUE – how to emulate Oracle’s analytic functions in MySQL
    • Advanced row sampling – how to select N records from each group in MySQL

    All these things employ RANGE CHECKING FOR EACH RECORD

    Returning to your question: there is no way to tell which plan will MySQL use for every given constant, since there is no plan before the constant is given.

    Unfortunately, there is no way to force MySQL to use one query plan for every value of a bound parameter.

    You can control the JOIN order and INDEX‘es being chosen by using STRAIGHT_JOIN and FORCE INDEX clauses, but they will not force a certain access path on an index or forbid the IMPOSSIBLE WHERE.

    On the other hand, for all JOIN‘s, MySQL employs only NESTED LOOPS. That means that if you build right JOIN order or choose right indexes, MySQL will probably benefit from all IMPOSSIBLE WHERE‘s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update is below code chunks. Original Question: I have an input field that is
EDIT: Please note that I am appending my original question with my actual question
Note I have completely re-written my original post to better explain the issue I
Note: This question has broadened in scope from previous revisions. I have tried to
NOTE: I have solved the majority of this problem but have run into a
I have a SQL query that I'm currently solving by doing two queries. I
[Note: This question had the original title " C (ish) style union in C#
Note: Full working example now below. Original question follows: I'm having problems using ld's
UPDATED - please read further details below original question I have a select form
Note: In some situations elements with display:table-cell; have space at the top that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.