Question : How can I run this query faster?

This is a very interesting SQL problem. I wish I can assign more than 500 points to this question !

I have the following query
 
1:
2:
3:
4:
5:
6:
7:
8:
SELECT p1.idpattern , p2.idpattern , COUNT(p1.idtag) count1 , (SELECT COUNT(*) FROM rsdb1.positivepatterns20 PI WHERE pi.iduser = 3  AND pi.idpattern = p2.idpattern ) count2 
FROM positivepatterns20 p1, positivepatterns20 p2
WHERE p1.idtag = p2.idtag
AND p1.iduser = 1 
AND p2.iduser = 3 
GROUP BY 1,2
HAVING count1 < count2
AND count1 = (SELECT COUNT(*) FROM positivepatterns20 P1_sub WHERE P1_sub.iduser = 1 AND P1_sub.idpattern = P1.idpattern);


it takes like 10 minutes to run
Is there a way I can make it faster?
Here is table positivepatterns20 describtion, number of rows is only 148000 rows!
 
1:
2:
3:
4:
5:
6:
7:
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| iduser    | int(11) | NO   | PRI | NULL    |       |
| idpattern | int(11) | NO   | PRI | 0       |       |
| idtag     | int(11) | NO   | PRI | 0       |       |
+-----------+---------+------+-----+---------+-------+



-------------
Not to be rude, I DO NOT WANT  "copy paste links" answers

Answer : How can I run this query faster?

There were some missing group by's in there, please try this one.

SELECT p1.idpattern , p2.idpattern , COUNT(distinct p1.idtag) count1 , pi.Count2, count3
FROM positivepatterns20 p1
inner join positivepatterns20 p2 on p1.idtag = p2.idtag and p2.iduser = 3
inner join (
  SELECT P1_sub.idpattern, COUNT(distinct idtag) as Count3 FROM positivepatterns20 P1_sub
  WHERE P1_sub.iduser = 1
  GROUP BY P1_sub.idpattern) P1_sub on P1_sub.idpattern = P1.idpattern
inner join (
  SELECT pi.idpattern, COUNT(distinct idtag) as Count2 FROM rsdb1.positivepatterns20 pi
  WHERE pi.iduser = 3
  group by pi.idpattern) Pi on pi.idpattern = p2.idpattern
WHERE p1.iduser = 1
GROUP BY p1.idpattern , p2.idpattern, pi.count2, count3
HAVING COUNT(distinct p1.idtag) < count2 and COUNT(distinct p1.idtag) = count3
Random Solutions  
 
programming4us programming4us