diff --git a/server/datastore/mysql/mdm.go b/server/datastore/mysql/mdm.go index 6321d6fb39..147d9d65f8 100644 --- a/server/datastore/mysql/mdm.go +++ b/server/datastore/mysql/mdm.go @@ -61,24 +61,29 @@ END AS platform // These subqueries are only used for the all-hosts listing; host-scoped // requests go through listMDMCommandsByHostIdentifier instead. func getMDMCommandsSubqueries() (appleStmt, windowsStmt string) { + // Apple branch joins the underlying nano_* tables directly instead of + // going through the nano_view_queue VIEW. The view bakes + // "ORDER BY q.priority DESC, q.created_at" into its definition, which + // MySQL re-materializes on every query — defeating the outer LIMIT and + // forcing a full join + sort regardless of page size. The host-scoped + // path (see listMDMCommandsByHostIdentifier) already bypasses the view + // for the same reason. appleStmt = ` SELECT - nvq.id as host_uuid, - nvq.command_uuid, - COALESCE(NULLIF(nvq.status, ''), 'Pending') as status, - COALESCE(nvq.result_updated_at, nvq.created_at) as updated_at, - nvq.request_type as request_type, + q.id AS host_uuid, + c.command_uuid, + COALESCE(NULLIF(r.status, ''), 'Pending') AS status, + COALESCE(r.updated_at, q.created_at) AS updated_at, + c.request_type, h.hostname, h.team_id, - nvq.name -FROM - nano_view_queue nvq -INNER JOIN - hosts h -ON - nvq.id = h.uuid -WHERE - nvq.active = 1 + c.name +FROM nano_enrollment_queue q + INNER JOIN nano_commands c ON q.command_uuid = c.command_uuid + INNER JOIN hosts h ON h.uuid = q.id + LEFT JOIN nano_command_results r + ON r.command_uuid = q.command_uuid AND r.id = q.id +WHERE q.active = 1 ` // The Windows sub-statement is itself a UNION ALL of two branches: one