Search code examples
mysqldistinct

Does DISTINCT have a length limit?


I store images in my DB like base64 strings.

DB is normalized and I use DISTINCT to escape duplicates in cases when I use JOINs.

The problem is that my query returns only one image.

These strings are same in beginning, maybe it is a metadata or something else.

Does DISTINCT have a length limit?

Could it be that DISTINCT truncated these strings to the max length, compared them and decided that they were the same?

EDIT

I use TEXT data type for images column. Strings are ~4000 characters long, so I think thats would be enough.

The query is

SELECT goods.id, goods.title, goods.description, goods.image, goods.price, goods.title, 
GROUP_CONCAT(DISTINCT ingredients.title) AS ingredients_title, 
GROUP_CONCAT(DISTINCT ingredients.description) AS ingredients_description, 
GROUP_CONCAT(DISTINCT ingredients.image) AS ingredients_image, 
GROUP_CONCAT(DISTINCT composition.title) AS composition_title, 
GROUP_CONCAT(DISTINCT goods_composition.value) AS goods_compositionvalue FROM goods 
INNER JOIN goods_ingredients ON goods_ingredients.good_id = goods.id 
INNER JOIN ingredients ON goods_ingredients.ingredient_id = ingredients.id 
INNER JOIN goods_composition ON goods_composition.good_id = goods.id 
INNER JOIN composition ON goods_composition.composition_id = composition.id 
WHERE goods.id = :goods_id

Solution

  • GROUP_CONCAT() itself has a length limit.

    It's configured by the option group_concat_max_len. The default is 1024 bytes.

    This is independent of whether you use DISTINCT inside the call.