Set Analysis can be really expensive, maybe some transformations in the script can avoid the use of that on each column. You said that you db contains 300M records, do you load the whole db in qv ? try to decrease the size of the information by keeping only column that are really relevant, and doing some aggregations in the script to get less rows.
When you use your document on the full client you can have information about the resource consummation of each object with the document properties, general tab, Memory statistic, it will produce some files indicating which objects are the most expensive. And on the server you will find a qv application that load these files to help you to analyze your one.