One idea might be to write a seperate daemon that your CGI script connects to via sockets. The daemon would then execute queries on the model on your script's behalf and return the results. While this introduces the overhead of communication with the daemon, it saves constantly re-loading the large file (keeping it in memory only once no matter how many simultaneous queries there are from the CGI script) so it should be a fairly substantial overall win. It has the added advantage that should you need to scale this application up further in the future it should be trivial to move the daemon to a seperate machine.