User-943250815 posted
And working fine but if in production the view contains 1 million records it return nothing
Looks a lot of records. Did you checked how much time takes to have all records ready to be transmmited to the client?
IMHO you are having a timeout on client side, so when records are ready, client already disconnected.
Of course you can set timeout on client, but I suggest you try to reduce number of records needed, if you can.