如何找出MySQL中没有索引的表

时间:2011-08-29 17:40:49

标签: mysql sql indexing

我正在处理一个包含大约300个表的数据库,我正在寻找一种方法来查找所有没有索引的表(不包括PRIMARY)。理想情况下,我想得到一个结果集,它给出了每个表的索引数量的计数。 我使用这样的查询尝试了这个:

SELECT 
  table_name, column_name, index_name, count(table_name) as index_count 
FROM 
  information_schema.statistics 
WHERE 
  index_name != 'primary' 
AND
  table_schema = 'your_database'
GROUP BY
  table_name

但它不会返回准确的结果。任何帮助将不胜感激

5 个答案:

答案 0 :(得分:8)

这将导致那些根本没有索引的表(甚至不是主键)

select * from INFORMATION_SCHEMA.tables
where table_schema = 'your_database'
and table_name not in 
(
select  table_name -- , count(*) 
from (
SELECT  table_name, index_name
FROM information_schema.statistics 
WHERE table_schema = 'your_database'
GROUP BY  table_name, index_name) tab_ind_cols
group by table_name
)

答案 1 :(得分:5)

select 
    t.table_schema,t.table_name,engine 
from 
    information_schema.tables t 
    inner join information_schema .columns c  
        on t.table_schema=c.table_schema and t.table_name=c.table_name 
group by 
    t.table_schema,t.table_name   
having 
    sum(if(column_key in ('PRI','UNI'), 1,0)) = 0;

此选择有效但如果表不是PRIMARY或Unique

则返回

答案 2 :(得分:1)

这将返回所有表的索引数

select  table_name, count(*) from (
SELECT  table_name, index_name
FROM information_schema.statistics 
WHERE table_schema = 'your_database'
GROUP BY  table_name, index_name) tab_ind_cols
group by table_name

答案 3 :(得分:1)

这种情况的有用工具也是启用MySQL慢查询日志。您需要在my.cnf上启用它,但因为只记录超过设定时间的查询,您甚至可以在生产系统上保持打开状态。

有了这些,就会记录需要很长时间才能回答的查询,然后你可以看到是什么让它们变慢了。

答案 4 :(得分:0)

bpgergo,在这里有所作为。

我相信这就是你要找的东西

-- ============================================================================
-- TABLES WITHOUT INDICES BUT HAVE A PRIMARY KEY
-- ============================================================================
SELECT *
FROM INFORMATION_SCHEMA.TABLES AS MAIN_TABLE
WHERE
    TABLE_SCHEMA = 'your_database'
    -- ============================================================================
    -- FIND TABLES WITH A PRIMARY KEY
    -- ============================================================================
    AND TABLE_NAME IN (
                SELECT TABLE_NAME
                FROM (
                        SELECT TABLE_NAME, INDEX_NAME, COUNT(INDEX_NAME) AS TEST
        FROM INFORMATION_SCHEMA.STATISTICS
        WHERE TABLE_SCHEMA = 'your_database'
                AND INDEX_NAME = 'PRIMARY'
        GROUP BY TABLE_NAME , INDEX_NAME) AS TAB_IND_COLS
    GROUP BY TABLE_NAME)
    -- ============================================================================
    -- FIND TABLES WITH OUT ANY INDICES
    -- ============================================================================
AND TABLE_NAME NOT IN (
        SELECT TABLE_NAME
        FROM (
                SELECT TABLE_NAME, INDEX_NAME, COUNT(INDEX_NAME) AS TEST
        FROM INFORMATION_SCHEMA.STATISTICS
        WHERE TABLE_SCHEMA = 'your_database'
                AND INDEX_NAME <> 'PRIMARY'
        GROUP BY TABLE_NAME , INDEX_NAME) AS TAB_IND_COLS
    GROUP BY TABLE_NAME
    )
;
  

您只需要更改your_database即可使其正常运行。