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

前端 未结 3 1293
甜味超标
甜味超标 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.

提交回复
热议问题