Skip to content

Commit

Permalink
Merge pull request #321 from kinvolk/query_optim
Browse files Browse the repository at this point in the history
Optimisations for slow queries in instance_application table
  • Loading branch information
joaquimrocha authored Jan 15, 2021
2 parents 702e71b + be5279d commit dc65fe0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pkg/api/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (api *API) DeleteApp(appID string) error {
// GetApp returns the application identified by the id provided.
func (api *API) GetApp(appID string) (*Application, error) {
var app Application
query, _, err := api.appsQuery().
query, _, err := goqu.From("application").
Where(goqu.C("id").Eq(appID)).ToSQL()
if err != nil {
return nil, err
Expand Down
67 changes: 45 additions & 22 deletions pkg/api/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/api/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (api *API) DeleteChannel(channelID string) error {
func (api *API) GetChannel(channelID string) (*Channel, error) {
var channel Channel

query, _, err := api.channelsQuery().
query, _, err := goqu.From("channel").
Where(goqu.C("id").Eq(channelID)).
ToSQL()
if err != nil {
Expand All @@ -159,7 +159,7 @@ func (api *API) GetChannel(channelID string) (*Channel, error) {
func (api *API) getSpecificChannels(channelID ...string) ([]*Channel, error) {
var channels []*Channel

query, _, err := api.channelsQuery().
query, _, err := goqu.From("channel").
Where(goqu.Ex{"id": channelID}).
ToSQL()
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/db/migrations/0011_add_index_instance_application.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- +migrate Up

create index instance_application_instance_id_application_id_idx on instance_application (instance_id,application_id);

create index activity_class_created_ts_severity_version_group_id_applica_idx on activity (class,created_ts,severity,"version",group_id,application_id);

-- +migrate Down

drop index instance_application_instance_id_application_id_idx;

drop index activity_class_created_ts_severity_version_group_id_applica_idx;
4 changes: 2 additions & 2 deletions pkg/api/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (api *API) DeleteGroup(groupID string) error {
func (api *API) GetGroup(groupID string) (*Group, error) {
var group Group

query, _, err := api.groupsQuery().
query, _, err := goqu.From("groups").
Where(goqu.C("id").Eq(groupID)).
ToSQL()
if err != nil {
Expand Down Expand Up @@ -308,7 +308,7 @@ func (api *API) GetGroupID(trackName string, arch Arch) (string, error) {
// before the generation because all writes are sequential.
if cachedGroupsRef == nil {
cachedGroups = make(map[GroupDescriptor]string)
query, _, err := api.groupsQuery().ToSQL()
query, _, err := goqu.From("groups").ToSQL()
var groups []*Group
if err == nil {
groups, err = api.getGroupsFromQuery(query)
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ func (api *API) instanceAppQuery(appID, instanceID string, duration postgresDura
query := goqu.From("instance_application").
Select("version", "status", "last_check_for_updates", "last_update_version", "update_in_progress", "application_id", "group_id").
Where(goqu.C("instance_id").Eq(instanceID), goqu.C("application_id").Eq(appID)).
Where(goqu.L("last_check_for_updates > now() at time zone 'utc' - interval ?", duration)).
Where(goqu.L(ignoreFakeInstanceCondition("instance_id")))
Where(goqu.L("last_check_for_updates > now() at time zone 'utc' - interval ?", duration))
return query
}

Expand Down

0 comments on commit dc65fe0

Please sign in to comment.