MySQL Determine longest VarChar length

前端 未结 4 1630
情话喂你
情话喂你 2021-02-08 23:05

I am trying to optimize my database. In order to do so, I need to be able to determine the longest data entry in a varchar column, and then trim the column definition to just a

相关标签:
4条回答
  • 2021-02-08 23:31

    The back ticks and the char_length function in Aurelio's answer both cause errors. This answer works.

    0 讨论(0)
  • 2021-02-08 23:35

    I will post the answer in the question I made:

    SELECT CHARACTER_MAXIMUM_LENGTH
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'Database'
        AND TABLE_NAME = 'Table'
        AND COLUMN_NAME = 'Field'
    
    0 讨论(0)
  • 2021-02-08 23:49

    You can do this by using the following statement

    SELECT column_name FROM database.table PROCEDURE ANALYSE(1,1);
    

    The Max_length value of the results from this will give you the longest varchar in that column.

    So in this case assuming you want to investigate the output column then you can run the following

    SELECT output FROM poller_output PROCEDURE ANALYSE(1,1);
    

    The advantage of this is that it will also give you the optimal fieldtype in the return values which will be something along the lines of

    VARCHAR(56) NOT NULL
    
    0 讨论(0)
  • 2021-02-08 23:50

    If you want to know the max length of the field output, for example, you can use this query:

    SELECT Max(CHAR_LENGTH(`output`)) AS Max FROM `poller_output`
    
    0 讨论(0)
提交回复
热议问题