You internal server error is may be due to the Fastcgi script idle timeout .
Because when your queries takes longer than the idle timeout. Fastcgi script
gives up and reports and internal server error and logs in apache logs as
incomplete header received from server as it has not got any response which
it is waiting for.
So try to increase your Fastcgi idle timeout and set it to double the
current value you have and tell me what is the result