]> git.ipfire.org Git - thirdparty/squid.git/blame - src/log/DB/doc/date_day_column.sql
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / log / DB / doc / date_day_column.sql
CommitLineData
b8ae064d 1-- Copyright (C) 1996-2023 The Squid Software Foundation and contributors
9a1b46cc
AJ
2--
3-- Squid software is distributed under GPLv2+ license and includes
4-- contributions from numerous individuals and organizations.
5-- Please see the COPYING and CONTRIBUTORS files for details.
6--
7
1c363fe7
AJ
8-- we need a separate column to store the date and time of the request
9ALTER TABLE access_log ADD COLUMN date_day DATE;
10ALTER TABLE access_log ADD COLUMN date_time TIME;
11
12-- let's populate the new columns, in case some rows already exist;
13-- the date and time values should be set by a trigger
14UPDATE access_log SET date_day = DATE(FROM_UNIXTIME(time_since_epoch));
15UPDATE access_log SET date_time = TIME(FROM_UNIXTIME(time_since_epoch));
16
17-- let's create a view that uses the date column
18CREATE VIEW requests_per_day_2 AS SELECT date_day, COUNT(*) AS num_of_requests FROM access_log GROUP BY 1 ORDER BY 1;
19
20-- that view needs an index on the group by column
21CREATE INDEX date_day_idx ON access_log(date_day);
22
23
24-- a trigger that automatically extracts the date value from the time_since_epoch column
25-- and stores it in the date_day column
26DELIMITER //
27CREATE TRIGGER extract_date_day_bi BEFORE INSERT ON access_log FOR EACH ROW
28BEGIN
29 SET NEW.date_day = DATE(FROM_UNIXTIME(NEW.time_since_epoch));
30 SET NEW.date_time = TIME(FROM_UNIXTIME(NEW.time_since_epoch));
31END //
32
33
34-- Note: after running this script against an already populated access_log,
35-- views have to be recreated, or the new date_day column will not show up.