[zabbix20/el5] SQL speed-up patch for graphs (ZBX-6804)
Volker Fröhlich
volter at fedoraproject.org
Wed Oct 2 19:23:21 UTC 2013
commit 6ece5476cdadeecc70e9b815cf3068d14dcfb0f3
Author: Volker Fröhlich <volker27 at gmx.at>
Date: Mon Sep 23 17:48:21 2013 +0200
SQL speed-up patch for graphs (ZBX-6804)
zabbix-2.0.8-ZBX-6804.patch | 121 +++++++++++++++++++++++++++++++++++++++++++
zabbix20.spec | 5 ++
2 files changed, 126 insertions(+), 0 deletions(-)
---
diff --git a/zabbix-2.0.8-ZBX-6804.patch b/zabbix-2.0.8-ZBX-6804.patch
new file mode 100644
index 0000000..f3547b4
--- /dev/null
+++ b/zabbix-2.0.8-ZBX-6804.patch
@@ -0,0 +1,121 @@
+Index: frontends/php/include/graphs.inc.php
+===================================================================
+--- frontends/php/include/graphs.inc.php (revision 38431)
++++ frontends/php/include/graphs.inc.php (revision 38500)
+@@ -199,18 +199,19 @@
+ /**
+ * Return the time of the 1st appearance of item in trends.
+ *
+- * @param array|int $itemids
++ * @param array $itemIds
+ *
+ * @return int (unixtime)
+ */
+-function get_min_itemclock_by_itemid($itemids) {
+- zbx_value2array($itemids);
++function get_min_itemclock_by_itemid($itemIds) {
++ zbx_value2array($itemIds);
++
+ $min = null;
+ $result = time() - SEC_PER_YEAR;
+
+- $items_by_type = array(
++ $itemTypes = array(
+ ITEM_VALUE_TYPE_FLOAT => array(),
+- ITEM_VALUE_TYPE_STR => array(),
++ ITEM_VALUE_TYPE_STR => array(),
+ ITEM_VALUE_TYPE_LOG => array(),
+ ITEM_VALUE_TYPE_UINT64 => array(),
+ ITEM_VALUE_TYPE_TEXT => array()
+@@ -219,64 +220,68 @@
+ $dbItems = DBselect(
+ 'SELECT i.itemid,i.value_type'.
+ ' FROM items i'.
+- ' WHERE '.dbConditionInt('i.itemid', $itemids)
++ ' WHERE '.dbConditionInt('i.itemid', $itemIds)
+ );
+
+ while ($item = DBfetch($dbItems)) {
+- $items_by_type[$item['value_type']][$item['itemid']] = $item['itemid'];
++ $itemTypes[$item['value_type']][$item['itemid']] = $item['itemid'];
+ }
+
+ // data for ITEM_VALUE_TYPE_FLOAT and ITEM_VALUE_TYPE_UINT64 can be stored in trends tables or history table
+ // get max trends and history values for such type items to find out in what tables to look for data
+- $sql_from = 'history';
+- $sql_from_num = '';
++ $sqlFrom = 'history';
++ $sqlFromNum = '';
+
+- if (!empty($items_by_type[ITEM_VALUE_TYPE_FLOAT]) || !empty($items_by_type[ITEM_VALUE_TYPE_UINT64])) {
+- $itemids_numeric = zbx_array_merge($items_by_type[ITEM_VALUE_TYPE_FLOAT], $items_by_type[ITEM_VALUE_TYPE_UINT64]);
++ if (!empty($itemTypes[ITEM_VALUE_TYPE_FLOAT]) || !empty($itemTypes[ITEM_VALUE_TYPE_UINT64])) {
++ $itemIdsNumeric = zbx_array_merge($itemTypes[ITEM_VALUE_TYPE_FLOAT], $itemTypes[ITEM_VALUE_TYPE_UINT64]);
+
+ $sql = 'SELECT MAX(i.history) AS history,MAX(i.trends) AS trends'.
+ ' FROM items i'.
+- ' WHERE '.dbConditionInt('i.itemid', $itemids_numeric);
+- if ($table_for_numeric = DBfetch(DBselect($sql))) {
+- $sql_from_num = ($table_for_numeric['history'] > $table_for_numeric['trends']) ? 'history' : 'trends';
+- $result = time() - (SEC_PER_DAY * max($table_for_numeric['history'], $table_for_numeric['trends']));
++ ' WHERE '.dbConditionInt('i.itemid', $itemIdsNumeric);
++ if ($tableForNumeric = DBfetch(DBselect($sql))) {
++ $sqlFromNum = ($tableForNumeric['history'] > $tableForNumeric['trends']) ? 'history' : 'trends';
++ $result = time() - (SEC_PER_DAY * max($tableForNumeric['history'], $tableForNumeric['trends']));
+ }
+ }
+
+- foreach ($items_by_type as $type => $items) {
++ foreach ($itemTypes as $type => $items) {
+ if (empty($items)) {
+ continue;
+ }
+
+- switch($type) {
++ switch ($type) {
+ case ITEM_VALUE_TYPE_FLOAT:
+- $sql_from = $sql_from_num;
++ $sqlFrom = $sqlFromNum;
+ break;
+ case ITEM_VALUE_TYPE_STR:
+- $sql_from = 'history_str';
++ $sqlFrom = 'history_str';
+ break;
+ case ITEM_VALUE_TYPE_LOG:
+- $sql_from = 'history_log';
++ $sqlFrom = 'history_log';
+ break;
+ case ITEM_VALUE_TYPE_UINT64:
+- $sql_from = $sql_from_num.'_uint';
++ $sqlFrom = $sqlFromNum.'_uint';
+ break;
+ case ITEM_VALUE_TYPE_TEXT:
+- $sql_from = 'history_text';
++ $sqlFrom = 'history_text';
+ break;
+ default:
+- $sql_from = 'history';
++ $sqlFrom = 'history';
+ }
+
++ foreach ($itemIds as $itemId) {
++ $sqlUnions[] = 'SELECT MIN(ht.clock) AS c FROM '.$sqlFrom.' ht WHERE ht.itemid='.$itemId;
++ }
++
+ $dbMin = DBfetch(DBselect(
+- 'SELECT MIN(ht.clock) AS min_clock'.
+- ' FROM '.$sql_from.' ht'.
+- ' WHERE '.dbConditionInt('ht.itemid', $itemids)
++ 'SELECT MIN(ht.c) AS min_clock'.
++ ' FROM ('.implode(' UNION ALL ', $sqlUnions).') ht'
+ ));
+- $min = empty($min) ? $dbMin['min_clock'] : min($min, $dbMin['min_clock']);
++
++ $min = $min ? min($min, $dbMin['min_clock']) : $dbMin['min_clock'];
+ }
+
+- return empty($min) ? $result : $min;
++ return $min ? $min: $result;
+ }
+
+ function get_graph_by_graphid($graphid) {
diff --git a/zabbix20.spec b/zabbix20.spec
index f7e0da5..b484b49 100644
--- a/zabbix20.spec
+++ b/zabbix20.spec
@@ -40,6 +40,10 @@ Patch2: %{srcname}-2.0.1-no-flash.patch
# https://support.zabbix.com/browse/ZBX-4894
Patch4: %{srcname}-2.0.8-ZBX-6992.patch
+# SQL speedup for graphs, fixed in 2.0.9
+# https://support.zabbix.com/browse/ZBX-6840
+Patch5: %{srcname}-2.0.8-ZBX-6804.patch
+
BuildRequires: mysql-devel
BuildRequires: postgresql-devel
BuildRequires: sqlite-devel
@@ -227,6 +231,7 @@ Zabbix web frontend for PostgreSQL
%patch0 -p1
%patch1 -p1
%patch4 -p0
+%patch5 -p0
# Logrotate's su option is currently only available in Fedora
sed -i '/su zabbix zabbix/d' %{SOURCE5}
More information about the scm-commits
mailing list