MySQL Indexing
MySQL Indexing
Dr.Nikorn Rongbutsri
Introduction to MySQL Indexing
In MySQL, indexing is a crucial aspect of database optimization, aiming to
enhance the speed and efficiency of query processing. MySQL supports various
indexing techniques, each designed to address specific use cases and query
patterns. Understanding how indexing works in MySQL is essential for creating
well-performing databases.
Syntax for Creating Indexes in MySQL
Creating indexes in MySQL is a straightforward process using the CREATE
INDEX statement or incorporating index definitions within the CREATE TABLE
statement. 3 ways to create index in MySQL:
1. CREATE INDEX
CREATE INDEX Indexname ON Tablename (indexcolumn1, indexcolumn2,
….);
2. CREATE TABLE
CREATE TABLE Tablename (column1 CHAR (30) NOT NULL, INDEX
(column2));
3. ALTER TABLE
ALTER TABLE Tablename ADD INDEX (column1, column2);
Index Types
● B-tree
● Unique
● Primary key
● Full text
● Spatial
● Hash* (not supported by MySQL)
World Database
sample structure
B-Tree Index
● Default indexing mechanism in MySQL.
● Suitable for most scenarios, including exact matches and range queries.
● Automatically created for primary key and unique key columns.
Examples
Examples
Examples
ALTER TABLE country ADD SPATIAL INDEX idx_Geometry_Spatial
(Geometry);
Well-suited for Equality Searches: Ideal for scenarios where equality searches
are the primary focus, such as finding exact matches.
Hash index disadvantages
No Range Queries: Less effective for range queries or searches that require
ordering of results.
Hash Collisions: Susceptible to hash collisions, where different keys produce the
same hash code, potentially impacting performance.
Data Type Limitations: May not be suitable for variable-length strings or data
types with varying lengths.
Order Preservation: Lacks order preservation, making it unsuitable for queries
that depend on the order of keys.
Limited Database Support: Not universally supported across all database
systems; some databases primarily use B-tree indexes.
Index Description Use Case Pros Cons
Type
B-Tree Standard index structure in most General indexing, equality and Efficient for ordered data May not perform well for certain types
RDBMS range queries of queries
Unique Enforces uniqueness on indexed Enforcing unique constraints Guarantees uniqueness May slow down INSERTs and
columns UPDATEs
Primary Unique identifier for each record Identifying records Enforces uniqueness and NOT NULL Imposes a structure on the data
Key in a table constraints
Full Text Indexing for full-text search Searching within large text fields Enables efficient searching for words Typically resource-intensive
capabilities and phrases
Spatial Indexing for spatial/geometric Geographical and geometric data Efficient for spatial queries Requires specialized spatial data types
data types
Hash Uses a hash function to index Equality searches Uniform distribution of keys Not suitable for range queries or
data sorting operations
Characteristic B-tree Index Hash Index
Structure Tree structure with nodes and leaf Direct mapping using a hash function.
nodes.
Search Efficient for equality and range queries. Efficient for equality searches, less effective for
Operations range queries.
Order Preserves order of keys, suitable for Does not preserve order, less suitable for
Preservation sorting. sorting.
Database Widely supported in various databases Support varies among databases; not
Support (e.g., MySQL, PostgreSQL, Oracle). universally available.
Data Types Applicable to a variety of data types. Applicable to data types that can be hashed
(e.g., integers, fixed-length strings).
Performance Generally provides good performance Provides constant-time lookups for exact
for a broad range of query types. matches, efficient for specific use cases.
Use Cases Well-suited for mixed equality and Well-suited for scenarios where exact match
range queries. searches are prevalent.
Best Practices and Considerations
1. Choose Appropriate Columns:
● Identify columns frequently used in WHERE clauses or JOIN conditions.
● Consider the cardinality of columns when deciding on index creation.
2. Avoid Overindexing:
● While indexes enhance read performance, they can impact write performance.
● Overindexing (creating too many indexes) can lead to increased maintenance overhead.