开发者

Saving multiple items per single database cell

开发者 https://www.devze.com 2022-12-30 17:19 出处:网络
i have a countries list. Each user can check multiple countries. Once saved, this \"user country list\" will be used to get whether other users fit into countries certain user chose.

i have a countries list. Each user can check multiple countries. Once saved, this "user country list" will be used to get whether other users fit into countries certain user chose.

Question is what would be the most efficient approach to this problem...

I have one, one to save user selection as delimited list like Canada,USA,France ... in single varchar(max) field but problem with it would be that once user from Germany enters page i perform this check on. To search for Germany i would be needed to get all items and un-delimit each field to check against value or to use sql 'like' which again is pretty damn slow..

If you have better solution or some开发者_开发百科 tips i would be glad to hear.

Just to make sure, many users will have their own selections of countries from which and only they want to have users to land on their page. While millions of users will reach those pages. So the faster approach will be the better.

technology, MSSQL and ASP.NET

thanks


You should not store a list of values in one cell. Consider having a separate table that stores each of the selected countries with a foreign key reference to the user table. This is standard Database Normalization.

PLEASE don't go down the route you're thinking of, storing multiple entries in one field. I've had to re-write more applications because of bad database design than for any other reason, and that is a bad design.

Added

I have this poster on my wall at work: http://www.informationqualitysolutions.com/FreeStuff/rettigNormalizationPoster.pdf

One of my predecessors was a newbie to DB Design, and this helped her a lot. I keep it for any new hires that may need it. It explains normalization very nicely, with examples.


Do not save delimited fields into your database. Your database will not be normalized.

You need a many-to-many table for users and countries:

UserId
CountryId

If you do start using a delimited field, you end up needing to parse it (either in SQL or your Code). It is more difficult to query and optimize.


In this case, you want will want to create a table called UserCountries (or some such) which would store the UserID and CountryID. This is a standard relational construct. To beginners, it seems strange and too involved, but this structure makes it very easy and very fast to write flexible queries against this type of data. No delimiting required!


I think it would be better to use a UserCountry table, which contains a link to the User and the Country table. This creates a lot more possibilities to query against the database. Example queries that are much simpler this way:

  • Number of Countries per user
  • All users which selected a particular country
  • Sort all popular countries


Do not store multiple countries in a single field. Add 2 additional tables - Countries (ID, Name) and UserCountries (UserID, CountryID)

0

精彩评论

暂无评论...
验证码 换一张
取 消