monitoring/eventhistory: improve query performance

This takes care of timestamp columns, modifies queries in a way not
depending on db functions, filters are passed through to subqueries
for better performance.
This commit is contained in:
Thomas Gelf 2014-06-20 19:14:34 +02:00
parent 6d7a1cfe08
commit a904ff51aa
9 changed files with 79 additions and 13 deletions

View File

@ -136,9 +136,9 @@ class DbQuery extends SimpleQuery
}
}
} else {
// TODO: render Filter (Where/like/time...)
$str .= $this->whereToSql($filter->getColumn(), $filter->getSign(), $filter->getExpression());
}
return $str;
}
@ -165,6 +165,12 @@ class DbQuery extends SimpleQuery
return preg_replace('/\*/', '%', $value);
}
protected function timestampForSql($value)
{
// TODO: do this db-aware
return $this->escapeForSql(date('Y-m-d H:i:s', $value));
}
public function whereToSql($col, $sign, $expression)
{
if ($this->isTimestamp($col)) {

View File

@ -26,6 +26,15 @@ class CommentdeletionhistoryQuery extends IdoQuery
)
);
public function whereToSql($col, $sign, $expression)
{
if ($col === 'UNIX_TIMESTAMP(h.deletion_time)') {
return 'h.deletion_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
}
}
protected function joinBaseTables()
{
$this->select->from(

View File

@ -26,6 +26,15 @@ class CommenthistoryQuery extends IdoQuery
)
);
public function whereToSql($col, $sign, $expression)
{
if ($col === 'UNIX_TIMESTAMP(h.comment_time)') {
return 'h.comment_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
}
}
protected function joinBaseTables()
{
$this->select->from(

View File

@ -26,6 +26,15 @@ class DowntimeendhistoryQuery extends IdoQuery
)
);
public function whereToSql($col, $sign, $expression)
{
if ($col === 'UNIX_TIMESTAMP(h.actual_end_time)') {
return 'h.actual_end_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
}
}
protected function joinBaseTables()
{
$this->select->from(

View File

@ -26,6 +26,15 @@ class DowntimestarthistoryQuery extends IdoQuery
)
);
public function whereToSql($col, $sign, $expression)
{
if ($col === 'UNIX_TIMESTAMP(h.actual_start_time)') {
return 'h.actual_start_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
}
}
protected function joinBaseTables()
{
$this->select->from(

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Monitoring\Backend\Ido\Query;
use Icinga\Data\Filter\Filter;
use Zend_Db_Select;
class EventHistoryQuery extends IdoQuery
@ -21,7 +22,6 @@ class EventHistoryQuery extends IdoQuery
'service_description' => 'eho.name2 COLLATE latin1_general_ci',
'object_type' => "CASE WHEN eho.objecttype_id = 1 THEN 'host' ELSE 'service' END",
'timestamp' => 'eh.timestamp',
'raw_timestamp' => 'eh.raw_timestamp', // TODO: remove this as soon as removed from frontends
'state' => 'eh.state',
'attempt' => 'eh.attempt',
'max_attempts' => 'eh.max_attempts',
@ -40,7 +40,6 @@ class EventHistoryQuery extends IdoQuery
protected function joinBaseTables()
{
$columns = array(
'raw_timestamp',
'timestamp',
'object_id',
'type',
@ -80,14 +79,22 @@ class EventHistoryQuery extends IdoQuery
return parent::order($columnOrAlias, $dir);
}
public function where($condition, $value = null)
{
$this->requireColumn($condition);
foreach ($this->subQueries as $sub) {
$sub->where($condition, $value);
}
return $this;
}
public function addFilter(Filter $filter)
{
foreach ($this->subQueries as $sub) {
$sub->applyFilter(clone $filter);
}
return $this;
}
public function where($condition, $value = null)
{
$this->requireColumn($condition);
foreach ($this->subQueries as $sub) {
$sub->where($condition, $value);
}
return $this;
}
protected function joinHostgroups()
{

View File

@ -26,6 +26,15 @@ class NotificationhistoryQuery extends IdoQuery
)
);
public function whereToSql($col, $sign, $expression)
{
if ($col === 'UNIX_TIMESTAMP(n.start_time)') {
return 'n.start_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
}
}
protected function joinBaseTables()
{
switch ($this->ds->getDbType()) {

View File

@ -26,6 +26,15 @@ class StatehistoryQuery extends IdoQuery
)
);
public function whereToSql($col, $sign, $expression)
{
if ($col === 'UNIX_TIMESTAMP(sh.state_time)') {
return 'sh.state_time ' . $sign . ' ' . $this->timestampForSql(strtotime($expression));
} else {
return parent::whereToSql($col, $sign, $expression);
}
}
protected function joinBaseTables()
{
$this->select->from(

View File

@ -48,7 +48,6 @@ class EventHistory extends DataView
'service_description',
'object_type',
'timestamp',
'raw_timestamp',
'state',
'attempt',
'max_attempts',
@ -64,7 +63,7 @@ class EventHistory extends DataView
{
return array(
'timestamp' => array(
'columns' => array('raw_timestamp'),
'columns' => array('timestamp'),
'order' => 'DESC'
)
);