简体   繁体   中英

Returning parent-child data from SQL query

I have an asp.net page which needs to return an object with a one-to-many relationship. ie there is a single header row followed by an unspecified number of data rows. Typically there will be between 1 and 10 rows, so I'm not dealing with a huge amount of data here - just a page that is called frequently.

I know that the OLE provider supports the SHAPE command which allows hierarchical data to be returned, but I'm using SQLDataReader (ADO?) which doesn't support it. The question is, what is best practice/best performance here?

  1. INNER JOIN in the query and return a single table containing the header and data rows, meaning the header is repeated in every row after the first. Is this generating unnessary data traffic or is there some hidden optimisation behind the scenes?
  2. Make two separate calls to SQLCommand.executeReader() - one to return the header row and one for the variable number of data rows. But this incurs the overhead of issuing 2 separate queries instead of 1.
  3. Use an API that supports SHAPE instead.
  4. Make a single call to SQLCommand.executeReader() but with the query containing 2 select statements to return exactly 2 data sets. Then call SQLDataReader.nextResult() after processing the header row. This seems like a good idea for solving my original problem, as there is only one header row. There are other situations where this is not the case however, in which case #4 wouldn't be an option. Furthermore the data access is sequential and my application needs to use the header both before and after I use the data rows. So I'd have to write the header row to an in-memory DataTable before calling nextResult().

I don't have experience with SHAPE so I don't know if it performs well. Of the other three options, Option #4 is absolutely the most efficient because it minimizes both trips to the database and duplication of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM