针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005,使用强制索引。
drop table if exists salaries; CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL, `salary` int(11) NOT NULL, `from_date` date NOT NULL, `to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`)); create index idx_emp_no on salaries(emp_no); INSERT INTO salaries VALUES(10005,78228,'1989-09-12','1990-09-12'); INSERT INTO salaries VALUES(10005,94692,'2001-09-09','9999-01-01');
后台会检查是否使用强制索引
SELECT * FROM salaries FORCE INDEX(idx_emp_no) WHERE emp_no = '10005';