From 6ae052c17d8212c42e59f131df369e6d53b989f2 Mon Sep 17 00:00:00 2001
From: gillespi314 <73313222+gillespi314@users.noreply.github.com>
Date: Mon, 6 Mar 2023 12:41:27 -0600
Subject: [PATCH] Optimize sql for mdm profile status counts (#10304)
Local performance results with 2000+ records in hosts (no index for
hosts.uuid) and 4000+ records in host_mdm_apple_profiles:
New query (30ms)
Old query (900ms)
---
server/datastore/mysql/apple_mdm.go | 89 +++++++++++++++--------------
1 file changed, 45 insertions(+), 44 deletions(-)
diff --git a/server/datastore/mysql/apple_mdm.go b/server/datastore/mysql/apple_mdm.go
index c3b9f87d2d..3366d2a19f 100644
--- a/server/datastore/mysql/apple_mdm.go
+++ b/server/datastore/mysql/apple_mdm.go
@@ -1065,53 +1065,54 @@ func (ds *Datastore) GetMDMAppleHostsProfilesSummary(ctx context.Context, teamID
// TODO(sarah): add cases to handle Fleet-managed profiles (e.g., disk encryption)
sqlFmt := `
SELECT
- COUNT(
- CASE WHEN h.failed > 0 THEN
- 'failed'
- END) AS failed,
- COUNT(
- CASE WHEN h.failed = 0
- AND h.pending > 0 THEN
- 'pending'
- END) AS pending,
- COUNT(
- CASE WHEN h.failed = 0
- AND h.pending = 0 THEN
- 'applied'
- END) AS applied
-FROM (
- SELECT
- host_uuid,
- COUNT(
- CASE WHEN status = 'applied' THEN
- 1
- END) AS applied,
- COUNT(
- CASE WHEN status = 'failed' THEN
- 1
- END) AS failed,
- COUNT(
- CASE WHEN status = 'pending' THEN
- 1
- END) AS pending
- FROM
- host_mdm_apple_profiles hmap
- GROUP BY
- host_uuid) AS h
-WHERE
- EXISTS (
- SELECT
+ count(
+ CASE WHEN EXISTS (
+ SELECT
+ 1 FROM host_mdm_apple_profiles hmap
+ WHERE
+ h.uuid = hmap.host_uuid
+ AND hmap.status = 'failed') THEN
1
- FROM
- hosts
- WHERE
- hosts.uuid = host_uuid
- AND %s)
-`
+ END) AS failed,
+ count(
+ CASE WHEN EXISTS (
+ SELECT
+ 1 FROM host_mdm_apple_profiles hmap
+ WHERE
+ h.uuid = hmap.host_uuid
+ AND hmap.status = 'pending')
+ AND NOT EXISTS (
+ SELECT
+ 1 FROM host_mdm_apple_profiles hmap
+ WHERE
+ h.uuid = hmap.host_uuid
+ AND hmap.status = 'failed') THEN
+ 1
+ END) AS pending,
+ count(
+ CASE WHEN EXISTS (
+ SELECT
+ 1 FROM host_mdm_apple_profiles hmap
+ WHERE
+ h.uuid = hmap.host_uuid
+ AND hmap.status = 'applied')
+ AND NOT EXISTS (
+ SELECT
+ 1 FROM host_mdm_apple_profiles hmap
+ WHERE
+ h.uuid = hmap.host_uuid
+ AND(hmap.status = 'failed'
+ OR hmap.status = 'pending')) THEN
+ 1
+ END) AS applied
+FROM
+ hosts h
+WHERE
+ %s`
- teamFilter := "hosts.team_id IS NULL"
+ teamFilter := "h.team_id IS NULL"
if teamID != nil && *teamID > 0 {
- teamFilter = fmt.Sprintf("hosts.team_id = %d", *teamID)
+ teamFilter = fmt.Sprintf("h.team_id = %d", *teamID)
}
var res fleet.MDMAppleHostsProfilesSummary