In Mysql, I have a table with two columns (id, uuid). Then I inserted 30 million values into it. (ps: the uuid can repeated)
Now, I want to find the repeat value in the table by using Mysql grammar, but the sql spent too much time.
I want to search all columns, but it takes much time, so I tried querying first million rows, the it spent 8 seconds.
Then I tried with 10 million rows, it spend 5mins, then with 20 million rows, the server seem died.
select count(uuid) as cnt
from uuid_test
where id between 1
and 1000000
group by uuid having cnt > 1;
Anyone can help me to optimized the sql, thanks
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `uuid_test` ADD INDEX `uuid_test_idx_id` (`id`);
ALTER TABLE `uuid_test` ADD INDEX `uuid_test_idx_uuid` (`uuid`);
SELECT
count(uuid_test.uuid) AS cnt
FROM
uuid_test
WHERE
uuid_test.id BETWEEN 1 AND 1000000
GROUP BY
uuid_test.uuid
HAVING
cnt > 1
ORDER BY
NULL