Skip to content

MySQL

sql
sql

-- Add unique index to existing table
sql

-- Add index to existing table
sql

-- For InnoDB and MyISAM tables

-- Use MATCH AGAINST for searching

-- Natural language mode

-- Boolean mode
sql

-- Query using spatial functions
sql

-- Composite index column order matters
-- Index can be used for:
-- - user_id alone
-- - user_id AND status
-- But NOT for status alone (without user_id)
sql
    
    -- Indexes
sql
-- Add simple index

-- Add unique index

-- Add composite index

-- Add full-text index (MySQL 5.6+)

-- Add spatial index
sql

-- Custom index name
sql
-- Show indexes on a table

-- Detailed index information

-- From information_schema
sql
-- Drop index

-- Drop multiple indexes

-- Using ALTER TABLE
sql
-- Use EXPLAIN to see query execution

-- EXPLAIN output columns
-- id: Query identifier
-- select_type: Type of SELECT
-- table: Table being accessed
-- type: How rows are found (ALL, index, range, etc.)
-- possible_keys: Indexes available
-- key: Index actually used
-- key_len: Length of key used
-- rows: Rows examined
-- Extra: Additional information
sql
-- Check index cardinality (unique values)

-- High cardinality: Many unique values (good for indexing)
-- Low cardinality: Few unique values (less effective)
sql
-- Index columns used in WHERE clauses

-- Index columns used in JOINs

-- Index ORDER BY columns

-- Index columns in GROUP BY
sql
-- Consider query patterns
-- Query: WHERE user_id = ? AND status = ?
-- Should have index on (user_id, status)

-- Left-to-right rule
-- Index supports queries starting from left columns

-- Can be used for:
-- WHERE user_id = 5
-- WHERE user_id = 5 AND status = 'completed'
-- But NOT for WHERE status = 'completed' alone
sql
-- Index contains all columns needed by query
-- Query doesn't need to access main table

-- Create covering index

-- This query uses covering index

-- EXPLAIN shows "Using index" in Extra column
sql
-- Index on first N characters of string column
-- Useful for long text columns with repetitive prefixes

-- Create prefix index

-- Choose appropriate prefix length
sql
-- Don't index small tables
-- Don't index columns with low cardinality (few unique values)
-- Don't index columns frequently updated
-- Don't index columns with long text values (use prefix index)

B

:=、<、>、BETWEEN、LIKE 'prefix%'

:=、IN()、<>

sql

-- Search modes
-- Natural language (default)

-- Boolean mode

-- Query expansion
sql

-- Spatial queries

sql
-- Update index cardinality

-- Check table analysis

sql
-- Defragment table and reclaim unused space

-- Helps with:
-- - Deleted rows
-- - Split pages
-- - Index rebuilding
sql
-- Rebuild specific index

-- Rebuild all indexes
sql
-- Identify unused indexes (MySQL 8.0+)
sql
-- Create indexes for:
-- 1. Primary keys (automatic)
-- 2. Foreign keys (automatic for InnoDB)
-- 3. Columns in WHERE clauses
-- 4. Columns in JOIN conditions
-- 5. Columns in ORDER BY
-- 6. Columns in GROUP BY
-- 7. Columns for uniqueness
sql
-- Avoid indexes for:
-- 1. Tables with few rows
-- 2. Columns with low cardinality (few unique values)
-- 3. Columns frequently updated
-- 4. Columns with long text values (use prefix)
-- 5. Columns only in SELECT, not WHERE/JOIN/ORDER BY
sql
-- Enable performance schema

-- Check slow queries

-- Use EXPLAIN regularly

:PRIMARY、UNIQUE、INDEX、FULLTEXT、SPATIAL


Content is for learning and research only.