简体   繁体   中英

ADO.NET SQL Query Join?

I am writing a little test app which connects to a SQL Server database which has 2 tables in it. Table 1 is a list of data with a non unique key which points to multiple rows in table 2. At the moment what I do is traverse each record in Table 1 and then get the relevant records from Table 2 with a separate query. Table 2 has over 26 million records in it and this process takes a very long time. The data in Table 1 could have just a few hundred records in it or anything up to about 1 million.

Is there a way of speeding up this data access? Perhaps using a table join to get all the data in one query? Or anything else?

Sorry I cannot post the database tables or current code as I am under NDA as it is very sensitive data. The current code is sort of irrelevant here anyway as I am looking for a totally new (and better) way of doing this.

Please note I am using ADO within .NET with C#.

EDIT: I am okay doing the JOIN query but more asking if this will be more efficient with ADO? I know it would be more efficient in general but not sure if ADO can handle this. Also looking for some example C# code to do this. Thanks.

You can get all the data in one go with a JOIN like this:

SELECT t1.NonUniqueKey, t2.*
FROM Table1 t1
    JOIN Table2 t2 ON t1.NonUniqueKey = t2.NonUniqueKey

You'll want to consider putting an index on NonUniqueKey, but this should be better for you than executing n queries, one per row in Table1

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