¿Cómo ver los índices de una base de datos o tabla en MySQL?


¿Cómo puedo ver si mi base de datos tiene algún índice?

¿Qué tal para una tabla específica?

Author: Ramratan Gupta, 2011-03-06

8 answers

Para ver el índice de una tabla específica use SHOW INDEX:

SHOW INDEX FROM yourtable;

Para ver los índices de todas las tablas dentro de un esquema específico, puede usar la tabla de ESTADÍSTICAS de INFORMATION_SCHEMA:

SELECT DISTINCT
    TABLE_NAME,
    INDEX_NAME
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'your_schema';

La eliminación de la cláusula where le mostrará todos los índices en todos los esquemas.

 552
Author: Mark Byers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-06 21:11:19

Si desea ver todos los índices en todas las bases de datos a la vez:

use information_schema;
SELECT * FROM statistics;
 34
Author: RolandoMySQLDBA,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-17 08:48:04
SHOW INDEX FROM mytable FROM mydb;

SHOW INDEX FROM mydb.mytable;

Véase documentación.

 30
Author: LiorK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-17 08:47:50

Puede usar esta consulta para obtener el no de índices, así como los nombres de índice de cada tabla en la base de datos especificada.

SELECT TABLE_NAME,
       COUNT(1) index_count,
       GROUP_CONCAT(DISTINCT(index_name) SEPARATOR ',\n ') indexes
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = 'mydb'
      AND INDEX_NAME != 'primary'
GROUP BY TABLE_NAME
ORDER BY COUNT(1) DESC;
 5
Author: adeviloper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-21 07:38:58

Propongo esta pregunta:

SELECT DISTINCT s.*
FROM INFORMATION_SCHEMA.STATISTICS s
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS t 
    ON t.TABLE_SCHEMA = s.TABLE_SCHEMA 
       AND t.TABLE_NAME = s.TABLE_NAME
       AND s.INDEX_NAME = t.CONSTRAINT_NAME 
WHERE 0 = 0
      AND t.CONSTRAINT_NAME IS NULL
      AND s.TABLE_SCHEMA = 'YOUR_SCHEMA_SAMPLE';

Has encontrado todo Index solo index.

Atención.

 3
Author: user2065095,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-21 06:46:31

Para comprobar todos los índices deshabilitados en db

SELECT INDEX_SCHEMA, COLUMN_NAME, COMMENT 
FROM information_schema.statistics
WHERE table_schema = 'mydb'
AND COMMENT = 'disabled'
 1
Author: Digital87,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-28 06:25:00

Esto funciona en mi caso para obtener el nombre de la tabla y el nombre de la columna en la tabla correspondiente para los campos indexados.

SELECT TABLE_NAME , COLUMN_NAME, COMMENT 
FROM information_schema.statistics
WHERE table_schema = 'database_name';
 1
Author: asim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-28 12:06:14

Puede consultar sus índices en MySQL workbench.en las pestañas informes de rendimiento puede ver todos los índices utilizados y los índices no utilizados en el sistema. o puede iniciar la consulta.

select * from sys.schema_index_statistics;
 0
Author: Ganesh Giri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-28 12:06:06