Skip to content
Permalink
Browse files

don't COALESCE when it's unnecessary

  • Loading branch information...
dannyvankooten committed Nov 14, 2018
1 parent 5610a0a commit 7ccdea4535a0a763ad2445bd87f0811691c41d85
@@ -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
@@ -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
@@ -42,19 +42,22 @@ 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
}

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))

0 comments on commit 7ccdea4

Please sign in to comment.
You can’t perform that action at this time.