简体   繁体   中英

What's the difference between non-unique key lookup and index range scan?

I don't know what's the difference between non-unique key lookup and index range scan because I think both of them return rows which follow a condition like the operator "=" o "<".

WHERE x = 123   -- with INDEX(x)

will scan all the row or rows with x=123. It performs identically to

WHERE y BETWEEN 22 and 33   -- with any kind of index on y

Both will drill down the BTree of the index to find the first match. Then it will scan forward until the value no longer matches.

However, these work differently:

WHERE x = 123              AND b >= 88
WHERE y BETWEEN 22 and 33  AND b >= 88

The first can take advantage of INDEX(x,b) . It locates the first x=123 and b=88 , then scans until x > 123 .

The second cannot do anything like that. No composite index will really handle both y and b .

"Index range scan" and "table range scan" are similar, but applied to different BTrees. The "table" is contained in a BTree that is ordered by the PRIMARY KEY so, arguably, a "table range scan" is an "index range scan"

A "unique key lookup" and a "non-unique key lookup" differ in that the unique one can stop after finding 1 row (or no rows), where as the non-unique one must keep going until a non-matching value is encountered (or LIMIT is reached).

I think for the most part it is semantics. The two are very similar.

An index range scan starts with a lookup. In some cases, that is sufficient. For instance;

  • To find a maximum or minimum value.
  • If the index is known to be unique and the comparison is equality (or is null ).

More generally, the engine continues scanning the index either until the key value changes (for an = comparison) or until the end of the range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM