1

I am trying to get all the entries in a session log table where a session has more than 10 entries (i.e. the count of the session_id is greater than 10). What I have right now are two select statements:

select * from log_metrics where session_id in 
( select session_id from log_metrics 
  group by session_id having count(*) > 10
)

The log_metrics table is quite large, aprox. 7,700,000 rows. The inner select takes 12.88 seconds and finds 178,000 session id's. The whole query doesn't finish running written like this, but when adding limit 10 to the end of the outer select it completes in 18 seconds, limit 100 completes in 3 min 35 sec. I tried adding the limit to the inner select but got the following error:

ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Is there a way to rewrite this query to speed things up? I only need to get about 5,000 rows from log_metrics returned, not the total 178,000 of the session id's.

Thanks for any help you might be able to give. I am new to mysql and posting so pardon any etiquette mis-steps.

4

3 に答える 3

1
select * 
from log_metrics a
inner join (select session_id from log_metrics group by session_id having count(*) > 10) b
on a.session_id = b.session_id

これがSQLフィドルです:http ://sqlfiddle.com/#!2 / 7bed6 / 3

于 2012-06-01T16:44:52.333 に答える
0

EXISTSの代わりにチェックに切り替えてみてください。IN

select * from log_metrics a where EXISTS 
( select b.session_id from log_metrics b
  where a.session_id = b.session_id
  group by b.session_id having count(*) > 10
)
于 2012-06-01T16:27:01.473 に答える
0

これが機能するかどうかはわかりませんが(mySQLのバージョンがわからないので、インスタンスはありません)、必要に応じてJOIN機能を使用しますか?

SELECT *
FROM log_metrics a
JOIN (SELECT session_id
      FROM log_metrics
      GROUP BY session_id
      HAVING COUNT(session_id) > 10
      LIMIT 5000) b
ON b.session_id = a.session_id

あなたはこれについて言及しませんでしたが、将来の質問者のために、彼が内部クエリ内にステートメントを必要とする理由はログからの合計行(50,000行以上が返される可能性があります)ではなく、(最大)5000LIMIT秒が必要なためです。session_id

于 2012-06-01T16:43:40.227 に答える