>OK, might be premature to make a suggestion, but sometimes breaking up into multiple steps can help.
>
>I just took a query that was running way too long and did a "divide and conquer". Grabbed a chunk into a table variable and then did a subsequent query from that - brought the time down significantly. Not saying that would necessarily solve your problems, but something to at least consider.
Yes, this is all good. I wish I could have all that by pressing one key. :)
In overall, the query contains all the fields necessary for the output, the fields for the conditions and the INNER JOINs. I see this one as you said. I have been thinking about that since this afternoon. For such kind of flexibility to the user, I'd say also that we need to break it out. I will start by splitting it in two. Thus, I will only extract the primary keys with the conditions at first. Then, I will do another SQL select command to collect all the output fields with the INNER JOINs.