MySql - Is it better to select from many tables with union or using temp tables?
We have a report that users can run that needs to select records from 5 different services. Right not, I am using UNION to combine all the tables in one query, but sometimes, it was just too much for the server and it crashed! I optimized bits and pieces of the query (where's and table joins) and there haven't been any crashes since, but the report still takes a long time to load (ie the query is very slow).
The question is, will mysql perform faster a开发者_JS百科nd more optimally if I create 5 temp tables for the different service types, and then select from all of the temps? Or is there a different idea? I could, of course, just use 5 separate selects and then combine them in the code (php). But I imagine this would cause the report to load even slower...
Any ideas?
Usually the limiting factor in speed is the database, not PHP. I'd suggest running seperate queries and let the PHP do the combining, see if that is faster. If you're not storing all data in arrays or doing other heavy processing, I suspect the PHP way is much faster.
(this was actually meant as a comment but don't have those rights yet..)
精彩评论