MySQL Forums
Forum List  »  Performance

Query with 2 x subquery
Posted by: Jes Ramsing
Date: April 29, 2016 06:44AM

I have a query that is bugging me. I "just" need the latest recorded values within for a given partid before a certain sessionid

SELECT sessionid, varid, val FROM data_i16 WHERE ROW(sessionid, varid) IN
(SELECT MAX(sessionid) as sessionid, varid FROM data_i16 WHERE sessionid IN
(SELECT sessionid FROM session WHERE partid='5' AND sessionid<1611659) GROUP BY varid ORDER BY sessionid DESC);

Here is the EXPLAIN - the type ALL in the outermost query seems to be the killer.

1 PRIMARY data_i16 ALL 19945718 Using where
2 SUBQUERY session range PRIMARY,Index 1 PRIMARY 8 946256 Using where; Using temporary; Using filesort
2 SUBQUERY data_i16 ref PRIMARY,ui16_idx ui16_idx 8 nodejstest.session.sessionid 5 Using index

The problem is that the query is taking roughly 30s to deliver the correct 108 rows out of 16M rows.
Running the two subquerys isolated results in subsecond returns.

I am open to suggestions, and a "not possible to optimize is also interesing.

Best regards

Options: ReplyQuote


Subject
Views
Written By
Posted
Query with 2 x subquery
779
April 29, 2016 06:44AM
430
April 29, 2016 08:01AM
406
May 01, 2016 11:57PM
374
May 02, 2016 05:32AM
383
May 01, 2016 11:58PM
421
May 02, 2016 02:16AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.