简体   繁体   中英

Database to C# in the most efficient way

在此处输入图片说明

I'm working with the above database design in SQLite. The goal is to load the data into a C# application in the shortest amount of time possible. The amount of records in the Address table can go from a couple of 100's to a couple of 10.000's depending on how many Customers there are (not yet integrated in the model). Ultimately, every address in the world should be able to be in the database generating millions of records. This should also be taken in consideration when inserting the data in C#. I know it can't be as fast if there are only 100 records, but it should just be as fast as possible.

For the middle line (Continent -> Address) I came up with the following query:

SELECT * FROM ((((Continent 
LEFT OUTER JOIN Country ON Continent.Code = Country.ContinentCode) 
LEFT OUTER JOIN SubDivision ON Country.Code = SubDivision.CountryCode)
LEFT OUTER JOIN City ON SubDivision.SubDivisionID = City.SubDivisionID)
LEFT OUTER JOIN Address ON City.CityID = Address.CityID)
ORDER BY Continent.Code ASC, Country.Code ASC, SubDivision.SubDivisionID ASC, City.CityID ASC;

Approach 1

This query gets all the one-to-many information from the database in a single table. The way I would approach inserting this in C# is by inserting the 1st row as new data. Then, check every time if the last inserted info is different. If it's different, create a new object, if it's the same, use the last object and this for each table. (Each table represents a model in C#.) The 5 tables would be “converted” to 5 models in a single step.

All good but then there is no information about the spoken languages, used currencies and capitals. This would be added later on then by looking up indexes in the lists of continents, countries, ...

Approach 2

A second approach I'm thinking about is as follow: Query the continents and insert them in a list. Query the languages and currencies and insert them in a list. Query the countries with information about the languages and currencies. This will generate duplicate country records but if sorted, can be sealed with by checking if they're already added or not and just add the language and currency to the list inside the country model. Rinse and repeat for the rest of the data.

By doing this, each time something is inserted to C#, an index will have to be looked up in the lists so no duplicate objects exist. Also multiple queries have to be processed to insert everything.

Are any of my 2 approaches good or is there a 3rd better approach? I will admit I haven't tested either of the above approaches to save programming time and I rather think first about the problem instead of trying something and realizing it's not working.

You can just load every table into dictionary with PK as a key. It would not waste memory or connection bandwidth, would be quite fast, object relations can be looked up easily. Question really is, why would You want to mirror the whole database into Your application.

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