Optimize blogstats query

This commit is contained in:
Jan-Lukas Else 2024-01-03 18:23:44 +01:00
parent ad7536034a
commit 931864a2f6
1 changed files with 23 additions and 41 deletions

View File

@ -53,76 +53,58 @@ func (a *goBlog) serveBlogStatsTable(w http.ResponseWriter, r *http.Request) {
const blogStatsSql = ` const blogStatsSql = `
with filtered as ( with filtered as (
select select
path, (CASE WHEN coalesce(pub, '') != '' THEN substr(pub, 1, 4) ELSE 'N' END) as year,
pub, (CASE WHEN coalesce(pub, '') != '' THEN substr(pub, 6, 2) ELSE 'N' END) as month,
substr(pub, 1, 4) as year,
substr(pub, 6, 2) as month,
wordcount(content) as words, wordcount(content) as words,
charcount(content) as chars charcount(content) as chars
from ( from (
select select
path,
tolocal(published) as pub, tolocal(published) as pub,
mdtext(coalesce(content, '')) as content mdtext(coalesce(content, '')) as content
from posts from posts
where status = @status and visibility = @visibility and blog = @blog where status = @status and visibility = @visibility and blog = @blog
) )
), aggregated as (
select
year,
month,
coalesce(count(*), 0) as pc,
coalesce(sum(words), 0) as wc,
coalesce(sum(chars), 0) as cc,
coalesce(round(avg(words), 0), 0) as wpp
from filtered
group by year, month
) )
select * select *
from ( from (
select * select *
from ( from (
select select year, 'A', sum(pc), sum(wc), sum(cc), round(sum(wc)/sum(pc), 0)
year, from aggregated
'A', where year != 'N'
coalesce(count(path), 0) as pc,
coalesce(sum(words), 0) as wc,
coalesce(sum(chars), 0) as cc,
coalesce(round(sum(words)/count(path), 0), 0) as wpp
from filtered
where pub != ''
group by year group by year
order by year desc order by year desc
) )
union all union all
select * select *
from ( from (
select select *
year, from aggregated
month, where year != 'N'
coalesce(count(path), 0) as pc,
coalesce(sum(words), 0) as wc,
coalesce(sum(chars), 0) as cc,
coalesce(round(sum(words)/count(path), 0), 0) as wpp
from filtered
where pub != ''
group by year, month
order by year desc, month desc order by year desc, month desc
) )
union all union all
select * select *
from ( from (
select select *
'N', from aggregated
'N', where year == 'N'
coalesce(count(path), 0) as pc,
coalesce(sum(words), 0) as wc,
coalesce(sum(chars), 0) as cc,
coalesce(round(sum(words)/count(path), 0), 0) as wpp
from filtered
where pub == ''
) )
union all union all
select * select *
from ( from (
select select 'A', 'A', sum(pc), sum(wc), sum(cc), round(sum(wc)/sum(pc), 0)
'A', from aggregated
'A',
coalesce(count(path), 0) as pc,
coalesce(sum(words), 0) as wc,
coalesce(sum(chars), 0) as cc,
coalesce(round(sum(words)/count(path), 0), 0) as wpp
from filtered
) )
); );
` `