我有以下数据结构,表名以粗体显示,下面的相关列名。
common_authorprofile:
{id, full_name, description, avatar_id, profile_id}
aldryn_people_person 表:
{id, phone, ...}
aldryn_newsblog_article:
{id, is_published, is_featured, ..., author_id}
值得注意的是,common_authorprofile.profile_id = aldryn_people_person.id
和aldryn_newsblog_article.author_id = aldryn_people_person.id
我正在尝试计算common_authorprofile
.
这是目前的做法:
SELECT main.*, sub.article_count
FROM common_authorprofile AS main
INNER JOIN aldryn_people_person
ON aldryn_people_person.id = main.profile_id,
LATERAL
(SELECT author_id, COUNT(*) as article_count
FROM aldryn_newsblog_article AS sub
WHERE
sub.author_id = aldryn_people_person.id AND
sub.app_config_id = 1 AND
sub.is_published IS TRUE AND
sub.publishing_date <= now() AND
aldryn_people_person.id = sub.author_id
GROUP BY author_id
) AS sub
我的问题有两个:
- 鉴于表关系,这是一种正确的做法吗?
- 这是一种有效的方法吗,即有没有办法提高它的速度和可读性?