加入 2 table 的最佳方式是什么?

What is best way join 2 table?

我有一个问题。
我想找到一个在注册后 24 小时内搜索过的新用户。
我有 2 个 table,这意味着用户加入并搜索了历史记录。
加入那些 table 的最佳方式是什么?(搜索 table 是 KST 时区,用户注册 table 是 UTC 时区)
而且,我写了两个查询,你觉得这样可以吗? ps。我无法附上图片,所以我将其上传为 link。

搜索历史table
Serched history table
用户注册table
User sign up table

预期输出
Expected output

我的查询

1.

select date(t1.kst_created_time), count(distinct t1.id)
from table1 as t1
inner join (
    select id, utc_created_time + interval '9' hour as utc_to_kst_created_time
    from table2
)as t2
    on t1.user_id = t2.id
    and date_diff('second', t2.utc_to_kst_created_time, t1.kst_created_time) <= 86400
group by 1
select
    dt, count(distinct id)
from (
    select
            date(t1.kst_created_time) as dt,
            t1.id as id,
            date_diff('second', t2.user_join_time_kst, t1.kst_created_time ) as second_diff
    from table1 as t1
    inner join (
        select id, utc_created_time + interval '9' hour as utc_to_kst_created_time
        from table2
    ) as t2
        on t1.user_id = t2.id
        and t1.kst_created_time >= t2.utc_to_kst_created_time
) as second_diff_of_t1_and_t2_created_time
where second_diff <= 86400
group by 1

我会推荐 exists 而不是连接(因为您要从 joined table 返回任何内容),并且时区转换使用 at time zone.

假设时间戳存储为timestamp with time zone:

select date(t1.kst_created_time) kst_created_date, count(distinct t1.id) no_
from table1 t1
where exists (
    select 1
    from table2 t2
    where 
        t2.id = t1.user_id 
        and t2.utc_created_time <= t1.kst_created_time at time zone 'UTC' + interval '1' day
)
group by 1

此查询不需要转换 table2 的时间戳,因此它应该利用 table2(id, utc_created_time).

上的索引