2

I have a (what it seems to me) simple query which fails to run on a simple table, the error is:

Out of sort memory, consider increasing server sort buffer size

These are the SQL statements which I'm trying to run:

CREATE TABLE `permissions` (
  `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `name` (`name`)

)ENGINE=InnoDB
AUTO_INCREMENT=11 CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci';

INSERT INTO `permissions` (`id`, `name`) VALUES
  (1,'ocjena_dobavljaca.view'),
  (2,'ocjena_dobavljaca.edit'),
  (3,'ocjena_dobavljaca.delete'),
  (4,'zaposlenici.view'),
  (5,'zaposlenici.edit'),
  (6,'zaposlenici.delete'),
  (7,'vatrogasni_aparati.view'),
  (8,'vatrogasni_aparati.edit'),
  (9,'vatrogasni_aparati.delete'),
  (10,'organizacijska_shema_drustva');
COMMIT;

SELECT 
    SUBSTRING_INDEX(name, '.', 1) as n, 
    COUNT(*) as num,
    GROUP_CONCAT(name)as nn
FROM permissions
GROUP BY n

You can check it in this SQL Fiddle.

This answer states:

While raising sort_buffer_size can help queries queries with GROUP BYs and ORDER BYs, you are better off improving the queries that you can improve and adding indexes that can be used by the Query Optimizer.

but I don't see any way to improve the query or add indexes to the table, as they are quite simple.

Increasing the sort_buffer_size from 64K (default) to 256K solves the issue, but I'm not sure if this is the right solution.
The other solution is to make the query with a subquery (two SELECTs), which seems a bit redundant:

SELECT 
  n,
  COUNT(*) as num,
  GROUP_CONCAT(name)as nn
FROM (
    SELECT 
        SUBSTRING_INDEX(name, '.', 1) as n, 
        name 
    FROM permissions) a
GROUP BY n;

Can anyone explain why the error?

  • Get rid of UNIQUE KEY id (id), -- It is redundant because the PK is a UNIQUE key. – Rick James Feb 21 '18 at 23:40
  • @RickJames The DDL was automatically generated by a gui app – ekstrakt Feb 22 '18 at 0:09
0

It looks like your name column concatenates two items. Try building the table with the column split.

CREATE TABLE `permissions` (
  `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `object` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `permission` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  UNIQUE KEY `object_permission` (`object`, `permission`)
d
)ENGINE=InnoDB
AUTO_INCREMENT=11 CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci';

INSERT INTO `permissions` (`id`, `name`) VALUES
  (1,'ocjena_dobavljaca','view'),
  (2,'ocjena_dobavljaca','edit'),
  (3,'ocjena_dobavljaca','delete'),
  (4,'zaposlenici','view'),
  (5,'zaposlenici','edit'),
  (6,'zaposlenici','delete'),
  (7,'vatrogasni_aparati','view'),
  (8,'vatrogasni_aparati','edit'),
  (9,'vatrogasni_aparati','delete'),
  (10,'organizacijska_shema_drustva','');
COMMIT;

SELECT 
    object, 
    COUNT(*) as num,
    GROUP_CONCAT(permission)as nn
FROM permissions
GROUP BY object;
  • I mentioned in my questions that I'm aware of other solutions (splitting the column or moving the object to a different table is one of them), but I wanted to avoid complicating a seemingly simple query. And I wanted to learn why this error happens. – ekstrakt Feb 22 '18 at 0:14
  • @ekstrakt When you generate queries that are edge cases, as you have done, you can get strange results. To determine what actually is occurring you would need to examine the code or step through it with a debugger. I suspect you are getting some large buffers or join sets for each record in the original query. An explain plan may provide some initial insights. – BillThor Feb 22 '18 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.