Why exactly do you want to create a temp table in the initial SQL? I'm not really sure that's going to solve a performance problem. If your custom SQL is slow, then that's an issue at the database layer. You'll want to look at ways to optimize the query. There's a lot to this, but indexes are probably the biggest thing to consider. My suggestion would be to look at the query execution plan and spend some time working through how to optimize the query.