]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Improve queries for processing radacct into periodic usage data
authorNick Porter <nick@portercomputing.co.uk>
Wed, 12 Oct 2022 10:37:17 +0000 (11:37 +0100)
committerNick Porter <nick@portercomputing.co.uk>
Wed, 12 Oct 2022 11:13:51 +0000 (12:13 +0100)
Removing `OR` from the query allows better index usage

raddb/mods-config/sql/main/mssql/process-radacct.sql
raddb/mods-config/sql/main/mysql/process-radacct.sql
raddb/mods-config/sql/main/postgresql/process-radacct.sql
raddb/mods-config/sql/main/sqlite/process-radacct-refresh.sh

index a3a64451d56979369f177cf971dd173c6670bd84..01129b6c4c8a1c4c71c8bffca9fe5b1c027899e5 100644 (file)
@@ -99,11 +99,21 @@ BEGIN
                 @v_end AS period_end,
                 SUM(acctinputoctets) AS acctinputoctets,
                 SUM(acctoutputoctets) AS acctoutputoctets
-            FROM
-                radacct
-            WHERE
-                acctstoptime > @v_start OR
-                acctstoptime=0
+            FROM ((
+                SELECT
+                    username, acctinputoctets, acctoutputoctets
+                FROM
+                    radacct
+                WHERE
+                    acctstoptime > @v_start
+            ) UNION ALL (
+                SELECT
+                    username, acctinputoctets, acctoutputoctets
+                FROM
+                    radacct
+                WHERE
+                    acctstoptime=0
+            )) a
             GROUP BY
                 username
         ) s
index 8cd0bd25dcc9a17ec50f947f909b79d2e448bdc4..f8956bb60e1fc0a049a864e8d1f8b3b8eb018e5b 100644 (file)
@@ -109,11 +109,21 @@ BEGIN
             v_end,
             SUM(acctinputoctets) AS acctinputoctets,
             SUM(acctoutputoctets) AS acctoutputoctets
-        FROM
-            radacct
-        WHERE
-            acctstoptime > v_start OR
-            acctstoptime IS NULL
+        FROM ((
+            SELECT
+                username, acctinputoctets, acctoutputoctets
+            FROM
+                radacct
+            WHERE
+                acctstoptime > v_start
+        ) UNION ALL (
+            SELECT
+                username, acctinputoctets, acctoutputoctets
+            FROM
+                radacct
+            WHERE
+                acctstoptime IS NULL
+        )) AS a
         GROUP BY
             username
     ) AS s
index a103c8f026ee9c2391e9e3ce42a4b0387d0c043c..16d0de39c15dffeddb8fccfacd66c8afb6f69a47 100644 (file)
@@ -96,11 +96,21 @@ BEGIN
             v_end,
             SUM(acctinputoctets) AS acctinputoctets,
             SUM(acctoutputoctets) AS acctoutputoctets
-        FROM
-            radacct
-        WHERE
-            acctstoptime > v_start OR
-            acctstoptime IS NULL
+        FROM ((
+            SELECT
+                username, acctinputoctets, acctoutputoctets
+            FROM
+                radacct
+            WHERE
+                acctstoptime > v_start
+        ) UNION ALL (
+            SELECT
+                username, acctinputoctets, acctoutputoctets
+            FROM
+                radacct
+            WHERE
+                acctstoptime IS NULL
+        )) AS a
         GROUP BY
             username
     ) AS s
index c32fd438d6428c2acb8124712a90721043edaa69..38c34249b78986596e14c516baa8e9a52b088355 100755 (executable)
@@ -60,7 +60,18 @@ cat <<EOF | sqlite3 "$1"
         FROM
             radacct
         WHERE
-            acctstoptime > (SELECT value FROM vars WHERE key='v_start') OR
+            acctstoptime > (SELECT value FROM vars WHERE key='v_start');
+
+    INSERT INTO radacct_sessions
+        SELECT
+            username,
+            acctstarttime,
+            acctstoptime,
+            acctinputoctets,
+            acctoutputoctets
+        FROM
+            radacct
+        WHERE
             acctstoptime IS NULL;