0

I'm currently studying DB index. In the MySQL classicmodels db, I don't know why does this query use full index scan.

    select salesRepEmployeeNumber, count(*) 
    from customers 
    where creditLimit > 100000
    group by salesRepEmployeeNumber;

The customers table has clustered index on customerNumber and Non-clustered index on salesRepEmployeeNumber. From what I learned, there is a <key, key of clustered index> in the data entry of the non-clustered tree. Isn't the creditLimit column that exists in the WHERE clause information that can only be known by accessing the data record? If so, shouldn't MySQL query optimizer use full table scan not full index scan?B tree clustered index B tree Non-clustered index Query Execution Plan

2
  • Is this a typo? salesRepEmployeeNumcustomersber Commented Oct 17, 2020 at 15:48
  • Thanks. It is typo, I just fixed it. Commented Oct 17, 2020 at 21:53

1 Answer 1

1

If you have an index defined on (credit limit, salesRepEmployeeNumber), this query can be fulfilled from the index alone without touching the data pages.

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

4 Comments

@ChaeunGong: you should create the index that this answers suggests, so your query executes quickly.
@GMB: Thanks your comment. I know that. But I want to know why does the query optimizer use full index scan not full table scan?
I'm sorry. When I checked the post, the content was strange, so I modified the post. My point is why the query optimizer uses 'full index scan' when there are no indexes associated with creditLimits.
Can you provide output of SHOW CREATE TABLE customers; ?

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.