要查询MySQL中所有表的行数,您可以使用以下方法:
DELIMITER //
CREATE PROCEDURE count_rows()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE tableName CHAR(255);
DECLARE rowCount BIGINT;
DECLARE cur CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE();
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
CREATE TEMPORARY TABLE temp_row_counts (tableName VARCHAR(255), rowCount BIGINT);
OPEN cur;
read_loop: LOOP
FETCH cur INTO tableName;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql = CONCAT('SELECT COUNT(*) INTO @rowCount FROM ', tableName);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
INSERT INTO temp_row_counts VALUES (tableName, @rowCount);
END LOOP;
CLOSE cur;
SELECT * FROM temp_row_counts;
END//
DELIMITER ;
这将创建一个名为count_rows
的存储过程,该过程将遍历当前数据库中的所有表,并计算每个表的行数。
CALL count_rows();
这将返回一个结果集,其中包含每个表的名称及其对应的行数。请注意,这种方法可能需要一些时间才能在大型数据库上完成。