有没有一种方法可以查询数据库以找出所有表中有多少行?
即
table1 1234
table2 222
table3 7888
希望你能指教
Answers:
SELECT
TABLE_NAME,
TABLE_ROWS
FROM
`information_schema`.`tables`
WHERE
`table_schema` = 'YOUR_DB_NAME';
SELECT SUM(TABLE_ROWS) FROM information_schema.tables WHERE table_schema = 'YOUR_DATABASE_NAME' && (TABLE_NAME='table1' || TABLE_NAME='table2')
TABLE_ROWS
可能与当前表的内容不同步,但是您可以通过运行ANALYZE
;)进行更新。
ORDER BY TABLE_ROWS DESC
检测一下猪。
将上面的信息和此信息合成为一组查询,我们得到了一个自写查询,该查询将提供准确的行数:
SET @tableSchema = 'my_schema';
SET SESSION group_concat_max_len = 10000000;
SET @rowCounts = (
SELECT group_concat(CONCAT('SELECT ''',TABLE_NAME,''', COUNT(*) FROM ', TABLE_NAME) SEPARATOR ' union all ')
FROM information_schema.tables WHERE table_schema = @tableSchema
);
PREPARE statement FROM @rowCounts;
EXECUTE statement;
-- don't run dealloc until you've exported your results ;)
DEALLOCATE PREPARE statement;
使用存储过程来获取表的行很方便。例如:
CALL database_tables_row_count('my_shop_db');
将显示:
+-------------------+-----------+
| table | row_count |
+-------------------+-----------+
| user | 5 |
| payment | 12 |
+-------------------+-----------+
如果'my_shop_db'中没有表,您将得到:
Empty set (0.00 sec)
如果您拼写错误的数据库名称,则会得到:
ERROR 1049 (42000): Unknown database 'my_so_db'
就像您发表声明一样 use non_existing_db;
该存储过程必须存储在某个地方(在数据库中)。如果将其存储到当前数据库中,则可以通过这种方式使用
CALL database_tables_row_count('my_shop_db');
只要使用存储该过程的当前数据库,就可以获得有关任何数据库的结果
由于诸如表行计数之类的查询非常普遍,因此您可能希望将该过程存储在例如的公共数据库(一种工具箱)中admin
。要在新数据库中创建存储过程:
CREATE DATABASE IF NOT EXISTS `admin`;
然后切换到它:
USE `admin`;
并创建存储过程:
DROP PROCEDURE IF EXISTS `database_tables_row_count`;
DELIMITER $$
CREATE PROCEDURE `database_tables_row_count`(IN tableSchema VARCHAR(255))
BEGIN
DECLARE msg VARCHAR(128);
IF (SELECT COUNT(TABLE_NAME) FROM information_schema.tables WHERE table_schema = `tableSchema`) = 0 THEN
SET msg = CONCAT('Unknown database \'', `tableSchema`, '\'');
SIGNAL SQLSTATE '42000' SET MESSAGE_TEXT = msg, MYSQL_ERRNO = 1049;
END IF;
SET SESSION group_concat_max_len = 10000000;
SET @rowCounts = (
SELECT group_concat(CONCAT('SELECT ''',TABLE_NAME,''' AS `table`, COUNT(*) AS `row_count` FROM ', `tableSchema`, '.', TABLE_NAME) SEPARATOR ' union all ')
FROM information_schema.tables WHERE table_schema = `tableSchema`
AND TABLE_TYPE = 'BASE TABLE'
);
IF @rowCounts IS NOT NULL THEN
PREPARE statement FROM @rowCounts;
EXECUTE statement;
DEALLOCATE PREPARE statement;
ELSE
# if no base tables found then return an empty set
select 1 where 0 = 1;
END IF;
END$$
DELIMITER ;
然后在当前数据库中使用它:
CALL admin.database_tables_row_count('my_shop_db');
得到结果。
不需要创建一个单独的数据库来保存该过程,但是我发现拥有一个专用的数据库作为一种工具箱很有用,因此我不需要为每次开发和每次都重新创建过程/视图/功能。在我使用删除数据库后...
您可能要更改此行:
AND TABLE_TYPE = 'BASE TABLE'
至:
AND TABLE_TYPE IN ('BASE TABLE', 'VIEW')
如果您想获得行数,则视图也是如此。
运行此查询a以获得结果,Information Schema将不会给出正确的结果。
Select group_concat(Query SEPARATOR ' union all ') as Full_Query from (SELECT CONCAT('SELECT ''',table_name,''', COUNT(*) FROM ', table_name) as Query
FROM information_schema.tables) AS T1 into @sql from (select
table_schema db,
table_name tablename from information_schema.tables where table_schema not in
('performance_schema', 'mysql', 'information_schema')) t;
然后运行
prepare s from @sql; execute s; deallocate prepare s;