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)
<img width="1166" alt="Screenshot 2023-03-03 at 3 41 22 PM"
src="https://user-images.githubusercontent.com/73313222/222861016-4adab32a-697b-48ab-9e1e-6043ea9ba561.png">

Old query (900ms)
<img width="1166" alt="Screenshot 2023-03-03 at 3 41 49 PM"
src="https://user-images.githubusercontent.com/73313222/222861104-a6f4758b-0c17-4d25-b0aa-20292c932108.png">
This commit is contained in:
gillespi314 2023-03-06 12:41:27 -06:00 committed by GitHub
parent 074ea7dc8d
commit 6ae052c17d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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