AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / dba / Perguntas / 212318
Accepted
Sybil
Sybil
Asked: 2018-07-17 06:04:40 +0800 CST2018-07-17 06:04:40 +0800 CST 2018-07-17 06:04:40 +0800 CST

A consulta para identificar índices ruins e/ou nenhum índice (em tabelas com muitos registros) leva de 4 a 6 minutos para ser executada

  • 772

Consulta:

SELECT Concat(t.table_schema, '.', t.table_name),
       t.table_rows,
       snu.non_unique,
       smax.cardinality,
       ( t.table_rows / Ifnull(smax.cardinality, 1) )                AS
       "medium distribution",
       t.table_rows * ( t.table_rows / Ifnull(smax.cardinality, 1) ) AS
       "replication row reads"
FROM   information_schema.tables t
       LEFT JOIN (SELECT table_schema,
                         table_name,
                         Max(cardinality) cardinality
                  FROM   information_schema.statistics
                  GROUP  BY table_schema,
                            table_name) AS smax
              ON t.table_schema = smax.table_schema
                 AND t.table_name = smax.table_name
       LEFT JOIN (SELECT table_schema,
                         table_name,
                         Min(non_unique) non_unique
                  FROM   information_schema.statistics
                  GROUP  BY table_schema,
                            table_name) AS snu
              ON t.table_schema = snu.table_schema
                 AND t.table_name = snu.table_name
WHERE  t.table_rows > 0
       AND t.table_schema <> 'information_schema'
       AND t.table_schema <> 'performance_schema'
       AND t.table_schema <> 'mysql'
       AND ( snu.non_unique IS NULL
              OR snu.non_unique = 1 )
       AND ( ( t.table_rows / Ifnull(smax.cardinality, 1) ) > 1.99 )
       AND t.table_rows * ( t.table_rows / Ifnull(smax.cardinality, 1) ) >
           100000
ORDER  BY t.table_rows * ( t.table_rows / Ifnull(smax.cardinality, 1) ) DESC; 

Versões:

(none)> show variables like '%version%';
+-------------------------+---------------------------+
| Variable_name           | Value                     |
+-------------------------+---------------------------+
| innodb_version          | 5.6.36-82.1               |
| protocol_version        | 10                        |
| slave_type_conversions  |                           |
| version                 | 10.1.26-MariaDB           |
| version_comment         | Source distribution       |
| version_compile_machine | x86_64                    |
| version_compile_os      | Linux                     |
| version_malloc_library  | system                    |
| version_ssl_library     | OpenSSL 1.0.1f 6 Jan 2014 |
| wsrep_patch_version     | wsrep_25.19               |
+-------------------------+---------------------------+
10 rows in set
Time: 0.010s

Explique:

+----+-------------+------------+------+---------------+--------+---------+-------------------------------------------------------------------+--------+----------+--------------------------------------------------------------------------------------+
| id | select_type | table      | type | possible_keys | key    | key_len | ref                                                               | rows   | filtered | Extra                                                                                |
+----+-------------+------------+------+---------------+--------+---------+-------------------------------------------------------------------+--------+----------+--------------------------------------------------------------------------------------+
| 1  | PRIMARY     | t          | ALL  | <null>        | <null> | <null>  | <null>                                                            | <null> | <null>   | Using where; Open_full_table; Scanned all databases; Using temporary; Using filesort |
| 1  | PRIMARY     | <derived2> | ref  | key0          | key0   | 390     | information_schema.t.TABLE_SCHEMA,information_schema.t.TABLE_NAME | 2      | 100.0    | Using where                                                                          |
| 1  | PRIMARY     | <derived3> | ref  | key0          | key0   | 390     | information_schema.t.TABLE_SCHEMA,information_schema.t.TABLE_NAME | 2      | 100.0    | Using where                                                                          |
| 3  | DERIVED     | statistics | ALL  | <null>        | <null> | <null>  | <null>                                                            | <null> | <null>   | Open_frm_only; Scanned all databases; Using temporary; Using filesort                |
| 2  | DERIVED     | statistics | ALL  | <null>        | <null> | <null>  | <null>                                                            | <null> | <null>   | Open_full_table; Scanned all databases; Using temporary; Using filesort              |
+----+-------------+------------+------+---------------+--------+---------+-------------------------------------------------------------------+--------+----------+--------------------------------------------------------------------------------------+
5 rows in set
Time: 0.022s

Contar:

> select count('A') from information_schema.tables;
+------------+
| count('A') |
+------------+
| 7846       |
+------------+
1 row in set
Time: 0.069s

Parece que os indocumentados Open_full_table; Scanned all databases;demoram tanto assim? Como otimizar essa consulta ou essa duração é normal em um servidor ocupado?

mysql mariadb
  • 1 1 respostas
  • 222 Views

1 respostas

  • Voted
  1. Best Answer
    Rick James
    2018-07-17T11:59:53+08:002018-07-17T11:59:53+08:00

    O MySQL 8.0 corrige o problema tendo essencialmente todas as information_schemacoisas nas tabelas InnoDB. Você está executando o MariaDB 10.1, que é aproximadamente comparável ao MySQL 5.6 (pelo menos nesta área).

    O que você tem envolve a leitura de todos os arquivos .frm -- isso pode ser lento (até minutos) se você tiver muitas tabelas.

    Uma possível solução...

    1. Descubra quantas mesas você tem. (Provavelmente SELECT COUNT(*) FROM information_schema.tablesestaria perto o suficiente.)
    2. Defina os vários caches de arquivos e tabelas para pelo menos esse valor. Cuidado: Se for muito alto, pode haver pressão de memória que você deve evitar.

    Você encontrou alguns esquemas impertinentes?

    • 1

relate perguntas

  • Onde posso encontrar o log lento do mysql?

  • Como posso otimizar um mysqldump de um banco de dados grande?

  • Quando é o momento certo para usar o MariaDB em vez do MySQL e por quê?

  • Como um grupo pode rastrear alterações no esquema do banco de dados?

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    conectar ao servidor PostgreSQL: FATAL: nenhuma entrada pg_hba.conf para o host

    • 12 respostas
  • Marko Smith

    Como fazer a saída do sqlplus aparecer em uma linha?

    • 3 respostas
  • Marko Smith

    Selecione qual tem data máxima ou data mais recente

    • 3 respostas
  • Marko Smith

    Como faço para listar todos os esquemas no PostgreSQL?

    • 4 respostas
  • Marko Smith

    Listar todas as colunas de uma tabela especificada

    • 5 respostas
  • Marko Smith

    Como usar o sqlplus para se conectar a um banco de dados Oracle localizado em outro host sem modificar meu próprio tnsnames.ora

    • 4 respostas
  • Marko Smith

    Como você mysqldump tabela (s) específica (s)?

    • 4 respostas
  • Marko Smith

    Listar os privilégios do banco de dados usando o psql

    • 10 respostas
  • Marko Smith

    Como inserir valores em uma tabela de uma consulta de seleção no PostgreSQL?

    • 4 respostas
  • Marko Smith

    Como faço para listar todos os bancos de dados e tabelas usando o psql?

    • 7 respostas
  • Martin Hope
    Jin conectar ao servidor PostgreSQL: FATAL: nenhuma entrada pg_hba.conf para o host 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane Como faço para listar todos os esquemas no PostgreSQL? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh Por que o log de transações continua crescendo ou fica sem espaço? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland Listar todas as colunas de uma tabela especificada 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney O MySQL pode realizar consultas razoavelmente em bilhões de linhas? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx Como posso monitorar o andamento de uma importação de um arquivo .sql grande? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison Como você mysqldump tabela (s) específica (s)? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas Como posso cronometrar consultas SQL usando psql? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas Como inserir valores em uma tabela de uma consulta de seleção no PostgreSQL? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas Como faço para listar todos os bancos de dados e tabelas usando o psql? 2011-02-18 00:45:49 +0800 CST

Hot tag

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve