Comment répertorier toutes les tables de la base de données actuelle, ainsi que le nombre de lignes de la table.
En d'autres termes, pouvez-vous penser à une requête pour trouver quelque chose comme ça dans mysql?
+------------------------++------------------------+
| Tables_in_database | Number of rows |
+------------------------++------------------------+
| database 1 | 1000 |
| database 2 | 1500 |
+------------------------++------------------------+
Différentes approches sont les bienvenues.
J'ai une approche très agressive en utilisant Dynamic Force Dynamic SQL
SET group_concat_max_len = 1024 * 1024 * 100;
SELECT CONCAT('SELECT * FROM (',GROUP_CONCAT(CONCAT('SELECT ',QUOTE(tb),' Tables_in_database,
COUNT(1) "Number of Rows" FROM ',db,'.',tb) SEPARATOR ' UNION '),') A;')
INTO @sql FROM (SELECT table_schema db,table_name tb
FROM information_schema.tables WHERE table_schema = DATABASE()) A;
PREPARE s FROM @sql; EXECUTE s; DEALLOCATE PREPARE s;
Exemple: dans ma base de données de test, j'obtiens ceci
mysql> use test
Database changed
mysql> SET group_concat_max_len = 1024 * 1024 * 100;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT CONCAT('SELECT * FROM (',GROUP_CONCAT(CONCAT('SELECT ',QUOTE(tb),' Tables_in_database,
'> COUNT(1) "Number of Rows" FROM ',db,'.',tb) SEPARATOR ' UNION '),') A;')
-> INTO @sql FROM (SELECT table_schema db,table_name tb
-> FROM information_schema.tables WHERE table_schema = DATABASE()) A;
Query OK, 1 row affected (0.00 sec)
mysql> PREPARE s FROM @sql; EXECUTE s; DEALLOCATE PREPARE s;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
+--------------------+----------------+
| Tables_in_database | Number of Rows |
+--------------------+----------------+
| biblio | 3 |
| biblio_old | 7 |
| dep | 5 |
| e | 14 |
| emp | 4 |
| fruit | 12 |
| fruit_outoforder | 12 |
| nums_composite | 0 |
| nuoji | 4 |
| prod | 3 |
| prodcat | 6 |
| test2 | 9 |
| worktable | 5 |
| yoshi_scores | 24 |
+--------------------+----------------+
14 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql>
CAVEAT: Si toutes les tables sont MyISAM, cela se produira très rapidement. Si toutes les tables sont InnoDB, chaque table sera comptée. Cela peut être brutal et implacable pour les très grandes tables InnoDB.
Essayez la requête ci-dessous sans requête dynamique
SELECT Table_name AS TablesInDatabase ,table_rows AS NumberOfRows
FROM information_schema.tables
WHERE Table_schema=DATABASE();
Obtenez le nombre exact de lignes pour toutes les tables dans MySQL en utilisant le script Shell.
définir le paramètre dans le fichier parameter.config comme
# Server Details
Host_server=10.109.25.37
# Mysql credentials
mysql_user=root
mysql_pass=root
mysql_port=3306
Le script à compter est:
#!/bin/bash
# This Script is used to take rows count of mysql all user database
# Read Parameter
source parameter.config
# Find path
MY_PATH="`dirname \"$0\"`"
#echo "$MY_PATH"
start=`date +%s`
echo -e "\n\n"
echo MySQL script start runing at Date and Time is: `date +"%D %T"`
echo -e "@@@ Start of rows Count of MySQL on Old Ficus Release $Host_server @@@"
echo -e "\n***** MySQL Rows Count Start *****"
#Make directory to save rows count
NOW="$(date +%Y%m%dT%H%M%S)"
dir_name="mysqlRows_$Host_server"
if [ ! -d "$MY_PATH/$dir_name" ]; then
mkdir "$MY_PATH/$dir_name"
fi
echo -e "\n..... Directory $dir_name is Created for mysql....."
echo -e "\n..... Check MySQL Connection ....."
# Verifying mysql connections on new release machine
MYSQL_CONN="-u$mysql_user -p$mysql_pass -h$Host_server"
mysql ${MYSQL_CONN} -e "exit"
if [ $? -eq 0 ];
then
echo -e "\n..... MySQL Database Connection Successful on server $Host_server ....."
else
echo -e "\n..... MySQL Database Connection Fail. Please verify $Host_server credential ....."
exit
fi
echo -e "\nReading MySQL database names..."
mysql ${MYSQL_CONN} -ANe "SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('mysql','information_schema','performance_schema')" > $MY_PATH/$dir_name/dbs_$NOW.txt
DBS="$(cat $MY_PATH/$dir_name/dbs_$NOW.txt)"
# echo -e "\nList of databases:\t" ${DBS}
echo -e "\n..... Running for row count of tables of all user databases ....."
# All User databases
for db in ${DBS[@]}
do
# echo $db , ${db[@]}
# Find list of database
echo -e "\n\t... Running for ${db[@]} database tables list ..."
mysql ${MYSQL_CONN} -ANe "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA IN ('${db[@]}')" > $MY_PATH/$dir_name/${db[@]}_tables_$NOW.txt
TBL="$(cat $MY_PATH/$dir_name/${db[@]}_tables_$NOW.txt)"
# echo "Table in $db are:" ${TBL[@]}, $MY_PATH/$dir_name/${db[@]}_tables.txt
echo -e "\n\t... Running for ${db[@]} database tables rows count ..."
for tbs in ${TBL[@]}
do
# echo $tbs , ${tbs[@]}
count=$(mysql -u$mysql_user -p$mysql_pass -h$Host_server ${db[@]} -N -e "select count(*) from ${tbs[@]}")
# count="$(cat /tmp/$db_rows_$NOW.txt)"
# echo "Row in $tb Table of $db database are:" ${count[@]}
echo -e "${db[@]},${tbs[@]},$count" >> $MY_PATH/$dir_name/${db[@]}_rows_$NOW.csv
echo -e "${db[@]},${tbs[@]},$count" >> $MY_PATH/$dir_name/alldbs_rows_$NOW.csv
done
done
echo -e "\n..... End of rows count of tables of all databases ....."
echo -e "\n===== MySQL Rows Count End ====="
# Display script execution time.
echo -e "@@@ Completion of Rows Count of MySQL on old Release $Host_server @@@"
echo Script ended at Date and Time is: `date +"%D %T"`
end=`date +%s`
runtime=$((end-start))
echo -e "Time(in second) taken for running MySQL row count script:" $runtime "Sec."
enregistrez-le dans le fichier "mysqlrowscount.sh", exécutez ce script en utilisant la commande:
bash mysqlrowscount.sh
Peut-être que cette requête peut être utile. Il montre la taille des données et le nombre d'enregistrements.
SET @table=(SELECT DATABASE());
select @table;
SELECT
table_schema as `Database`,
table_name AS `Table`,
round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB`,
table_rows as 'Rows'
FROM information_schema.TABLES
WHERE table_schema = @table
ORDER BY (data_length + index_length) DESC;