如何为这样的sql查询添加索引?

时间:2017-07-05 13:35:00

标签: mysql sql indexing

请告诉我如何为这个SQL查询添加索引?

SELECT * FROM table WHERE (cities IS NULL) AND (position_id = '2') AND (is_pub = '1') ORDER BY ordering asc LIMIT 1

字段类型:

  • cities = text
  • position_id = int(11)
  • is_pub = tinyint(1)

我试试这样: ALTER TABLE table ADD FULLTEXT ( 'cities', 'position_id', 'is_pub' );

但我收到错误:The used table type doesn't support FULLTEXT indexes

2 个答案:

答案 0 :(得分:0)

首先,重写查询,这样您就不会混合类型。也就是说,摆脱单引号:

SELECT *
FROM table
WHERE (cities IS NULL) AND (position_id = 2) AND (is_pub = 1)
ORDER BY ordering asc
LIMIT 1;

然后,对此的最佳查询是table(position_id, is_pub, cities, ordering)

create index idx_table_4 on table(position_id, is_pub, cities(32), ordering);

前三列可以是索引中的任何顺序,只要它们是前三列。

您应该将cities更改为varchar()类型。您是否有理由为此使用文字?

答案 1 :(得分:-2)

您需要将表格的引擎更改为MyISAM。

#1214 - The used table type doesn't support FULLTEXT indexes

可能重复