SQL query taking too much time to run

More than 500ms isn’t outside the realms of normal. Do you have unusual permissions on the categories? Have you tested with an anonymous or new user rather than a staff user?