我对我要做的索引感到有些困惑。
首先,我使用的是4列索引,如下所示:
索引名称 - advanced_query
列将用于索引 - 标题,类别1,类别2,类别3
索引代码
ALTER TABLE table_name ADD INDEX advanced_query (`title`, `cat_1`, `cat_2`, `cat_3`, `date_posted`)
好的,这就是(根据我的理解)它将起作用的方式:
TL; DR
所以在这个索引中,只有查询cat_3 不会从中受益,对吧?谢谢!
问/ A
我在做什么查询?搜索帖子(它的标题和3个不同的类别)
桌子的大小是多少?不到2千行
表的结构?
CREATE TABLE `post_lists` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`users_id` varchar(100) NOT NULL,
`code` varchar(255) NOT NULL,
`date_posted` datetime NOT NULL,
`date_updated` datetime NOT NULL,
`title` varchar(255) NOT NULL,
`cat_1` varchar(255) NOT NULL,
`cat_3_code` varchar(255) NOT NULL,
`details` varchar(10000) NOT NULL,
`cat_2` varchar(255) NOT NULL,
`cat_3` varchar(255) NOT NULL,
UNIQUE KEY `id` (`id`),
KEY `date_posted` (`date_posted`),
KEY `code` (`urlcode`),
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`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=latin1
此表使用多少次?大多数时候。这是高级搜索功能,因此不仅仅是基本搜索。
这就是我实际使用索引的方式。
示例表
title | cat_1 | cat_2 | cat_3 | date_posted
我的疑问很简单:
标题
SELECT * FROM tbl_name WHERE title LIKE %title% ORDER BY date_posted DESC
标题 + cat_1
SELECT * FROM tbl_name WHERE title LIKE %title% AND cat_1 = 'cat_1' ORDER BY date_posted DESC
标题 + cat_1 + cat_2
SELECT * FROM tbl_name WHERE title LIKE %title% AND cat_1 = 'cat_1' AND cat_2 = 'cat_2' ORDER BY date_posted DESC
标题 + cat_1 + cat_2 + cat_3
SELECT * FROM tbl_name WHERE title LIKE %title% AND cat_1 = 'cat_1' AND cat_2 = 'cat_2' AND cat_3 = 'cat_3' ORDER BY date_posted DESC
标题 + cat_1 + cat_3
SELECT * FROM tbl_name WHERE title LIKE %title% AND cat_1 = 'cat_1' and cat_3 = 'cat_3' ORDER BY date_posted DESC
标题 + cat_2
SELECT * FROM tbl_name WHERE title LIKE %title% AND cat_2 = 'cat_2' ORDER BY date_posted DESC
标题 + cat_2 + cat_3
SELECT * FROM tbl_name WHERE title LIKE %title% AND cat_2 = 'cat_2' AND cat_3 = 'cat_3' ORDER BY date_posted DESC
标题 + cat_3
SELECT * FROM tbl_name WHERE title LIKE %title% AND cat_3 = 'cat_3' ORDER BY date_posted DESC
cat_1
SELECT * FROM tbl_name WHERE cat_1 = 'cat_1' ORDER BY date_posted DESC
cat_1 + cat_2
SELECT * FROM tbl_name WHERE cat_1 = 'cat_1' AND cat_2 = 'cat_2' ORDER BY date_posted DESC
cat_1 + cat_2 + cat_3
SELECT * FROM tbl_name WHERE cat_1 = 'cat_1' AND cat_2 = 'cat_2' AND cat_3 = 'cat_3' ORDER BY date_posted DESC
cat_1 + cat_3
SELECT * FROM tbl_name WHERE cat_1 = 'cat_1' AND cat_3 = 'cat_3' ORDER BY date_posted DESC
cat_2
SELECT * FROM tbl_name WHERE cat_2 = 'cat_2' ORDER BY date_posted DESC
cat_2 + cat_3
SELECT * FROM tbl_name WHERE cat_2 = 'cat_2' ORDER BY date_posted DESC
cat_3
SELECT * FROM tbl_name WHERE cat_3 = 'cat_3' ORDER BY date_posted DESC
我该如何查询?
修改
嗨,我阅读并搜索了全文搜索,我正在考虑使用它(在基本搜索中)而不是LIKE %wildcard%
并将其应用于title
和details
,我的问题是我希望他们对ORDER BY date_posted DESC
进行排序,那么我应该在全文搜索中添加date_posted
还是创建一个单独的索引?
答案 0 :(得分:2)
我认为"答案"对你的问题有点复杂,你对索引使用的假设并不总是正确的。
较短的答案是:"它取决于"。
实际上索引的使用取决于几个因素:表中的记录数,索引结构,请求的字段,查询中的条件,统计信息。
1)记录数:如果它很小,也许db引擎决定不使用索引(特别是如果你编写SELECT的SELECT * - 表中的几列不在索引中)。
如果只选择索引中的部分或全部列,则可以使用索引(不考虑WHERE条件)。
2)索引结构:正如你所指出的那样,它是相关的。更多信息有两种不同的主要方式,索引可以"使用":扫描和搜索。寻求是最有效的。在大多数情况下,如果您按照编写它们的顺序查找索引中的列,则可以进行搜索:例如。从你的表中选择标题,如标题所示' ABC%')。注意:如果你写了LIKE'%ABC%'它不能进行搜索,而是扫描。 (扫描意味着数据库必须从头到尾查找整个索引,而搜索时他会直接查找相关页面,因为您将使用lastname查找电话簿中某个人的电话号码)。
3)请求的字段:你应该考虑如果你编写SELECT *(正如我上面指出的那样,db引擎可以决定使用全表扫描)
4)查询中的条件。
5)统计:数据库引擎写入数据和索引的统计数据(记录数,结构数等)。如果它们没有更新,它可能会使用或不使用索引" erroneus"方式。
-----更新:简单(不详尽......)演示
实际上(使用这些小数据,我必须评论你的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,
`code` 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_code` 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 `code` (`code`)
, 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, code, title, cat_1, cat_3_code, details, cat_2, cat_3) VALUES ('123', 'ABC', 'TITLE1', '001','C3','blah blah blah', '002', '003');
INSERT INTO post_lists (users_id, code, title, cat_1, cat_3_code, 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, code 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 | code | date_posted | date_updated | title | cat_1 | cat_3_code | 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 |
+----+----+-------------+------------+------------+------+----------------+----------------+---------+-------+------+----------+-------------+