我试图找出某个列是否被索引(本身)。换句话说,我不想提取多列的索引。
有人可以协助我将其重写为仅提取单列索引吗?
select table_name, index_name
from information_schema.statistics
where table_schema='schema' and table_name='table' and column_name='column';
最佳答案
您可以在子查询中按索引名称对索引进行分组,然后选择唯一的索引。
select index_name from
(select index_name, COUNT(*) as col_count
from information_schema.statistics
where table_schema='schema' and table_name='table'
group by index_name) as sub
where col_count = 1
您可能希望将
column_name
添加到内部和外部select
-列表中;以及外部where
条件。在这种情况下,由于条件col_count = 1
,这可能会起作用,但通常在COUNT
-list中没有列出,而通常具有琐碎的列(不使用MAX
,select
,...)。 group
语句导致无法预测的行为,请参见。 sql antipattern。