我甚至不知道我的头衔是否有意义,所以希望我的解释能够弥补我的标题清晰的湖泊。
我想创建一个查询,返回有关我的表的一般信息,行数,平均长度,数据长度,最小值(id)和最大值(id)等。
我知道如何查询INFORMATION_SCHEMA.PARTITIONS
以获取'尺寸'信息。例如:
mysql> SELECT TABLE_NAME AS Name,
-> TABLE_ROWS as 'Row Count',
-> AVG_ROW_LENGTH AS 'Avg Row Len',
-> DATA_LENGTH,
-> ROUND(DATA_LENGTH / 1024 / 1024) AS 'DATA_LENGTH(M)'
->
-> FROM INFORMATION_SCHEMA.PARTITIONS
->
-> WHERE (TABLE_NAME = 'event_log'
-> OR TABLE_NAME = 'event_log_policy_entries'
-> OR TABLE_NAME = 'event_log_policy_status');
+--------------------------+-----------+-------------+-------------+----------------+
| Name | Row Count | Avg Row Len | DATA_LENGTH | DATA_LENGTH(M) |
+--------------------------+-----------+-------------+-------------+----------------+
| event_log | 10000089 | 182 | 1822425088 | 1738 |
| event_log_policy_entries | 10000137 | 171 | 1710227456 | 1631 |
| event_log_policy_status | 10000094 | 244 | 2449473536 | 2336 |
+--------------------------+-----------+-------------+-------------+----------------+
3 rows in set (0.57 sec)
mysql>
此外,我知道如何在每个表的基础上使用MIN()
和MAX()
函数来获取其最小/最大ID值。 (我的所有三个表都有一个名为id的列。)
mysql> SELECT MIN(id) AS 'Min', MAX(id) AS 'Max' from event_log;
+------+---------+
| Min | Max |
+------+---------+
| 0 | 9999999 |
+------+---------+
1 row in set (0.00 sec)
mysql>
但我不知道如何使这个 一个查询 。我的目标是拥有一个如下表所示的表:
+--------------------------+-----------+-------------+-------------+----------------+-----+-----+
| Name | Row Count | Avg Row Len | DATA_LENGTH | DATA_LENGTH(M) | Min | Max |
+--------------------------+-----------+-------------+-------------+----------------+-----+-----+
| event_log | 10000089 | 182 | 1822425088 | 1738 | # | # |
| event_log_policy_entries | 10000137 | 171 | 1710227456 | 1631 | # | # |
| event_log_policy_status | 10000094 | 244 | 2449473536 | 2336 | # | # |
+--------------------------+-----------+-------------+-------------+----------------+-----+-----+
我一直在阅读关于表JOIN和SUBQUERIES的内容,但我还没有弄清楚这一点。 (我假设这可以通过单个查询完成)。
答案 0 :(得分:0)
试试这个:
mysql> SELECT TABLE_NAME AS Name,
-> TABLE_ROWS as 'Row Count',
-> AVG_ROW_LENGTH AS 'Avg Row Len',
-> DATA_LENGTH,
-> ROUND(DATA_LENGTH / 1024 / 1024) AS 'DATA_LENGTH(M)',
-> MIN(id) AS 'Min', MAX(id) AS 'Max'
->
-> FROM INFORMATION_SCHEMA.PARTITIONS, event_log
->
-> WHERE (TABLE_NAME = 'event_log'
-> OR TABLE_NAME = 'event_log_policy_entries'
-> OR TABLE_NAME = 'event_log_policy_status');
它将返回INFORMATION_SCHEMA.PARTITIONS的过滤结果,以及event_log的所有结果。
我遇到的一个问题是,event_log表中是否有一个与INFORMATION_SCHEMA.PARTITIONS表相关的字段,以便您可以将event_log的每一行绑定到INFORMATION_SCHEMA.PARTITIONS表中的一行?
答案 1 :(得分:0)
不是堆栈溢出用户的同事向我推荐了一个示例,然后我将其推断为以下内容:
mysql> SELECT TABLE_NAME AS Name,
-> AVG_ROW_LENGTH AS 'Avg_Row_Size',
-> DATA_LENGTH AS 'Table_Size',
-> INDEX_LENGTH AS 'Index_Size',
-> elog.Min,
-> elog.Max
->
-> FROM INFORMATION_SCHEMA.PARTITIONS AS isp
->
-> LEFT JOIN
-> (SELECT 'event_log' AS 'Name',
-> MIN(id) AS 'Min',
-> MAX(id) AS 'Max'
-> FROM event_log
-> UNION
-> (SELECT 'event_log_policy_entries' AS 'Name',
-> MIN(id) AS 'Min',
-> MAX(id) AS 'Max'
-> FROM event_log_policy_entries)
-> UNION
-> (SELECT 'event_log_policy_status' AS 'Name',
-> MIN(id) AS 'Min',
-> MAX(id) AS 'Max'
-> FROM event_log_policy_status)) AS elog ON (elog.Name = isp.TABLE_NAME)
->
-> WHERE (isp.TABLE_NAME = 'event_log'
-> OR isp.TABLE_NAME = 'event_log_policy_entries'
-> OR isp.TABLE_NAME = 'event_log_policy_status');
+--------------------------+--------------+------------+------------+------+---------+
| Name | Avg_Row_Size | Table_Size | Index_Size | Min | Max |
+--------------------------+--------------+------------+------------+------+---------+
| event_log | 182 | 1822425088 | 0 | 11 | 9999999 |
| event_log_policy_entries | 170 | 1709178880 | 137019392 | 33 | 9999999 |
| event_log_policy_status | 244 | 2448392192 | 137019392 | 132 | 9999999 |
+--------------------------+--------------+------------+------------+------+---------+
3 rows in set (0.56 sec)
mysql>
它似乎与原始查询一样快,没有最小值/最大值。我使用SELECT
和UNION
创建了一个包含名称和最小/最大值的子表,然后将其连接到INFORMATION_SCHEMA.PARTITION TABLE_NAME的结果。
有更好的方法吗?这很有效,我觉得这很好。但是有更好的方法吗?