I am working on a small project that generates SQL queries from a list of column names. These columns are spread across several tables, but they’re all in one database. Some columns aren’t real columns, i.e., they’re “range” pseudocolumns calculated as the difference between two columns over a range of time. My code works, but it generates a huge query with tons of joins, outer joins, aliases for use in range calculations (which my generator must keep track of and reuse as columns are added), etc.
The goal of all of this is to increase performance by getting a bunch of data out of SQL directly out of the DB with minimal post-processing. My question, then, is whether or not there would be a significant performance hit if I were to select each column individually (in order to simplify my generator code) and union the results. Are there other (better) ways to do this without a ton of hard-to-read joins and aliases?
If you have the necessary indexes on the table, I don’t think you’ll see a huge performance hit from the joins compared to unions.
But the best way to find this out, is to view the execution plan of each so that you can make the best decision based off of the given information. And you’ll also be able to see where/if additional indexes will increase performance.