MySQL - 查找type = mediumtext和max(char_length)< 255的列

时间:2016-05-17 21:33:25

标签: mysql

我遇到的情况是由于列数太多而导致最大行数达到最大值。

我找到了一种方法来列出我打算转换为varchar(200或更小)的mediumtext类型的列和最大字符,以减少占用空间。

我想进一步优化此查询,只显示少于255个字符的任何媒体文本。

SELECT CONCAT(
GROUP_CONCAT(
CONCAT('(SELECT \'',COLUMN_NAME,'\' AS `column`,MAX(CHAR_LENGTH(`',COLUMN_NAME,'`)) AS `max_length` ','FROM`',TABLE_SCHEMA,'`.`',TABLE_NAME,'` ORDER BY `max_length` DESC LIMIT 1)')
SEPARATOR ' UNION ALL '
), ';'
) AS _SQL
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'myTable'
AND COLUMN_TYPE='mediumtext';

我一直用;取代order by max_length desc;以获得我需要的东西,然后消除我不想要的东西。不过,我想把它搞清楚。

这是我现在得到的输出。

| column                   | max_length |
+--------------------------+------------+
| a_str_7                  |        291 |
| description              |        268 |
| a_str_8                  |        160 |
| a_str_9                  |         93 |
| close_notes              |         46 |
| a_str_6                  |          2 |
| comments                 |       NULL |
| work_notes               |       NULL |
| group_list               |       NULL |
| a_str_10                 |       NULL |
| comments_and_work_notes  |       NULL |
| additional_assignee_list |       NULL |
| user_input               |       NULL |
| a_str_11                 |       NULL |
| work_notes_list          |       NULL |
| approval_history         |       NULL |
| watch_list               |       NULL |
| a_str_12                 |       NULL |
| mgt_only                 |       NULL |

0 个答案:

没有答案