mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 08:58:41 +00:00
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:
parent
074ea7dc8d
commit
6ae052c17d
1 changed files with 45 additions and 44 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue