我正在从单个表中进行简单的选择。
CREATE TABLE `book` (
`Book_Id` int(10) NOT NULL AUTO_INCREMENT,
`Book_Name` varchar(100) COLLATE utf8_turkish_ci DEFAULT NULL ,
`Book_Active` bit(1) NOT NULL DEFAULT b'1' ,
`Author_Id` int(11) NOT NULL,
PRIMARY KEY (`Book_Id`),
KEY `FK_Author` (`Author_Id`),
CONSTRAINT `FK_Author` FOREIGN KEY (`Author_Id`) REFERENCES `author` (`Author_Id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5947698 DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci ROW_FORMAT=COMPACT
表:书
列:
Book_Id (INTEGER 10) | 书名 (VARCHAR 100) | Author_Id (整数 10) | Book_Active (布尔值)
我在三列上有索引: Book_Id (PRIMARY key) , Author_Id (FK) , Book_Active 。
第一个查询:
SELECT * FROM book WHERE Author_Id = 1 AND Book_Active = 1
EXPLAIN :
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE book ref FK_Author,index_Book_Active FK_Author 4 const 4488510 Using where
第二个查询:
SELECT b.* FROM book b
WHERE Book_Active=1
AND Book_Id IN (SELECT Book_Id FROM book WHERE Author_Id=1)
EXPLAIN :
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY book ref index_Book_Active index_Book_Active 1 const 9369399 Using where
2 DEPENDENT SUBQUERY book unique_subquery PRIMARY,FK_Author PRIMARY 4 func 1 Using where
数据统计是这样的:
16.8 million books
10.5 million Book_Active=true
6.3 million Book_Active = false
而对于Author_Id=1
2.4 million Book_Active=false
5000 Book_Active=true
第一个查询需要6.7秒。第二次查询耗时0.0002秒
造成这种巨大差异的原因是什么?使用嵌套选择查询是否正确?
编辑:添加“sql解释”