问题描述
有什么方法可以指示 MySQL 不使用索引,即使它可用?同样,即使列可用也不使用? (例如,只需将查询错误为“列不存在”。)在准备删除索引或字段时,拥有此功能会很有用。
Postgres 怎么样?
解决方法
在 PostgreSQL 中,您可以turn off 进行索引扫描:
set enable_indexscan to 'off';
set enable_indexonlyscan to 'off';
set enable_bitmapscan to 'off';
或者让它们变得非常昂贵:
set random_page_cost to 10000000;
,
免责声明:仅使用 PostgreSQL... 8 年多没有使用 MySQL。
另外,您似乎没有针对查询,但我想我还是会提供它(也许对于有类似问题的任何人)。
这有点像黑客,但如果您考虑下表:
create table foo (
id integer
);
create index foo_ix1 on foo (id);
insert into foo generate_series (1,1000000);
正如您所料,这将使用索引:
explain
select * from foo
where id between 5 and 10;
除非您对该特定函数具有基于函数的索引,否则将任何函数应用于该列都应该使规划器无法使用该索引。简单例子:
explain
select * from foo
where id + 0 between 5 and 10;
我没有更改结果,但我强制进行了全面扫描。
例如,对于文本列,您可以只附加一个空字符串:text_field || ''
。