From 8a736b5bd0ad00770fb64f72543384ca3eea67d6 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Tue, 19 Sep 2023 11:47:47 +0800 Subject: [PATCH] sqlx: optimize get hots tweets in postgres database env --- internal/dao/sakila/auto/pgc/pgc.go | 20 ++++++++++++++++++++ internal/dao/sakila/tweets.go | 12 ++++++++++-- internal/dao/sakila/tweets_pgc.go | 12 ++++++++++++ internal/dao/sakila/yesql/yesql_pgc.sql | 14 ++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/internal/dao/sakila/auto/pgc/pgc.go b/internal/dao/sakila/auto/pgc/pgc.go index 00780f9f..12143fa6 100644 --- a/internal/dao/sakila/auto/pgc/pgc.go +++ b/internal/dao/sakila/auto/pgc/pgc.go @@ -22,6 +22,7 @@ const ( _CommentManage_CreateCommentReply = `INSERT INTO @comment_reply (comment_id, user_id, content, at_user_id, ip, ip_loc, created_on) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING *` _ContactManager_CreateContact = `INSERT INTO @contact (user_id, friend_id, status, created_on) VALUES (?, ?, ?, ?) RETURNING *` _Message_CreateMessage = `INSERT INTO @message (sender_user_id, receiver_user_id, type, brief, content, post_id, comment_id, reply_id, created_on) VALUES (:sender_user_id, :receiver_user_id, :type, :brief, :content, :post_id, :comment_id, :reply_id, :created_on) RETURNING id` + _Tweet_ListIndexHotsTweets = `SELECT post.* FROM @post post LEFT JOIN @post_metric metric ON post.id=metric.post_id WHERE post.visibility>=90 AND post.is_del=0 ORDER BY post.is_top DESC, metric.rank_score DESC NULLS LAST, post.latest_replied_on DESC LIMIT ? OFFSET ?` _TweetManage_AddAttachment = `INSERT INTO @attachment (user_id, file_size, img_width, img_height, type, content, created_on) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id` _TweetManage_AddPost = `INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, latest_replied_on, created_on) VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replied_on, :created_on) RETURNING id` _TweetManage_AddPostCollection = `INSERT INTO @post_collection (post_id, user_id, created_on) VALUES (?, ?, ?) RETURNING *` @@ -68,6 +69,11 @@ type Message struct { CreateMessage *sqlx.NamedStmt `yesql:"create_message"` } +type Tweet struct { + yesql.Namespace `yesql:"tweet"` + ListIndexHotsTweets *sqlx.Stmt `yesql:"list_index_hots_tweets"` +} + type TweetManage struct { yesql.Namespace `yesql:"tweet_manage"` AddAttachment *sqlx.Stmt `yesql:"add_attachment"` @@ -135,6 +141,20 @@ func BuildMessage(p PreparexBuilder, ctx ...context.Context) (obj *Message, err return } +func BuildTweet(p PreparexBuilder, ctx ...context.Context) (obj *Tweet, err error) { + var c context.Context + if len(ctx) > 0 && ctx[0] != nil { + c = ctx[0] + } else { + c = context.Background() + } + obj = &Tweet{} + if obj.ListIndexHotsTweets, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Tweet_ListIndexHotsTweets))); err != nil { + return nil, fmt.Errorf("prepare _Tweet_ListIndexHotsTweets error: %w", err) + } + return +} + func BuildTweetManage(p PreparexBuilder, ctx ...context.Context) (obj *TweetManage, err error) { var c context.Context if len(ctx) > 0 && ctx[0] != nil { diff --git a/internal/dao/sakila/tweets.go b/internal/dao/sakila/tweets.go index fb40b889..c5416969 100644 --- a/internal/dao/sakila/tweets.go +++ b/internal/dao/sakila/tweets.go @@ -562,11 +562,19 @@ func (s *tweetSrv) GetPostContentByID(id int64) (res *ms.PostContent, err error) return db.Get[ms.PostContent](s.q.GetPostContentById, &res, id) } -func newTweetService(db *sqlx.DB) core.TweetService { - return &tweetSrv{ +func newTweetService(db *sqlx.DB) (s core.TweetService) { + ts := &tweetSrv{ sqlxSrv: newSqlxSrv(db), q: ccBuild(db, cc.BuildTweet), } + s = ts + if cfg.Any("PostgreSQL", "PgSQL", "Postgres") { + s = &pgcTweetSrv{ + tweetSrv: ts, + p: pgcBuild(db, pgc.BuildTweet), + } + } + return } func newTweetManageService(db *sqlx.DB, cacheIndex core.CacheIndexService) (s core.TweetManageService) { diff --git a/internal/dao/sakila/tweets_pgc.go b/internal/dao/sakila/tweets_pgc.go index e4f38772..8fd177b3 100644 --- a/internal/dao/sakila/tweets_pgc.go +++ b/internal/dao/sakila/tweets_pgc.go @@ -17,11 +17,23 @@ var ( _ core.TweetManageService = (*pgcTweetManageSrv)(nil) ) +type pgcTweetSrv struct { + *tweetSrv + p *pgc.Tweet +} + type pgcTweetManageSrv struct { *tweetManageSrv p *pgc.TweetManage } +func (s *pgcTweetSrv) ListIndexHotsTweets(limit int, offset int) (res []*ms.Post, total int64, err error) { + if err = s.p.ListIndexHotsTweets.Select(&res, limit, offset); err == nil { + err = s.q.CountIndexHotsTweets.Get(&total) + } + return +} + func (s *pgcTweetManageSrv) CreatePost(r *ms.Post) (*ms.Post, error) { now := time.Now().Unix() r.Model = &ms.Model{CreatedOn: now} diff --git a/internal/dao/sakila/yesql/yesql_pgc.sql b/internal/dao/sakila/yesql/yesql_pgc.sql index ecca700c..edf98ea4 100644 --- a/internal/dao/sakila/yesql/yesql_pgc.sql +++ b/internal/dao/sakila/yesql/yesql_pgc.sql @@ -4,6 +4,20 @@ -- version is c* -------------------------------------------------------------------------------- +-------------------------------------------------------------------------------- +-- tweet sql dml +-------------------------------------------------------------------------------- + +-- name: list_index_hots_tweets@tweet +-- prepare: stmt +SELECT post.* +FROM @post post +LEFT JOIN @post_metric metric +ON post.id=metric.post_id +WHERE post.visibility>=90 AND post.is_del=0 +ORDER BY post.is_top DESC, metric.rank_score DESC NULLS LAST, post.latest_replied_on DESC +LIMIT ? OFFSET ?; + -------------------------------------------------------------------------------- -- tweet_manage sql dml --------------------------------------------------------------------------------