我在mysql中有一个查询。
SELECT
*
FROM
Accounts AS a
WHERE
('s' IS NULL OR (a.FirstName LIKE CONCAT('s','%') OR
a.LastName LIKE CONCAT('s','%') OR
a.FullName LIKE CONCAT('s','%')
)
)我应该如何为表放置索引?
附注:“%s”实际上是存储的proc中的一个变量,因此“%s”为NULL,并且必须连接。
发布于 2011-01-27 01:32:20
首先,给你一个简短的建议:如果没有必要,不要使用concat。您的查询可以重写,因为('s' is NULL)始终为FALSE,所以无论如何您都可以根据第二个条件获取所有行:
SELECT
*
FROM
Accounts AS a
WHERE
a.FirstName LIKE 's%' OR
a.LastName LIKE 's%' OR
a.FullName LIKE 's%'可能会帮助的索引,但不一定会:
create index idx_01 on accounts(FirstName);
create index idx_01 on accounts(LastName);
create index idx_01 on accounts(FullName);您还可以考虑为表创建全文搜索索引。
发布于 2011-01-27 01:33:30
对于您来说,full text indexing也是一种选择
为3个字段添加全文索引,然后
使用
MATCH() AGAINST()语法
例如
SELECT * FROM articles WHERE MATCH (title,body)
AGAINST ('superb catch' IN BOOLEAN MODE);https://stackoverflow.com/questions/4807715
复制相似问题