Select distinct … inner join vs. select … where id in (…)

前端 未结 3 1292
甜味超标
甜味超标 2021-01-19 12:58

I\'m trying to create a subset of a table (as a materialized view), defined as those records which have a matching record in another materialized view.

For example,

相关标签:
3条回答
  • 2021-01-19 13:35

    The second query is probably working more the harddrive than the first query (join+distinc).

    The first query will probably translates to something like:

    for each row in table Log find corresponding row in table User (in memory).

    The database is probably smart enough to create in memory structures for table User that is probably much smaller than Log table.

    I believe that query one (join+distinct) will require only one pass on table Log.

    The distinct is probably executed in memory.

    The second query probably forces the database to do multiples fulls reads on table Log.

    So in the second query you probably get:

    For each row in table user read all the rows in table Log (from disk) in order to match the condition.

    You have also to consider that some query may experience a dramatic diference in speed due to changes in memory availability, load and table increase.

    0 讨论(0)
  • 2021-01-19 13:42

    This will depend on the data you have, but using Distinct within the join could improve your performance:

    Select u.*
    From Users u
    Join ( Select Distinct user_id
           From log_mview ) l On u.user_id = l.user_id
    
    0 讨论(0)
  • 2021-01-19 13:50

    Try this

    select * from Users u
    where exists 
       ( select user_id 
         from Log_mview l
         where l.user_id = u.user_id )
    /
    

    If the sub-query returns a large number of rows WHERE EXISTS can be substantially faster than WHERE ... IN.

    0 讨论(0)
提交回复
热议问题