前提・実現したいこと
掲示板サイトで限定公開機能を実装しています。
テーブル設計として、限定公開相手の情報を掲示板のテーブルと別にするか同じにするかで悩んでいます。
どちらがいいと思うか、ご意見お聞かせ願えませんでしょうか。
掲示板のテーブルと別にする
別のテーブルにするというのは次のような構成で、postsだけでなくlimitedsも作るということです。
CREATE TABLE posts (`post_id` int, `author_id` int, `publicness` varchar(10), `parent_thread_id` int, `parent_comment_id` int, `kind` varchar(10), `text` varchar(1000), `post_date` datetime) ; INSERT INTO posts (`post_id`, `author_id`, `publicness`, `parent_thread_id`, `parent_comment_id`, `kind`, `text`, `post_date`) VALUES (1, 2, 'limited', 0, 0, 'thread', '掲示板1', '2020-01-01 00:00:00') ,(2, 2, null, 1, 0, 'comment', 'コメント2', '2020-01-02 00:00:00') ,(3, 1, null, 1, 2, 'reply', '返信3', '2020-01-03 00:00:00') ,(4, 3, 'public', 0, 0, 'thread', '掲示板4', '2020-01-04 00:00:00') ,(5, 1, 'limited', 0, 0, 'thread', '掲示板5', '2020-01-05 00:00:00') ,(6, 2, null, 5, 0, 'comment', 'コメント6', '2020-01-06 00:00:00') ,(7, 3, 'folllow', 4, 0, 'thread', '掲示板7', '2020-01-07 00:00:00') ; CREATE TABLE limiteds (`limited_id` int, `thread_id` int, `target_id` int) ; INSERT INTO limiteds (`limited_id`, `thread_id`, `target_id`) VALUES #thread1がuser1547105473に公開されてる (1, 1, 1547105473) #thread5がuser4902045731とuser2305478462に公開されてる ,(2, 5, 4902045731), (3, 5, 2305478462) #もし1つのtureadが1000人に公開される場合 #それだけでここが1000レコードになってしまう ;
上記ですと公開相手が数人ならともかくもし1000人いた場合、掲示板1つのためにlimitedsに1000件のレコードが増えることになる点でとうかと思うのですが、しかしlimitedsのtarget_idにインデックスを貼っておけば「自分に公開されているかどうか」をSELECTするのが早いのではないかと考えています。
掲示板のテーブルと同じにする
他方で同じテーブルにした場合の構成はこうでしょうか。
上でlimitedsという別のテーブルにあった情報をpostsのlimited_idsカラムにぶちこんだ感じです。
ユーザーIDが10桁あり、最大1000人に公開できるので、limited_idsはtext(10000)としています。
CREATE TABLE posts (`post_id` int, `author_id` int, `publicness` varchar(10), `parent_thread_id` int, `parent_comment_id` int, `kind` varchar(10), `text` varchar(1000), `post_date` datetime, `limited_ids` text(10000)) ; INSERT INTO posts (`post_id`, `author_id`, `publicness`, `parent_thread_id`, `parent_comment_id`, `kind`, `text`, `post_date`, `limited_ids`) VALUES (1, 2, 'limited', 0, 0, 'thread', '掲示板1', '2020-01-01 00:00:00', '1547105473') ,(2, 2, null, 1, 0, 'comment', 'コメント2', '2020-01-02 00:00:00', null) ,(3, 1, null, 1, 2, 'reply', '返信3', '2020-01-03 00:00:00', null) ,(4, 3, 'public', 0, 0, 'thread', '掲示板4', '2020-01-04 00:00:00', null) ,(5, 1, 'limited', 0, 0, 'thread', '掲示板5', '2020-01-05 00:00:00', '4902045731|2305478462') ,(6, 2, null, 5, 0, 'comment', 'コメント6', '2020-01-06 00:00:00', null) ,(7, 3, 'folllow', 4, 0, 'thread', '掲示板7', '2020-01-07 00:00:00', null) ;
正規化されているのは前者ですが、後者でも「自分に公開されているかどうか」をSELECTするときはlimited_idsにregexで検索をかければいいですし、
あとは「自分に公開されているすべての掲示板」という条件で取得することはなく、あくまで別の条件で取得した掲示板やコメントに対して、フィルターをかけるイメージで使うのが公開情報なので、そういったことを踏まえると後者でもいいのかなと考えているところです。
掲示板の限定公開機能はよくあるものかと思うので、実務的なご経験談など踏まえてお聞かせ願えましたら幸いです。
ご質問は以上になります。どうぞ宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/17 17:48