Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: Improve sql query performances #323

Merged
merged 4 commits into from
Jan 21, 2021
Merged

backend: Improve sql query performances #323

merged 4 commits into from
Jan 21, 2021

Conversation

yolossn
Copy link
Contributor

@yolossn yolossn commented Jan 20, 2021

Improve ignore fake instance condition

Performance improvement in Simple queries.

select * from instance where instance.id SIMILAR TO '\{[a-fA-F0-9-]{36}\}';

image

select * from instance where id LIKE '{________-____-____-____-____________}'

image

Performance improvement in Complex queries.

SELECT a.name AS app_name, count(*) as fail_count FROM application a, event e, event_type et WHERE a.team_id = 'd89342dc-9214-441d-a4af-bdd837a3b239' AND a.id = e.application_id AND e.event_type_id = et.id AND et.result = 0 AND et.type = 3 AND (e.instance_id IS NULL OR e.instance_id NOT SIMILAR TO '\{[a-fA-F0-9-]{36}\}') GROUP BY app_name ORDER BY app_name;

Overall time: 499ms

image

SELECT a.name AS app_name, count(*) as fail_count FROM application a, event e, event_type et WHERE a.team_id = 'd89342dc-9214-441d-a4af-bdd837a3b239' AND a.id = e.application_id AND e.event_type_id = et.id AND et.result = 0 AND et.type = 3 AND (e.instance_id IS NULL OR e.instance_id NOT LIKE '{________-____-____-____-____________}') GROUP BY app_name ORDER BY app_name;

Overall time: 224ms

image

Add composite index for stats query on instance_application table

query: SELECT COUNT (*) FROM "instance_application" WHERE (("application_id" = 'e96281a6-d1af-4bde-9a0a-97b76e56dc57') AND ("group_id" = '9a2deb70-37be-4026-853f-bfdd6b347bbe') AND last_check_for_updates > now() at time zone 'utc' - interval '1days' AND (instance_id IS NULL OR instance_id NOT SIMILAR TO '\{[a-fA-F0-9-]{36}\}'))

index: create index concurrently if not exists instance_application_application_id_group_id_last_check_for_idx on instance_application(application_id,group_id,last_check_for_updates,instance_id);

Before:

image

After:

image

Drop unused indexes.

There are few indexes that are not used, they can affect the insert performance.
Refer:
image

Right now the ignore fake instance condition uses a SIMILAR check
that is replaced with LIKE operator for better performance.

Signed-off-by: yolossn <santhosh@kinvolk.io>
@yolossn yolossn changed the title backend: Improve ignore fake instance condition for better performance backend: Improve performance of ignore fake instance condition Jan 20, 2021
At present the file is misnamed, so rename it to add more
composite indexes to the same migration before release.

Signed-off-by: yolossn <santhosh@kinvolk.io>
@yolossn yolossn requested a review from joaquimrocha January 20, 2021 18:58
@yolossn yolossn changed the title backend: Improve performance of ignore fake instance condition backend: Improve sql query performances Jan 20, 2021
This composite index improves the performance of slow
performing stats query on instance_application table.
Also makes the index creation and drop operations optional.

Signed-off-by: yolossn <santhosh@kinvolk.io>
At present there are few indexes that  are never used, unused indexes
can affect insert performance so they are removed.

Signed-off-by: yolossn <santhosh@kinvolk.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants