Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 different small query vs 1 query with subquery

Tags:

php

mysql

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 ?

like image 510
Oto Shavadze Avatar asked Apr 09 '13 10:04

Oto Shavadze


People also ask

Is subquery faster than two queries?

For subqueries and joins, the data needs to be combined. Small amounts can easily be combined in memory, but if the data gets bigger, then it might not fit, causing the need to swap temporary data to disk, degrading performance. So, there is no general rule to say which one is faster.

Can you have 2 subqueries in a SELECT statement?

More formally, it is the use of a SELECT statement inside one of the clauses of another SELECT statement. In fact, a subquery can be contained inside another subquery, which is inside another subquery, and so forth. A subquery can also be nested inside INSERT, UPDATE, and DELETE statements.

What is the advantage of using sub query or multi query in data manipulation?

Advantage of Using SubqueryIt would be easy to implement with a subquery, which computes the average salary. Subquery can act as a column with a single value: You can also use a subquery as a new column. The only constraint is that the subquery must return only one value.

Which SQL query is faster subquery or join?

Advantages Of Joins:The retrieval time of the query using joins almost always will be faster than that of a subquery. By using joins, you can maximize the calculation burden on the database i.e., instead of multiple queries using one join query.


2 Answers

It seems that subqueries are very slow as mentioned in this article http://www.mysqlperformanceblog.com/2010/10/25/mysql-limitations-part-3-subqueries.

You should try to avoid having subqueries and use joining instead.

like image 189
Bart Avatar answered Oct 16 '22 14:10

Bart


First query has heavy subquery. You must avoid this. The best solution for your problem is only one query:

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

This query will return you each repeated name only once. If you want to display the name of the duplicate as many times as they met you must use next query:

SELECT name, COUNT(*) AS count FROM mytable GROUP BY personal_number HAVING COUNT(*) > 1;

And then in PHP do something like this:

foreach ($rows as $row) {
  for ($i = 0; $i++; $i < $row['count']) {
    echo $row['name'] . "\n";
  }
}
like image 1
Michael Sivolobov Avatar answered Oct 16 '22 13:10

Michael Sivolobov