Maison > base de données > tutoriel mysql > le corps du texte

Comment interroger rapidement dans MySQL

coldplay.xixi
Libérer: 2020-11-13 10:09:25
original
3568 Les gens l'ont consulté

Méthodes de requête rapide MySQL : 1. Interroger les transactions en cours ; 2. Afficher les connexions actuelles et connaître le nombre de connexions ; 3. Afficher la taille d'une table ; 4. Afficher toutes les tables d'une taille de base de données.

Comment interroger rapidement dans MySQL

Plus de recommandations d'apprentissage gratuites connexes : Tutoriel MySQL(vidéo)

Méthode de requête rapide Mysql :

1. Interrogez les transactions en cours

select p.id,p.user,p.host,p.db,p.command,p.time,i.trx_state,i.trx_started,p.info  from information_schema.processlist p,information_schema.innodb_trx i where p.id=i.trx_mysql_thread_id;
Copier après la connexion

2 Vérifiez la connexion actuelle et connaissez le nombre de connexions

select SUBSTRING_INDEX(host,‘:‘,1) as ip , count(*) from information_schema.processlist group by ip;
Copier après la connexion

3. Vérifiez la taille. d'une table

select concat(round(sum(DATA_LENGTH/1024/1024),2),‘M‘) from information_schema.tables where table_schema=‘数据库名‘ AND table_name=‘表名‘;
Copier après la connexion

4. Vérifiez la taille de toutes les tables d'une base de données

select table_name,concat(round(sum(DATA_LENGTH/1024/1024),2),‘M‘)  from information_schema.tables where table_schema=‘t1‘ group by table_name;
Copier après la connexion

5 Vérifiez la taille de la bibliothèque et la taille de l'espace restant

select table_schema,round((sum(data_length / 1024 / 1024) + sum(index_length / 1024 / 1024)),2) dbsize,round(sum(DATA_FREE / 1024 / 1024),2) freesize,       
round((sum(data_length / 1024 / 1024) + sum(index_length / 1024 / 1024)+sum(DATA_FREE / 1024 / 1024)),2) spsize  
from information_schema.tables 
where table_schema not in (‘mysql‘,‘information_schema‘,‘performance_schema‘) 
group by table_schema order by freesize desc;
Copier après la connexion

. 6. Renseignez-vous sur l'utilisation du verrou

select r.trx_id waiting_trx_id,r.trx_mysql_thread_id waiting_thread,r.trx_query waiting_query,b.trx_id blocking_trx_id,b.trx_mysql_thread_id blocking_thread,b.trx_query blocking_query  
from information_schema.innodb_lock_waits w 
inner join information_schema.innodb_trx b 
on b.trx_id = w.blocking_trx_id 
inner join information_schema.innodb_trx r on r.trx_id = w.requesting_trx_id\G
Copier après la connexion

information_schema

1 Vérifiez la taille des données du tableau sous chaque bibliothèque

select table_name,concat(round(sum(DATA_LENGTH/1024/1024),2),‘M‘) 
from information_schema.tables where table_schema=‘db_name‘ group by table_name;
Copier après la connexion

2. taille des données de chaque base de données

select TABLE_SCHEMA, concat(round(sum(data_length)/1024/1024,2),‘ MB‘) as data_size  from information_schema.tables group by table_schema;
Copier après la connexion

3. Vérifiez si l'instance a une clé primaire

select table_schema,table_name from information_schema.tables 
where (table_schema,table_name) 
not in(select distinct table_schema,table_name from information_schema.STATISTICS where INDEX_NAME=‘PRIMARY‘ ) 
and table_schema not in ( ‘sys‘,‘mysql‘,‘information_schema‘,‘performance_schema‘);
Copier après la connexion

4 Vérifiez quels champs de l'instance peuvent être nuls

select TABLE_SCHEMA,TABLE_NAME from COLUMNS where IS_NULLABLE=‘YES‘ and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘mysql‘, ‘sys‘)\G
Copier après la connexion

5. quelles procédures et fonctions stockées sont dans l'instance

#存储过程
select ROUTINE_SCHEMA,ROUTINE_NAME,ROUTINE_TYPE
from information_schema.ROUTINES
where ROUTINE_TYPE=‘PROCEDURE‘ and ROUTINE_SCHEMA not in (‘mysql‘,‘sys‘,‘information_schema‘,‘performance_schema‘);
#函数
select ROUTINE_SCHEMA,ROUTINE_NAME,ROUTINE_TYPE 
from information_schema.ROUTINES 
where ROUTINE_TYPE=‘FUNCTION‘ and ROUTINE_SCHEMA not in (‘mysql‘,‘sys‘,‘information_schema‘,‘performance_schema‘);
Copier après la connexion

6. Vérifiez quels jeux de caractères de champ de table dans l'instance sont incohérents avec le jeu de caractères par défaut

select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,CHARACTER_SET_NAME 
from information_schema.COLUMNS 
where (CHARACTER_SET_NAME is null or CHARACTER_SET_NAME <> ‘utf8‘) 
and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘test‘,‘mysql‘,‘sys‘);
Copier après la connexion

7. dans l'instance sont incompatibles avec la valeur par défaut

Afficher le jeu de caractères actuel et les paramètres des règles de relecture

show variables like ‘collation_%‘;
select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,CHARACTER_SET_NAME,COLLATION_NAME 
from information_schema.COLUMNS 
where (COLLATION_NAME is null or COLLATION_NAME <> ‘utf8_general_ci‘) 
and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘test‘,‘mysql‘,‘sys‘);
Copier après la connexion

8 Vérifiez quels comptes disposent d'autorisations autres que la sélection, la mise à jour et l'insertion

select GRANTEE,PRIVILEGE_TYPE,concat(TABLE_SCHEMA,‘-‘,TABLE_NAME,‘-‘,COLUMN_NAME) from COLUMN_PRIVILEGES where PRIVILEGE_TYPE not in (‘select‘,‘insert‘,‘update‘)
union 
select GRANTEE,PRIVILEGE_TYPE,TABLE_SCHEMA from SCHEMA_PRIVILEGES where PRIVILEGE_TYPE not in (‘select‘,‘insert‘,‘update‘)
union
select GRANTEE,PRIVILEGE_TYPE,concat(TABLE_SCHEMA,‘-‘,TABLE_NAME) from TABLE_PRIVILEGES where PRIVILEGE_TYPE not in (‘select‘,‘insert‘,‘update‘) 
union
select GRANTEE,PRIVILEGE_TYPE,concat(‘user‘) from USER_PRIVILEGES where PRIVILEGE_TYPE not in (‘select‘,‘insert‘,‘update‘);
Copier après la connexion
<. 🎜>9. Vérifiez quelles tables de l'instance ne sont pas le moteur de stockage par défaut. Le moteur de stockage par défaut est innodb par exemple

select TABLE_NAME,ENGINE 
from information_schema.tables 
where ENGINE!=‘innodb‘ and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘test‘,‘mysql‘, ‘sys‘);
Copier après la connexion

10. Vérifiez quelles tables de l'instance ont des clés étrangères

select a.TABLE_SCHEMA,a.TABLE_NAME,a.CONSTRAINT_TYPE,a.CONSTRAINT_NAME,b.REFERENCED_TABLE_NAME,b.REFERENCED_COLUMN_NAME 
from information_schema.TABLE_CONSTRAINTS a LEFT JOIN information_schema.KEY_COLUMN_USAGE b 
ON a.CONSTRAINT_NAME=b.CONSTRAINT_NAME where a.CONSTRAINT_TYPE=‘FOREIGN KEY‘;
Copier après la connexion
<🎜. >11. Vérifiez quels champs de table dans l'instance ont des mises à jour en cascade

select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,REFERENCED_TABLE_SCHEMA,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME 
from information_schema.KEY_COLUMN_USAGE 
where REFERENCED_TABLE_SCHEMA is not null 
and REFERENCED_TABLE_NAME is not null 
and REFERENCED_COLUMN_NAME is not null and table_schema not in (‘information_schema‘,‘performance_schema‘,‘test‘,‘mysql‘, ‘sys‘);
Copier après la connexion

12. Comment filtrer les informations de connexion dans l'instance actuelle en fonction du nom d'utilisateur, de l'heure de connexion, du SQL exécuté, etc.

select USER,HOST,DB from processlist where TIME>2;
Copier après la connexion

13. Afficher les tables sans index dans la base de données

select TABLE_SCHEMA,TABLE_NAME from information_schema.tables 
where TABLE_NAME  not in (select distinct(any_value(TABLE_NAME))  from information_schema.STATISTICS group by INDEX_NAME) 
and TABLE_SCHEMA not in (‘sys‘,‘mysql‘,‘information_schema‘,‘performance_schema‘);
Copier après la connexion

14. Afficher les tables dans la base de données Table indexée, quels index ont été établis

Afficher les résultats : nom de la bibliothèque, nom de la table, index nom

select TABLE_SCHEMA,TABLE_NAME,group_concat(INDEX_NAME) 
from  information_schema.STATISTICS where TABLE_SCHEMA not in (‘sys‘,‘mysql‘,‘information_schema‘,‘performance_schema‘)  group by TABLE_NAME ;
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!