japem
japem

Reputation: 1111

Duplicate entry for key 'group_key'

I'm running a query in MySQL where I'm grouping by four columns and selecting a lot of things, including the four values I'm grouping by. But I get this error when I run the query:

Duplicate entry '92-0-10-7' for key 'group_key'

The 92, 0, 10, and 7 are all values of the four grouped-by columns for a particular row (actually, I checked, and there are 63 rows that fit those parameters). But I have no idea what that means, or how to run my query properly.

My query:

Create table table2 as
Select round(x, 0) as x, round(a, 0) as a, round(b, 0) as b, c, d, e, f
from table1
group by round(x, 0), round(a, 0), round(b, 0), c

The 92 would fit x, the 0 a, the 10 b, and the 7 c.

I have no indices on this table.

Thanks!

Upvotes: 13

Views: 14102

Answers (4)

PeterKA
PeterKA

Reputation: 24638

Your issue seems like a MySQL bug. I was reviewing the logs for our production server the other day and the error logs were filled with this error. I used the workaround below and the errors are gone.

SET SESSION max_heap_table_size=536870912;

SET SESSION tmp_table_size=536870912;

Source: http://bugs.mysql.com/bug.php?id=58081

Upvotes: 15

Mike Volmar
Mike Volmar

Reputation: 2093

I had this issue on a table with approx 150k rows and simply added an index on the field I wanted to group by and this error stopped

Upvotes: 0

lsd
lsd

Reputation: 670

You should put the alias names in the GROUP BY like this:

CREATE TABLE table2 AS
SELECT ROUND(x, 0) AS x, ROUND(a, 0) AS a, ROUND(b, 0) AS b, c, d, e, f
FROM table1
GROUP BY x, a, b, c;

Upvotes: 0

Mostafa Nazari
Mostafa Nazari

Reputation: 716

This is usually disk space issue. Specially temporary space (in linux /tmp). Check you database server Disk space.

Upvotes: 2

Related Questions