tags:

views:

459

answers:

5

is there a way to add the results of 2 different queries to a resultset?

something like that:

ResultSet rs ;

i=0;

while(i<=l)


  ResultSet rs1 = select * from tablei;

  rs = rs + rs1; 

 i++;

}

I know that I can do it with union, but I have a lot queries and if I use UNION the query is too slow.

Any idea?

A: 

I don't believe there is any way to add a ResultSet to another. They have no method in the class that does such a thing or updates the ResultSet from the code. ResultSets are designed to receive data from the database and not from developer manipulation, user input or the like.

My suggestion would be to extract the data to an array or something similar and manipulate the data in the code or do the UNION in the query.

Gareth
A: 

Depending on which data access library you use, ResultSet has a method called MoveNextRecordSet(), and SqlDataReader provides NextResult().

Create a stored procedure to return several result sets (i.e. several SELECT statements in one sp), and navigate through the result sets using these methods.

devio
+1  A: 

Are you doing a UNION or a UNION ALL? The latter shouldn't be much different from doing it yourself (although I'd expect doing it yourself to be slower).

Dustin
I'm using union. Each query takes 5948 mseg without union (2 queries 13000 mseg). But if I use union with 2 queries takes 56125 mseg. I have to do a lot of queries...
hangar
So use UNION ALL. UNION implicitly adds a DISTINCT operation, which both slows things down and changes your results. UNION ALL is a simple concatenation of results.
Dave Costa
A: 

I'd be surprised if you found a method that has better performance than UNION in the database. Union is doing what you want, and the database server will have optimised this as best as they can. You'd essentially be re-inventing the wheel.

If your UNION is too slow, then try looking into whether your database could do with better indexing. You should also do some timing analysis on the individual queries, compared with the UNION option. I'd expect one or the other queries is the slow bit, rather than the UNION.

Neil Barnwell
A: 

IF that Union takes too much time to complete maybe you should consider changing your indexes on the tables that you use.

Did you check your index fragmentation?

See if you add the right indexes if you can speed up the query that way. I do not think that a while being used like that will be quicker than a union all.

Coentje