]> 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:23:24 +0000 (12:23 +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-new-data-usage-period.sh

index a20d20010b3dffd043e9eb3a26e23b07d4578ac4..949a4c64f3d424402a3d6ce676778dc5a631775d 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 1cc8d30fb51d71623b7177cf1a7b8af3568ca266..3555abd98ff2cb89f7693b35a644dcf1b1cdc171 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 8a6047e55439a342629e9723493730fe16726fef..e8d825c8ae2428013997f4dffd8f869928e82182 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 edfb12933144972fa3158eefce83eae7c880d7e9..0deb3911441ce65f55e697d55f04c3e499e883ab 100755 (executable)
@@ -61,7 +61,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;