From 0f1cd79625d4075f74be0f1d25cb4fce90d33290 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Tue, 2 May 2023 21:47:41 +0200 Subject: [PATCH] New optimization attempt. --- lib/BurguillosInfo/Tracking.pm | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/BurguillosInfo/Tracking.pm b/lib/BurguillosInfo/Tracking.pm index d6f9f75..2fba6ff 100644 --- a/lib/BurguillosInfo/Tracking.pm +++ b/lib/BurguillosInfo/Tracking.pm @@ -104,20 +104,17 @@ EOF my $dbh = BurguillosInfo::DB->connect($app); my $data = $dbh->selectall_arrayref(<<"EOF", {Slice => {}}); SELECT paths.path, - ( - $SELECT_GLOBAL - where requests.path = paths.path and date > NOW() - interval '1 day' - ) as unique_ips_last_24_hours, - ( - $SELECT_GLOBAL - where requests.path = paths.path and date > NOW() - interval '1 week' - ) as unique_ips_last_week, - ( - $SELECT_GLOBAL - where requests.path = paths.path and date > NOW() - interval '1 month' - ) as unique_ips_last_month + COUNT(DISTINCT(requests_day.remote_address, requests_day.user_agent)) as unique_ips_last_24_hours, + COUNT(DISTINCT(requests_week.remote_address, requests_week.user_agent)) as unique_ips_last_week, + COUNT(DISTINCT(requests_month.remote_address, requests_month.user_agent)) as unique_ips_last_month FROM paths -WHERE paths.last_seen > NOW() - INTERVAL '1 month'; +LEFT join requests requests_day ON paths.path=requests_day.path +LEFT join requests requests_month ON paths.path=requests_month.path +LEFT join requests requests_week ON paths.path=requests_week.path +WHERE paths.last_seen > NOW() - INTERVAL '1 month' AND + requests_day.date > NOW() - interval '1 day' AND + requests_week.date > NOW() - interval '1 week' +GROUP BY paths.path EOF return $data; }