複合索引&最左原則

建表語句:

	CREATE TABLE `t_user` (
		`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
		`bu_id` INT(20) NOT NULL,
		`name` VARCHAR(255) NOT NULL,
		`age` INT(11) NOT NULL,
		`sex` VARCHAR(255) NULL DEFAULT NULL,
		PRIMARY KEY (`id`),
		INDEX `index_uid_name_age` (`bu_id`, `name`, `age`)
	)
	COLLATE='utf8_general_ci'
	ENGINE=InnoDB
	AUTO_INCREMENT=22014
	;
	
	
批量插入數據:

	CREATE PROCEDURE batch_insert() 
	BEGIN 
		DECLARE a INT DEFAULT 1; 

		WHILE (a <= 10000) DO 
		SET a = a + 1; 
		insert into t_user set name='name2', age=a, bu_id=a+5;
		END WHILE;

		COMMIT;
	END;
	CALL batch_insert();
	
	

Explain SQL:

	
Explain SQL:

	1)explain select bu_id,name,age,sex from t_user where bu_id=17
	  
		  結果:id	select_type	 table	type	possible_keys			key						key_len	 ref	rows 
				1	SIMPLE		 t_user	ref		index_buid_name_age		index_buid_name_age		4		 const	10853	
		  結論:使用了index_buid_name_age索引,並且只使用了複合索引中的bu_id列。
			
	2)explain select bu_id,name,age,sex from t_user where name='jack'
		
		  結果:id	select_type	 table	type	possible_keys			key 					key_len  ref	rows	Extra 
				1	SIMPLE		 t_user	ALL																		21706	Using where
		  結論:沒有使用索引,進行了全表掃描。
		
	3)explain select bu_id,name,age,sex from t_user where bu_id=17 and name='jack'
		
		  結果:id	select_type	 table	type	possible_keys			key 					key_len  ref			rows	Extra 
				1	SIMPLE		 t_user	ref		index_buid_name_age		index_buid_name_age		771		 const,const	1		Using index condition
				
		  結論:使用了index_buid_name_age索引,並且(根據key_len可以推斷出)使用了該複合索引中的bu_id列和name列。
		
	4)explain select bu_id,name,age,sex from t_user where bu_id=17 and name='jack' and age=18
	
		  結果:id	select_type	 table	type	possible_keys			key 					key_len  ref				rows	Extra 
				1	SIMPLE		 t_user	ref		index_buid_name_age		index_buid_name_age		775		 const,const,const	1		Using index condition
				
		  結論:使用了index_buid_name_age索引,並且(根據key_len可以推斷出)使用了該複合索引中的bu_id列、name列和age列。
	
	5)explain select bu_id,name,age,sex from t_user where bu_id=17 and name like '%jack'
	
		  結果:id	select_type	 table	type	possible_keys			key 					key_len  ref			rows	Extra 
				1	SIMPLE		 t_user	ref		index_buid_name_age		index_buid_name_age		4		 const			10853	Using index condition
		  結論:使用了index_buid_name_age索引,但是隻使用了該複合索引中的bu_id列。
	  
	6)explain select bu_id,name,age,sex from t_user where bu_id=17 and name like 'jack%'
	 
		  結果:id	select_type	 table	type	possible_keys			key 					key_len  ref			rows	Extra 
				1	SIMPLE		 t_user	range	index_buid_name_age		index_buid_name_age		771						1		Using index condition
		  結論:使用了index_buid_name_age索引,並且(根據key_len可以推斷出)使用了該複合索引中的bu_id列和name列。
	  
	7)explain select bu_id,name,age,sex from t_user where bu_id=17 and age=18
	  
		  結果:id	select_type	 table	type	possible_keys			key 					key_len  ref			rows	Extra 
				1	SIMPLE		 t_user	ref		index_buid_name_age		index_buid_name_age		4		 const			10852	Using index condition
		  結論:使用了index_buid_name_age索引,但是隻使用了該複合索引中的bu_id列。
	  
	8)explain select name from t_user where name='jack' 	或	explain select bu_id,name,age from t_user where name='jack'
	  
		  結果:id	select_type	 table	type	possible_keys			key 					key_len  ref			rows	Extra 
				1	SIMPLE		 t_user	index							index_buid_name_age		775						21705	Using where; Using index
		  結論:使用了index_buid_name_age索引,並且(根據key_len可以推斷出)使用了該複合索引中的bu_id列、name列和age列。
		  注意:比較一下第2個sql語句和第8個sql語句,二者的區別在於:
				1>第2個sql語句查詢了非索引列sex,故根據最左原則,無法使用該索引;
				2>第8個sql語句只查詢了索引列,(若只查詢索引列,則只需掃描索引樹即可)故可以使用該索引。
	  
	  
	  
	  
	  
			
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章