Cache/Re-Use a Subquery in MySQL

前端 未结 2 1379
太阳男子
太阳男子 2020-12-30 05:37

I have a very complex MySQL query that includes use of the same subquery three times. Will MySQL actually run the subquery three times? (It\'s an expensive one.) If so, is t

2条回答
  •  感动是毒
    2020-12-30 06:02

    For whatever reason, mysql IN clauses with a sub-select run very slow. Better to use join. Your sub-query becomes:

    SELECT id from programs P1 INNER JOIN programs P2 ON P1.id = P2.id INNER JOIN playlist_program_map PMAP ON P2.id = PMAP.program_id WHERE P1.submitter_id=32 AND P2.feed_id=2478 AND PMAP.playlist_id=181

    It will run much faster.

提交回复
热议问题