From 7ccdea4535a0a763ad2445bd87f0811691c41d85 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Wed, 14 Nov 2018 10:27:14 +0100 Subject: [PATCH] don't COALESCE when it's unnecessary --- pkg/datastore/sqlstore/page_stats.go | 8 ++++---- pkg/datastore/sqlstore/referrer_stats.go | 4 ++-- pkg/datastore/sqlstore/site_stats.go | 15 +++++++++------ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/datastore/sqlstore/page_stats.go b/pkg/datastore/sqlstore/page_stats.go index b3bfa551..edbd944d 100644 --- a/pkg/datastore/sqlstore/page_stats.go +++ b/pkg/datastore/sqlstore/page_stats.go @@ -43,11 +43,11 @@ func (db *sqlstore) GetAggregatedPageStats(siteID int64, startDate time.Time, en query := db.Rebind(`SELECT h.name AS hostname, p.name AS pathname, - SUM(pageviews) AS pageviews, - SUM(visitors) AS visitors, + MAX(SUM(pageviews), 1) AS pageviews, + MAX(SUM(visitors), 1) AS visitors, SUM(entries) AS entries, - COALESCE(SUM(entries*bounce_rate) / NULLIF(SUM(entries), 0), 0.00) AS bounce_rate, - COALESCE(SUM(pageviews*avg_duration) / SUM(pageviews), 0.00) AS avg_duration + COALESCE(SUM(entries*bounce_rate) / SUM(entries), 0.00) AS bounce_rate, + SUM(pageviews*avg_duration) / SUM(pageviews) AS avg_duration FROM page_stats s LEFT JOIN hostnames h ON h.id = s.hostname_id LEFT JOIN pathnames p ON p.id = s.pathname_id diff --git a/pkg/datastore/sqlstore/referrer_stats.go b/pkg/datastore/sqlstore/referrer_stats.go index 25aa3bc8..68aefebd 100644 --- a/pkg/datastore/sqlstore/referrer_stats.go +++ b/pkg/datastore/sqlstore/referrer_stats.go @@ -47,8 +47,8 @@ func (db *sqlstore) GetAggregatedReferrerStats(siteID int64, startDate time.Time COALESCE(groupname, '') AS groupname, SUM(visitors) AS visitors, SUM(pageviews) AS pageviews, - COALESCE(SUM(pageviews*NULLIF(bounce_rate, 0)) / SUM(pageviews), 0.00) AS bounce_rate, - COALESCE(SUM(pageviews*avg_duration) / SUM(pageviews), 0.00) AS avg_duration + SUM(pageviews*bounce_rate) / SUM(pageviews) AS bounce_rate, + SUM(pageviews*avg_duration) / SUM(pageviews) AS avg_duration FROM referrer_stats s LEFT JOIN hostnames h ON h.id = s.hostname_id LEFT JOIN pathnames p ON p.id = s.pathname_id diff --git a/pkg/datastore/sqlstore/site_stats.go b/pkg/datastore/sqlstore/site_stats.go index 4fa4ae23..120e98d2 100644 --- a/pkg/datastore/sqlstore/site_stats.go +++ b/pkg/datastore/sqlstore/site_stats.go @@ -42,7 +42,10 @@ func (db *sqlstore) updateSiteStats(s *models.SiteStats) error { func (db *sqlstore) GetSiteStatsPerDay(siteID int64, startDate time.Time, endDate time.Time) ([]*models.SiteStats, error) { results := []*models.SiteStats{} - query := db.Rebind(`SELECT * FROM site_stats WHERE site_id = ? AND ts >= ? AND ts <= ? ORDER BY ts DESC`) + query := db.Rebind(`SELECT * + FROM site_stats + WHERE site_id = ? AND ts >= ? AND ts <= ? + ORDER BY ts DESC`) err := db.Select(&results, query, siteID, startDate.Format(DATE_FORMAT), endDate.Format(DATE_FORMAT)) return results, err } @@ -50,11 +53,11 @@ func (db *sqlstore) GetSiteStatsPerDay(siteID int64, startDate time.Time, endDat func (db *sqlstore) GetAggregatedSiteStats(siteID int64, startDate time.Time, endDate time.Time) (*models.SiteStats, error) { stats := &models.SiteStats{} query := db.Rebind(`SELECT - COALESCE(SUM(pageviews), 0) AS pageviews, - COALESCE(SUM(visitors), 0) AS visitors, - COALESCE(SUM(sessions), 0) AS sessions, - COALESCE(SUM(pageviews*avg_duration) / NULLIF(SUM(pageviews), 0), 0.00) AS avg_duration, - COALESCE(SUM(sessions*bounce_rate) / NULLIF(SUM(sessions), 0), 0.00) AS bounce_rate + SUM(pageviews) AS pageviews, + SUM(visitors) AS visitors, + SUM(sessions) AS sessions, + SUM(pageviews*avg_duration) / SUM(pageviews) AS avg_duration, + COALESCE(SUM(sessions*bounce_rate) / SUM(sessions), 0.00) AS bounce_rate FROM site_stats WHERE site_id = ? AND ts >= ? AND ts <= ? LIMIT 1`) err := db.Get(stats, query, siteID, startDate.Format(DATE_FORMAT), endDate.Format(DATE_FORMAT))