[Solved] 2 different small query vs 1 query with subquery

EverSQL Database Performance Knowledge Base

2 different small query vs 1 query with subquery

I have table like this

name       | personal_number 
-----------------------------------------
Jon        | 222
Alex       | 555
Jon        | 222
Jimmy      | 999

I need get every name, which personal_number repeates in table more than 1, that is result must be:

 Jon        
 Jon        

So, Variant 1):

SELECT  name  FROM mytable WHERE personal_number IN (
        SELECT  personal_number  FROM mytable  GROUP BY personal_number
        HAVING COUNT(*) > 1
)

Variant 2):

SELECT  personal_number  FROM mytable  GROUP BY personal_number
        HAVING COUNT(*) > 1
)

Then, using php, retrieved personal_numbers join as string (soemthing like this '222', '222' ) and run other query

SELECT  name FROM mytable  WHERE personal_number IN( here joined string )

Variant 2 works approximately 10 times faster, than variant 1, this is surprise for me, I was thinking that one query will be faster, but...

(In table is 500 000 rows, column personal_number not indexed)

So, what you mean about cases like this? why variant 2 is many faster than variant 1 ?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
  3. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `mytable` ADD INDEX `mytable_idx_personal_number` (`personal_number`);
The optimized query:
SELECT
        mytable.name 
    FROM
        mytable 
    WHERE
        EXISTS (
            SELECT
                1 
            FROM
                mytable AS mytable1 
            WHERE
                (
                    mytable.personal_number = mytable1.personal_number
                ) 
            GROUP BY
                mytable1.personal_number 
            HAVING
                COUNT(*) > 1 
            ORDER BY
                NULL
        )

Related Articles



* original question posted on StackOverflow here.