Skip to content

Commit e992d20

Browse files
committed
SIP Trace: Optimize very wasteful "SELECT COUNT(*)" query
Do not pull all the data just to count the number of results, as the "sip_trace" table can easily grow to over tens of GB, and the query will take 5 minutes to run... Just use COUNT(*) !!! (cherry picked from commit c85bee6)
1 parent 7832498 commit e992d20

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

web/tools/system/siptrace/template/tracer.main.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,33 +134,24 @@
134134

135135

136136
if ($_SESSION['grouped_results']) {
137+
if ($config->db_driver == "mysql")
138+
$sql = "SELECT DISTINCT callid FROM ".$table." WHERE status='' AND direction='in'".$sql_search." ORDER BY id DESC";
139+
else if ($config->db_driver == "pgsql")
140+
$sql = "SELECT DISTINCT ON (callid) callid FROM ".$table." WHERE status='' AND direction='in'".$sql_search." ORDER BY callid DESC";
137141

138-
if ($config->db_driver == "mysql") {
139-
140-
$sql="SELECT DISTINCT callid FROM ".$table." WHERE status='' AND direction='in'".$sql_search." ORDER BY id DESC";
141-
142-
} else if ($config->db_driver = "pgsql") {
143-
144-
$sql="SELECT DISTINCT ON (callid) callid FROM ".$table." WHERE status='' AND direction='in'".$sql_search." ORDER BY callid DESC";
145-
146-
}
147-
142+
$sql_cnt = "SELECT COUNT(DISTINCT(callid)) FROM ".$table." WHERE status='' AND direction='in'".$sql_search." ORDER BY callid DESC";
148143
} else {
149-
150-
if ($sql_search=="") $sql="SELECT id FROM ".$table." ORDER BY id DESC";
151-
152-
else $sql="SELECT id FROM ".$table." WHERE (1=1) ".$sql_search." ORDER BY id DESC";
153-
144+
$sql = "SELECT id FROM ".$table." WHERE (1=1) ".$sql_search." ORDER BY id DESC";
145+
$sql_cnt = "SELECT COUNT(*) FROM ".$table." WHERE (1=1) ".$sql_search;
154146
}
155147

156-
$stm = $link->prepare($sql);
148+
$stm = $link->prepare($sql_cnt);
157149
if ($stm===FALSE) {
158150
die('Failed to issue query ['.$sql_command.'], error message : ' . $link->errorInfo()[2]);
159151
}
160152
$stm->execute( $sql_vals );
161-
$resultset = $stm->fetchAll(PDO::FETCH_ASSOC);
153+
$data_no = $stm->fetchColumn(0);
162154

163-
$data_no=count($resultset);
164155
if ($data_no==0) echo('<tr><td colspan="5" class="rowEven" align="center"><br>'.$no_result.'<br><br></td></tr>');
165156
else
166157
{

0 commit comments

Comments
 (0)