【发布时间】:2017-06-27 07:52:23
【问题描述】:
我对我要做的索引有点困惑。
首先,我使用的是 4 列索引,如下所示:
索引名称 - advanced_query
将在索引中使用列 - 标题、类别 1、类别 2、类别 3
索引代码
ALTER TABLE table_name ADD INDEX advanced_query (`title`, `cat_1`, `cat_2`, `cat_3`, `date_posted`)
好的,这就是(据我了解)它的工作方式:
- title 的查询将使用索引。
- cat_1 的查询将使用索引。
- cat_2 的查询将使用索引。
- cat_3 的查询将不使用索引。所以我会为它创建一个不同的索引。
- title,cat_1的查询将使用索引。
- title,cat_1,cat_2的查询将使用索引。
- title、cat_1、cat_2、cat_3的查询将使用索引。
- title,cat_1,cat_3的查询将使用索引。
- title,cat_2的查询将使用索引。
- title,cat_2,cat_3的查询将使用索引。
- title,cat_3的查询将使用索引。
- cat_1, cat_2 的查询将使用索引。
- cat_1、cat_2、cat_3的查询将使用索引。
- cat_1, cat_2 的查询将使用索引。
- cat_1, cat_3 的查询将使用索引。
TL;DR
所以在这个索引中,只有 cat_3 的查询 不会从中受益,对吧?谢谢!
问答
我在做什么查询?搜索帖子(它的标题和 3 个不同的类别)
桌子的尺寸是多少?少于 2000 行
表的结构?
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 -
title + 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还是创建一个单独的索引?
【问题讨论】:
-
要了解您的查询是否使用您的索引,您应该在将
EXPLAIN添加到它们的开头之后运行它们中的每一个,所以EXPLAIN SELECT * ...也许将解释的输出添加到您的问题中,请注意,如果您在开头使用通配符,则索引不适用于标题,因此'%title%'不会使用索引,但'title'和'titl%'会。 -
@FMashiro 我明白了,所以我将使用一个'%'而不是双倍
%?谢谢。 -
另外,当用户请求
Structure of the table ?时,他们通常意味着他们想要SHOW CREATE TABLE tablename输出或至少是数据类型 -
不一定,因为如果您在末尾使用单个通配符,
'itle%'将不再匹配'title'。请注意,我只是提到它不会使用索引。 -
但是你说
'title%'会使用索引对吗?所以像这样SELECT * FROM tbl WHERE title LIKE 'title%' AND cat_1 = 'cat_1 and so on...'这会使用索引吗?
标签: mysql