Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreatedAtはやっぱりほしい #12557

Open
fruitriin opened this issue Dec 3, 2023 · 16 comments
Open

CreatedAtはやっぱりほしい #12557

fruitriin opened this issue Dec 3, 2023 · 16 comments
Labels
✨Feature This adds/improves/enhances a feature packages/backend Server side specific issue/PR

Comments

@fruitriin
Copy link
Contributor

fruitriin commented Dec 3, 2023

Summary

DBのレコードを直接見たときにいつ作られたレコードなのか判別するのがとてもむずかしい。
CreatedAtの復活を熱望する

Purpose

DB上に直接SQLを実行する際に期間指定のクエリを実行するのが困難である
また、DBを直接見たときにレコードができた時間が人間にとって判別できない

@fruitriin fruitriin added the ✨Feature This adds/improves/enhances a feature label Dec 3, 2023
@fruitriin
Copy link
Contributor Author

今の期間指定クエリはどうしてるんだろうと思って調べてみたら、 任意の時刻からIdService.genId() をして、 where(id > 指定時刻に生成されるるID) としているらしい

@fruitriin
Copy link
Contributor Author

パフォーマンス上の利用で dateオブジェクトがメモリを食うという話なら、 Misskeyのアプリ上ではselectの対象にcreatedAtを含めなければよいかも

@samunohito
Copy link
Member

期間によるパーティショニングがやりやすくなるなど、管理的なメリットも大いにあるように思えます。

@acid-chicken
Copy link
Member

DB上に直接SQLを実行する際に期間指定のクエリを実行するのが困難である

pnpm dlx tsx <<<'process.stdout.write(require("./packages/backend/src/misc/id/aid").genAid(Date.now()).slice(0,8)+"00\n")'

@acid-chicken
Copy link
Member

手動で閲覧できたら「楽」なのは同意するが、じゃあ楽ならいいかというとそうは思っていない。
手動でクエリを叩いたり GUI でアクセスしたりするのは #12343 に指摘されるようにプライバシーというか検閲にあたる可能性がある問題を孕むので、手動でやらずにスクリプトを作ってオペレーションすべき。スクリプトを組むならば、先述のように createdAt は相互変換が容易なのでこの懸念はそもそも発生しないと言える。

@acid-chicken
Copy link
Member

いやでも 1fa1d31#commitcomment-130577911 の指摘内容を前提とするなら、スクリプトを組んだとてクエリのパフォーマンスが悪いという問題を孕むかもしれない

@syuilo
Copy link
Member

syuilo commented Dec 3, 2023

期間によるパーティショニングが絶対Date型じゃないとダメなのであれば持っておいても良さそう

@syuilo
Copy link
Member

syuilo commented Dec 3, 2023

パーティショニングってMisskeyが対応しなくても透過的に管理者が勝手にできるのかしら

@syuilo syuilo added the packages/backend Server side specific issue/PR label Dec 3, 2023
@kanade
Copy link

kanade commented Dec 4, 2023

CreatedAtがあったらなという話は以下でも出ていました。
#12278

@u1-liquid
Copy link
Sponsor Contributor

パーティショニングってMisskeyが対応しなくても透過的に管理者が勝手にできるのかしら

今みたいにIDだけでクエリーすると厳しいけどできなくはないですね

@u1-liquid
Copy link
Sponsor Contributor

u1-liquid commented Dec 10, 2023

ioでは2023.x.xマージの時点でcreatedAtを消さない方針にしてました

ref: MisskeyIO@2883f28 (ちょっと違う変更も混ざってますが)
MisskeyIO#169

@fruitriin
Copy link
Contributor Author

postgresqlではBase36エンコーディングはPL/SQLなどで関数を自分で行わないといけなさそう
PL/SQL自身にはBase36エンコーディングの機能がなさそう
postgresql15ではPL/JavaScript(PL/V8)をインストールするにはソースからビルドする必要がありそう

aidだけでこれなので、他のid体系を採用した場合などより事情は複雑になりそうで、技術的には可能だけど、その苦労に見合わないように思う

ちなみにパーティショニングはアプリケーションからは特に気にする必要ないはず

@fruitriin
Copy link
Contributor Author

これはアプリケーション実装上といよりデータベースを管理運営する上でやっぱりほしいですよねぇ。
(プライバシー云々はSQL叩ける権限の人ならもう全部見えるのでそっちの話は関係ない筋かと)

@fruitriin
Copy link
Contributor Author

ちなみにPL/SQLでやろうとするとこんな感じのアプローチになるようです

CREATE OR REPLACE FUNCTION public.aid_to_timestamp(aid text) RETURNS timestamptz AS $$
DECLARE
    time_part text;
    num bigint;
BEGIN
    time_part := left(aid, 8); -- AIDの最初の8文字
    -- ここはTO_NUMBER に Base36decodeに相当する処理がないので実際には実行できない
    num := TO_NUMBER(time_part, '0123456789abcdefghijklmnopqrstuvwxyz') + 946684800000;
    RETURN TO_TIMESTAMP((num / 1000)::bigint)
END;
$$ LANGUAGE plpgsql;

@fruitriin
Copy link
Contributor Author

fruitriin commented Dec 29, 2023

postgres上でAIDを生成するのではなく、アプリケーションから2つの期間からAIDを採番してその間にあるものを抽出するということはまあできないので、 createdAtがないからパーティショニングができないとかいうことはないけど、createdAtはあったほうがうれしいと思いました
(ところでPostgresql上での文字列の大小比較についてパフォーマンスに言及したものがみつかりませんでした。詳しい人いる?)

@u1-liquid
Copy link
Sponsor Contributor

https://gist.github.com/u1-liquid/cf1774c8c891443132bdcf02ca3ab083 🤯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨Feature This adds/improves/enhances a feature packages/backend Server side specific issue/PR
Projects
None yet
Development

No branches or pull requests

6 participants