Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mysql Putting it inside a select will make it slower

I had a question while writing a query using mysql.
So I searched it, but couldn’t find an answer, so I write a question.


SELECT CAT.ID, CAT2.BIRTH, CAT.NAME
FROM CAT
INNER JOIN CAT2 ON CAT.ID = CAT2.CATID
GROUP BY CAT.ID, CAT2.BIRTH, CAT.NAME

0.032Sec

SELECT * FROM (
  SELECT CAT.ID, CAT2.AGE, CAT.NAME
  FROM CAT
  INNER JOIN CAT2 ON CAT.ID = CAT2.CATID
  GROUP BY CAT.ID, CAT.Birth, CAT.NAME
) AS AA

2.16Sec

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I just wrapped it in select, but I don’t know why it’s so slow.

When I checked with explain, the indexes ( CAT.ID, CAT.Birth, CAT.NAME) are working fine.

>Solution :

In the second query, MySQL will place the result of the subquery into memory. It then must query that data again, which takes more time. You would likely see an even larger split in performance if you were filtering in the outer query. Note that any index can only be used inside the subquery, but not on the outer query, which generally must be scanned.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading