Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6903001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:52:12+00:00 2026-05-27T07:52:12+00:00

I’m trying to find duplicates in my users table (don’t ask, it’s a lot

  • 0

I’m trying to find duplicates in my users table (don’t ask, it’s a lot of red tape), but I’m having a problem creating an index for the query I’ve created. The table looks like:

+----------------+---------+------+-----+---------+----------------+
| Field          | Type    | Null | Key | Default | Extra          |
+----------------+---------+------+-----+---------+----------------+
| id             | int(10) | NO   | PRI | NULL    | auto_increment | 
| email          | text    | YES  | MUL | NULL    |                | 
| username       | text    | YES  | MUL | NULL    |                | 
| password       | text    | YES  |     | NULL    |                |
+----------------+---------+------+-----+---------+----------------+

There are other fields, but these are what I am searching off of. The query I wrote for finding the duplicates looks like:

SELECT COUNT(username) count,GROUP_CONCAT(id) ids,username,email,password
    FROM users
    GROUP BY username,email,password
    HAVING COUNT(username) > 1

And the index I created is:

CREATE INDEX users_id_username_password_email
    ON users id,username(64),password(64),email(64));

Unfortunately, describe doesn’t seem to be using this index:

mysql> describe SELECT COUNT(username) count,GROUP_CONCAT(id) ids,
    -> username,email,password
    -> FROM users
    -> GROUP BY username,email,password
    -> HAVING COUNT(username) > 1\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: users
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 705418
        Extra: Using filesort

So the root question is, what kind of index should I be creating to find duplicate entries on a table such as this?

Edit: Changing the order of the query to match the index did nothing:

mysql> describe SELECT COUNT(username) count,GROUP_CONCAT(id) ids,
    -> username,password,email
    -> FROM users
    -> GROUP BY username,password,email
    -> HAVING COUNT(username) > 1\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: users
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 705418
        Extra: Using filesort
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:52:13+00:00Added an answer on May 27, 2026 at 7:52 am

    There’s no point for the RDBMS to use an index if it needs to read every row anyway. The presence of an index doesn’t matter, the order of columns in the index doesn’t matter, and it wouldn’t even matter if you used FORCE INDEX.

    By analogy, if I asked you to find every occurrence of the word “the” in a book, would you use the index at the back of the book, or would you just read it cover to cover?

    Another way you can write the query is the following:

    select t1.id, t2.id from users t1 
    join users t2 using (username,password,email) 
    where t1.id<t2.id
    

    This results in the following explain plan:

    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: t1
             type: ALL
    possible_keys: PRIMARY,users_id_username_password_email
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 16516
            Extra: 
    *************************** 2. row ***************************
               id: 1
      select_type: SIMPLE
            table: t2
             type: ref
    possible_keys: PRIMARY,users_id_username_password_email
              key: users_id_username_password_email
          key_len: 201
              ref: test.t1.username,test.t1.password,test.t1.email
             rows: 82
            Extra: Using where
    

    It still does one table-scan of the users table, but it doesn’t have to sort the whole table to find the duplicates. It just has to do a key lookup.

    For what it’s worth, I tested with an index only on (username(64),email(64),password(64)). There’s no need to include id in the index, because all InnoDB indexes include the primary key column implicitly.


    Here’s another query that relies on joins to reduce the result set, and then groups by the smallest id and shows the higher id’s that are dupes. Optionally you can also return the columns by which you joined.

    select t1.id, /* t1.username, t1.password, t1.email, */ group_concat(t2.id) as dupes
    from users t1 
    join users t2 
      on (t1.username,t1.password,t1.email) = (t2.username,t2.password,t2.email) 
      and t1.id < t2.id 
    left outer join users t3
      on (t1.username,t1.password,t1.email) = (t3.username,t3.password,t3.email) 
      and t1.id > t3.id
    where t3.id is null
    group by t1.id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.