?
Solved

Best way to handle SETS in SQL SERVER 2008 R2

Posted on 2015-02-10
6
Medium Priority
?
122 Views
Last Modified: 2015-02-10
I have two tables. One represents a person, and another is a one-to-many child table that has certain characteristics of that person. Each person can have 0, 1 or many characteristics.  

create table people (id int, name varchar(25));
insert people (id, name) VALUES (1, 'Scott'), (2, 'Joe');

create table characteristics (characteristic int, characteristic_text varchar(25));
insert characteristics (characteristic, characteristic_text) VALUES
(1, 'Fat'), (2, 'Skinny'), (3, 'Ugly'), (4, 'Toothsome');

create table people_characteristics (person int, characteristic int);
insert people_characteristics (person, characteristic) VALUES
(1, 1), (1, 3), (2, 4);

Open in new window


So here is the question. I have a web page where people update the characteristics and the results are submitted in a form using a multi-select. When users make changes, I delete all the old values and replace them with the new ones, like this

delete from people_characteristics where person=1;
insert people_characteristics (person, characteristic) VALUES
(1, 1), (1, 2);

Open in new window


This strikes me as painfully inefficient. Does SQL Server have any other way to manage a set of boolean values? I understand that if I was truly enterprising, I could assign each characteristic a power of two and then store a sum, but I am looking for an extensible (i.e. easy) way of doing this.

Here's a fiddle http://sqlfiddle.com/#!3/df792/1
0
Comment
Question by:mankowitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40600573
this is the normalized way of doing it.
any other method is not really normalized, but then, it might not be the most efficient in regards to queries ...

now, in your "update" you "delete all + insert all" ... you could consider to delete only those that are not set anymore, and insert new ones.
however, in terms of efficiency, if you have that table in sql server with a clustered index on the personid, this will touch 1 page block at max, and hence be the most efficient solution for that part.

for the queries (to find all persons matching a certain "profile", you might consider adding a denormalized version of those settings on the person table, using for example some XML field (which could be indexed eventually)

it depends on the usage of those "settings/properties"
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40600578
It might make more sense to use updatable recordset objects in your front-end, and then just update, instead of an DELETE-INSERT.

What front-end are you using, and I recommend adding that zone to this question.

>Does SQL Server have any other way to manage a set of boolean values?
Nothing optimized for boolean data types.
0
 
LVL 24

Author Comment

by:mankowitz
ID: 40600627
for the time being, the scope of the project is pretty small. It includes the editing of various characteristics (as above) and running a monthly report showing the aggregate number of each of the characteristics in the community. In other words, how many fat people, how many ugly people for each month.

The front end is PHP, but I'm not sure that helps. For example, If I stored an object in the master table, I could accomplish my changes with a single update

UPDATE people SET characteristics='3,6,22,66' WHERE id=3

The problem with that is that my monthly query would be slower - something like this:

SELECT ....
WHERE characteristics like '%'+c.characteristic+'%'
FROM people p CROSS JOIN characteristics c
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40600651
As I wrote: better go the normalized way: then both the reporting and the OLAP queries will run just fine.
for the monthly reporting you may consider doing a copy of the actual table away to a dedicated reporting db, so any reporting will not pull the resources from the production database.
0
 
LVL 24

Author Closing Comment

by:mankowitz
ID: 40602162
I was hoping for an easy analog of MySQL's SET type, but no luck.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question