CREATE TABLE employees (
id INT AUTO_INCREMENT,
lastname VARCHAR(255),
PRIMARY KEY (id)
);
SELECT * FROM employees where id = 1;
CREATE UNIQUE INDEX idx_unique_email ON employees (email);
CREATE TABLE user_roles (
user_id INT,
role_id INT,
UNIQUE INDEX unique_user_role (user_id, role_id)
);
CREATE INDEX idx_lastname ON employees (lastname);
CREATE INDEX idx_name_dob ON employees (lastname, firstname, dob);
CREATE FULLTEXT INDEX idx_content ON articles (content);
SELECT * FROM articles
WHERE MATCH (content) AGAINST ('+MySQL -index' IN BOOLEAN MODE);
CREATE TABLE locations (
id INT AUTO_INCREMENT PRIMARY KEY,
place POINT,
SPATIAL INDEX (place)
);
SELECT id, place, ST_Distance_Sphere(place, POINT(1.0, 1.0)) as distance
FROM locations
WHERE ST_Distance_Sphere(place, POINT(1.0, 1.0)) < 10000
ORDER BY distance;
CREATE INDEX idx_fullname ON employees (lastname, firstname);
CREATE INDEX idx_multi ON orders (customer_id, order_date, status);
SELECT * FROM orders
WHERE customer_id = 123 AND status = 'shipped'
ORDER BY order_date DESC;
EXPLAIN SELECT * FROM employees WHERE lastname = 'Smith';
EXPLAIN SELECT * FROM orders
WHERE customer_id = 123 AND status = 'shipped'
ORDER BY order_date DESC;
SHOW INDEX
and ANALYZE TABLE
.SHOW INDEX FROM employees;
ANALYZE TABLE employees;
OPTIMIZE TABLE employees;
DROP INDEX idx_lastname ON employees;
CREATE INDEX idx_combined ON sales (product_id, sale_date, region);