MySQL Forums :: Performance :: Query with 2 x subquery


Advanced Search

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 733 Jes Ramsing 04/29/2016 06:44AM
Re: Query with 2 x subquery 412 Øystein Grøvlen 04/29/2016 08:01AM
Re: Query with 2 x subquery 388 Jes Ramsing 05/01/2016 11:57PM
Re: Query with 2 x subquery 344 Øystein Grøvlen 05/02/2016 03:45AM
Re: Query with 2 x subquery 355 Jes Ramsing 05/02/2016 05:32AM
Re: Query with 2 x subquery 358 Rick James 05/01/2016 11:58PM
Re: Query with 2 x subquery 401 Jes Ramsing 05/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.