MySQL
sql
sql
-- Add unique index to existing tablesql
-- Add index to existing tablesql
-- For InnoDB and MyISAM tables
-- Use MATCH AGAINST for searching
-- Natural language mode
-- Boolean modesql
-- Query using spatial functionssql
-- 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
-- Indexessql
-- Add simple index
-- Add unique index
-- Add composite index
-- Add full-text index (MySQL 5.6+)
-- Add spatial indexsql
-- Custom index namesql
-- Show indexes on a table
-- Detailed index information
-- From information_schemasql
-- Drop index
-- Drop multiple indexes
-- Using ALTER TABLEsql
-- 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 informationsql
-- 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 BYsql
-- 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' alonesql
-- 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 columnsql
-- Index on first N characters of string column
-- Useful for long text columns with repetitive prefixes
-- Create prefix index
-- Choose appropriate prefix lengthsql
-- 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 expansionsql
-- Spatial queries
sql
-- Update index cardinality
-- Check table analysis
sql
-- Defragment table and reclaim unused space
-- Helps with:
-- - Deleted rows
-- - Split pages
-- - Index rebuildingsql
-- Rebuild specific index
-- Rebuild all indexessql
-- 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 uniquenesssql
-- 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 BYsql
-- Enable performance schema
-- Check slow queries
-- Use EXPLAIN regularly:PRIMARY、UNIQUE、INDEX、FULLTEXT、SPATIAL