64

I've created a view which uses GROUP_CONCAT to concatenate results from a query on products column with data type of 'varchar(7) utf8_general_ci' in a column named concat_products.

The problem is that MySQL truncates value of "concat_products" column. phpMyAdmin says the data type of "concat_products" column is varchar(341) utf8_bin

Table products:

CREATE TABLE `products`(
    `productId` tinyint(2) unsigned NOT NULL AUTO_INCREMENT, 
    `product` varchar(7) COLLATE utf8_general_ci NOT NULL, 
    `price` mediumint(5) unsigned NOT NULL, 
    PRIMARY KEY (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci

The "concat_products_vw" view:

CREATE VIEW concat_products_vw AS
SELECT
  `userId`,
    GROUP_CONCAT(CONCAT_WS('_', `product`, `productId`, `price`) 
        ORDER BY `productId` ASC SEPARATOR '*') AS concat_products
FROM
  `users`
LEFT JOIN `products` 
ON `users`.`accountBalance` >= `product`.`price`
GROUP BY `productId` 

According to MySQL manual:

Values in VARCHAR columns are variable-length strings
Length can be specified as a value from 1 to 255 before MySQL 4.0.2 and 0 to 255 as of MySQL 4.0.2.


EDIT

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535.

  1. Why MySQL specifies more than 255 characters for varchar "concat_products" column? (solved!)

  2. Why uf8_bin instead of utf8_general_ci?

  3. Is it possible to change the data type of a column in a view for example in my case to text for "concat_products" column?

  4. If not what can I do to prevent MySQL from truncating "concat_products" column?

informatik01
  • 16,038
  • 10
  • 74
  • 104
pouya
  • 3,400
  • 6
  • 38
  • 53
  • What version of MySQL are you using? – gbn Aug 17 '12 at 08:01
  • Double-check your sources. The [MySQL manual](http://dev.mysql.com/doc/refman/5.5/en/char.html) says: Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. – Jocelyn Aug 17 '12 at 09:42
  • @Jocelyn you're right so why truncating takes place here if mysql can store up to 65535 characters in a varchar column? – pouya Aug 17 '12 at 09:51

3 Answers3

97

As I already wrote in an earlier comment, the MySQL manual says:

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535.

So the problem is not with the data type of the field.

The MySQL manual also says:

The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer: SET [GLOBAL | SESSION] group_concat_max_len = val;

Your options for changing the value of group_concat_max_len are:

  1. changing the value at MySQL startup by appending this to the command:
    --group_concat_max_len=your_value_here
  2. adding this line in your MySQL configuration file (mysql.ini): group_concat_max_len=your_value_here
  3. running this command after MySQL startup:
    SET GLOBAL group_concat_max_len=your_value_here;
  4. running this command after opening a MySQL connection:
    SET SESSION group_concat_max_len=your_value_here;

Documentation: SET, Server System Variables: group_concat_max_len

Jocelyn
  • 11,209
  • 10
  • 43
  • 60
  • thanks mysql doesn't truncate the concat_products column anymore – pouya Aug 17 '12 at 10:15
  • 4
    I personally use the 4th option I listed, because there are only a few of my scripts that need to compute "big" GROUP_CONCAT, and these scripts don't run very often. – Jocelyn Aug 17 '12 at 10:18
14

As Jocelyn mentioned, the size of a GROUP_CONCAT() result is bounded by group_concat_max_len, however there is an additional interaction with ORDER BY that results in a further truncation to 1/3 of group_concat_max_len. For an example, see this related answer.

The default value for group_concat_max_len is 1024, and 1024 / 3 = 341 probably explains why the type of concat_products shows up as varchar(341) in the original example. If you were to remove the GROUP BY productId clause, concat_products should show up as varchar(1024).

I have not found this interaction between GROUP_CONCAT() and ORDER BY mentioned in the MySQL Manual, but it affects at least MySQL Server 5.1.

Community
  • 1
  • 1
JoshS
  • 351
  • 3
  • 5
0

Year 2023, on Linux Server you can edit my.cnf file and then set:

[mysqld]
group_concat_max_len = 2048
Ale DC
  • 1,646
  • 13
  • 20