Skip to content

Commit

Permalink
Merge pull request #405 from Icinga/query-optimization
Browse files Browse the repository at this point in the history
Optimize queries
  • Loading branch information
nilmerg authored Nov 12, 2021
2 parents 698b84b + 2405936 commit 8d6a8a5
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
10 changes: 5 additions & 5 deletions application/controllers/CommentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function indexAction()
$sortControl = $this->createSortControl(
$comments,
[
'comment.entry_time desc' => t('Entry Time'),
'host.display_name, service.display_name' => t('Host'),
'service.display_name, host.display_name' => t('Service'),
'comment.author' => t('Author'),
'comment.expire_time desc' => t('Expire Time')
'comment.entry_time desc' => t('Entry Time'),
'host.display_name' => t('Host'),
'service.display_name' => t('Service'),
'comment.author' => t('Author'),
'comment.expire_time desc' => t('Expire Time')
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
Expand Down
21 changes: 11 additions & 10 deletions application/controllers/DowntimesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public function indexAction()
$sortControl = $this->createSortControl(
$downtimes,
[
'downtime.is_in_effect, downtime.start_time desc' => t('Is In Effect'),
'downtime.entry_time' => t('Entry Time'),
'host.display_name, service.display_name' => t('Host'),
'service.display_name, host.display_name' => t('Service'),
'downtime.author' => t('Author'),
'downtime.start_time desc' => t('Start Time'),
'downtime.end_time desc' => t('End Time'),
'downtime.scheduled_start_time desc' => t('Scheduled Start Time'),
'downtime.scheduled_end_time desc' => t('Scheduled End Time'),
'downtime.duration desc' => t('Duration')
'downtime.is_in_effect desc, downtime.start_time desc' => t('Is In Effect'),
'downtime.entry_time' => t('Entry Time'),
'host.display_name' => t('Host'),
'service.display_name' => t('Service'),
'downtime.author' => t('Author'),
'downtime.start_time desc' => t('Start Time'),
'downtime.end_time desc' => t('End Time'),
'downtime.scheduled_start_time desc' => t('Scheduled Start Time'),
'downtime.scheduled_end_time desc' => t('Scheduled End Time'),
'downtime.duration desc' => t('Duration')
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
Expand All @@ -68,6 +68,7 @@ public function indexAction()
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();

return;
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions application/controllers/HistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ public function indexAction()

$history->filter(Filter::lessThanOrEqual('event_time', $before));
$this->filter($history, $filter);

$history->getWith()['history.host']->setJoinType('LEFT');
$history->getSelectBase()
// Make sure we'll fetch service history entries only for services which still exist
->where(['history.service_id IS NULL', 'history_service.id IS NOT NULL'], Sql::ANY);
// Because of LEFT JOINs, make sure we'll fetch history entries only for items which still exist:
->where(['history_host.id IS NOT NULL', 'history_service.id IS NOT NULL'], Sql::ANY);

yield $this->export($history);

Expand Down
4 changes: 2 additions & 2 deletions application/controllers/HostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public function historyAction()
$history
->getSelectBase()
->where([
'history_host.id = ?' => $this->host->id,
'history.object_type = ?' => 'host'
'history.host_id = ?' => $this->host->id,
'history.service_id IS NULL'
]);

$before = $this->params->shift('before', time());
Expand Down
1 change: 1 addition & 0 deletions application/controllers/HostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function indexAction()
$db = $this->getDb();

$hosts = Host::on($db)->with(['state', 'icon_image', 'state.last_comment']);
$hosts->getWith()['host.state']->setJoinType('INNER');
$hosts->setResultSetClass(VolatileStateResults::class);

$this->handleSearchRequest($hosts);
Expand Down
4 changes: 2 additions & 2 deletions application/controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ public function historyAction()
$history
->getSelectBase()
->where([
'history_host_service.id = ?' => $this->service->id,
'history_service.id = ?' => $this->service->id
'history.host_id = ?' => $this->service->host_id,
'history.service_id = ?' => $this->service->id
]);

$before = $this->params->shift('before', time());
Expand Down
11 changes: 6 additions & 5 deletions application/controllers/ServicesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function indexAction()
'host.state',
'icon_image'
]);
$services->getWith()['service.state']->setJoinType('INNER');
$services->setResultSetClass(VolatileStateResults::class);

$this->handleSearchRequest($services);
Expand All @@ -61,11 +62,11 @@ public function indexAction()
$sortControl = $this->createSortControl(
$services,
[
'service.display_name, host.display_name' => t('Name'),
'service.state.severity desc' => t('Severity'),
'service.state.soft_state' => t('Current State'),
'service.state.last_state_change desc' => t('Last State Change'),
'host.display_name, service.display_name' => t('Host')
'service.display_name' => t('Name'),
'service.state.severity desc' => t('Severity'),
'service.state.soft_state' => t('Current State'),
'service.state.last_state_change desc' => t('Last State Change'),
'host.display_name' => t('Host')
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
Expand Down
2 changes: 1 addition & 1 deletion library/Icingadb/Model/Downtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getSearchColumns()

public function getDefaultSort()
{
return ['downtime.is_in_effect', 'downtime.start_time desc'];
return ['downtime.is_in_effect desc', 'downtime.start_time desc'];
}

public function createBehaviors(Behaviors $behaviors)
Expand Down

0 comments on commit 8d6a8a5

Please sign in to comment.