我认为对您的问题的“答案”有点复杂,您对使用索引的假设并不总是正确的。
简短的答案是:“取决于”。
实际上,索引的使用取决于几个因素:表中的记录数,索引结构,请求的字段,查询中的条件,统计信息。
1)记录数:如果很小,则db引擎可能会决定不使用索引(尤其是如果您将SELECt的SELECT *写入SELECT –表中的几列不在索引-中)。
如果仅选择索引中的部分或全部列,则可以使用索引(也不考虑WHERe条件)。
2)索引结构:正如您所指出的,它是相关的。Morevore有两种可以“使用”索引的主要方法:扫描和查找。寻求是最有效的。在大多数情况下,您是否会以编写顺序查找索引中的列:从表的标题中选择标题,例如“
ABC%”)。注意:如果您写了LIKE’%ABC%’,则它不能进行搜索,而只能进行扫描。(扫描表示db必须从头到尾查找整个索引,而查找时他将直接转到相关页面,因为您将使用姓氏在电话簿中查找某人的电话号码)。
3)要求的字段:您应该考虑如果您编写SELECT *(如上所述,db引擎可能会决定使用全表扫描)
4)查询条件。
5)统计信息:db引擎写入有关数据和索引(记录数,结构等)的统计信息。如果它们未更新,则可能以“错误”方式使用或不使用索引。
-----更新:简单(不详尽…)演示
实际上(使用少量数据,我不得不注释您的KEY’title_date_posted’以便在某些情况下使用“
advanced_query”索引:否则,它似乎尝试使用它;正如我告诉您的那样,db引擎会做出内部决策使用的索引)。
在rextester.com上完成的测试:
##DROP TABLE post_lists;CREATE TABLE `post_lists` ( `id` int(100) NOT NULL AUTO_INCREMENT, `users_id` varchar(100) NOT NULL, `pre` varchar(255) NOT NULL, `date_posted` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `date_updated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `title` varchar(255) NOT NULL, `cat_1` varchar(255) NOT NULL, `cat_3_pre` varchar(255) NOT NULL, `details` varchar(10000) NULL, `cat_2` varchar(255) NOT NULL, `cat_3` varchar(255) NOT NULL, UNIQUE KEY `id` (`id`) , KEY `date_posted` (`date_posted`) , KEY `pre` (`pre`) , KEY `users_id_date_posted` (`users_id`,`date_posted`) ##, KEY `title_date_posted` (`title`,`date_posted`) , KEY `cat_1_date_posted` (`cat_1`,`date_posted`)) DEFAULT CHARSET=latin1;ALTER TABLE post_lists ADD INDEX advanced_query (`title`, `cat_1`, `cat_2`, `cat_3`, `date_posted`);INSERT INTO post_lists (users_id, pre, title, cat_1, cat_3_pre, details, cat_2, cat_3) VALUES ('123', 'ABC', 'TITLE1', '001','C3','blah blah blah', '002', '003');INSERT INTO post_lists (users_id, pre, title, cat_1, cat_3_pre, details, cat_2, cat_3) VALUES ('456', 'ABC', 'TITLE2', '002','C32','blah blah blah', '0021', '0031');SELECT * FROM post_lists;EXPLAIN SELECt * FROM post_lists WHERe title = 'TITLE1'; EXPLAIN SELECt title FROM post_lists WHERe title = 'TITLE1'; EXPLAIN SELECt title, cat_1, cat_3, pre FROM post_lists WHERe title = 'TITLE1'; EXPLAIN SELECt title, cat_1, cat_3 FROM post_lists WHERe title = 'TITLE1';DROP TABLE post_lists;输出:
+----+----+----------+------+---------------------+---------------------+--------+-------+------------+----------------+-------+-------+| | id | users_id | pre | date_posted | date_updated | title | cat_1 | cat_3_pre | details | cat_2 | cat_3 |+----+----+----------+------+---------------------+---------------------+--------+-------+------------+----------------+-------+-------+| 1 | 1 | 123 | ABC | 27.06.2017 11:02:16 | 27.06.2017 11:02:16 | TITLE1 | 001 | C3 | blah blah blah | 002 | 003 || 2 | 2 | 456 | ABC | 27.06.2017 11:02:16 | 27.06.2017 11:02:16 | TITLE2 | 002 | C32 | blah blah blah | 0021 | 0031 |+----+----+----------+------+---------------------+---------------------+--------+-------+------------+----------------+-------+-------++----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+| | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+| 1 | 1 | SIMPLE | post_lists | NULL | ref | advanced_query | advanced_query | 257 | const | 1 | 100 | NULL |+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------++----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------------+| | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------------+| 1 | 1 | SIMPLE | post_lists | NULL | ref | advanced_query | advanced_query | 257 | const | 1 | 100 | Using index |+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------------++----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+| | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+| 1 | 1 | SIMPLE | post_lists | NULL | ref | advanced_query | advanced_query | 257 | const | 1 | 100 | NULL |+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------++----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------------+| | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------------+| 1 | 1 | SIMPLE | post_lists | NULL | ref | advanced_query | advanced_query | 257 | const | 1 | 100 | Using index |+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------------+



