Mysql indexes: Difference between revisions

From wikinotes
Line 3: Line 3:
= Index Types =
= Index Types =
<blockquote>
<blockquote>
Show the current index using
<syntaxhighlight lang="mysql">
SHOW INDEX FROM foo_table;
</syntaxhighlight>
== BTree ==
== BTree ==
<blockquote>
<blockquote>
Line 34: Line 39:
But not queries that
But not queries that
<syntaxhighlight lang="yaml">
<syntaxhighlight lang="yaml">
- match on only a single key from a multi-key index  
- match on only a single key from a multi-key index
   (since both keys are hashed together)
   (since both keys are hashed together)
- are range bound. (ex. less than 10, betweeen C and F)
- are range bound. (ex. less than 10, betweeen C and F)
   (hash-tables are un-ordered, so range queries are not optimized)  
   (hash-tables are un-ordered, so range queries are not optimized)
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Hash -->
</blockquote><!-- Hash -->
</blockquote><!-- Index Types -->
</blockquote><!-- Index Types -->

Revision as of 20:38, 4 September 2022

Indexes use various datastructures to store data to prevent full scans of all rows in the table.

Index Types

Show the current index using

SHOW INDEX FROM foo_table;

BTree

A binary tree. Default for persisted storage engines.

Optimize Queries that are

- sorted
- match on field prefixes (but not suffixes)
- range bound (less than 10, between C and F)

But not queries

- match on field suffixes

Hash

A hash table. Default for the memory engine.
hash-collisions are accounted for, but each key with the same hash will need to be checked, making the query more expensive.
Indexes have a small memory footprint.

Optimize queries that are

- based on the full value (ex. 'IN, NOT IN, =')

But not queries that

- match on only a single key from a multi-key index
  (since both keys are hashed together)
- are range bound. (ex. less than 10, betweeen C and F)
  (hash-tables are un-ordered, so range queries are not optimized)