If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > DB Performance

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-28-10, 05:08
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
DB Performance

Dear All,
I have an object that is represented with a set of key-value pairs. I am considering two approaches:
1) to make two tables; one for the object other for key-value pairs and then to connect them with an association table or
2) to put all key value pair in a table that represent object.
The first approach avoids redundancy and the second should be more performant.
What is a solution for this problem? When to use approach 1 and when 2? If I apply index in the approach1 are they are going to provide better performance?
Thankfully
Reply With Quote
  #2 (permalink)  
Old 09-29-10, 09:08
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
By option 2 do you mean that for an object like Employee with key-value pairs like(ID=123, Name=John, Salary=100, HireDate=01-APR-2010) you would create a table like this:
Code:
create table employee 
( id integer primary key
, name varchar(10)
, salary number
, hiredate date
);
?

If so, it would get my vote!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 09-29-10, 09:31
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
reply

Thanks for the reply,
I am looking for the solution of the problem that emerges in case that the number of key value pairs is not known. Is there any way to define a table object in run time?
The second question would be is it always more performant to avoid association tables even on the costs of data redundancy?
Thanks
Reply With Quote
  #4 (permalink)  
Old 09-29-10, 09:41
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by krontrex View Post
I am looking for the solution of the problem that emerges in case that the number of key value pairs is not known. Is there any way to define a table object in run time?
Which DBMS?
In Postgres you could use the hstore datatype:
Code:
CREATE TABLE object_attributes
(
   object_id     integer        NOT NULL PRIMARY KEY,
   object_name   varchar(100)   NOT NULL,
   attributes    hstore         NOT NULL
);

INSERT INTO object_attributes
(object_id, object_name, attributes)
VALUES
(1, 'something', ('color => blue, size => XL'));

INSERT INTO object_attributes
(object_id, object_name, attributes)
VALUES
(2, 'another thing', ('resolution => "1900x1600"'));
Reply With Quote
  #5 (permalink)  
Old 09-29-10, 10:13
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
Yes Postgres,
Thanks I have not heard of hstore.
Again about performance?
Why is the solution with an association table less performant? Does it help when I use indices?
Thanks
Reply With Quote
  #6 (permalink)  
Old 09-29-10, 10:40
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
An association table is used to implement a many to many relationship. For it to be applicable in this instance would imply you intend to store every possible key value pair combination in one table and associate these with objects in the association table.

Is that what you had in mind?
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #7 (permalink)  
Old 09-30-10, 04:33
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
Yes exactly, and my question is related to performance.
As I could see, the solution to save key value pairs in respective object table is much more preferred than to store all pairs in one table which will be associated through a table with object tables.
Why?
Is it because it employs two tables less?
Reply With Quote
  #8 (permalink)  
Old 09-30-10, 04:39
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
I didn't say one was preferable over the other. With the association table you do at least have something approximating relational integrity. I also don't know anything about Postgres. If there is a significant number of rows then table\ index partitioning would help keep the B-Tree level down, but I don't know if postgres supports this or if you would have to roll your own. Or, indeed, if it would be necessary.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #9 (permalink)  
Old 09-30-10, 05:48
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
Best practice

What about other DBMSs?
So, sometimes is not so effective to stick to the normalization rules. Is there any best practice with the respect to this problem?
Reply With Quote
  #10 (permalink)  
Old 09-30-10, 06:14
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by krontrex View Post
What about other DBMSs?
For Oracle I have seen it suggested that a solution is to have an XMLTYPE column to contain such arbitrary name/value pairs - search for XML here. Tom Kyte, the Oracle guru who runs that website, calls your requirement the "funky data model", and he's about as keen on it as I am!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #11 (permalink)  
Old 09-30-10, 06:26
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
SQL Server also supports XML. Note though that the use of XML is not in violation of normalisation. The XML datatype is still atomic to the database engine - shredding would be done by an application layer.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #12 (permalink)  
Old 09-30-10, 07:19
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by pootle flump View Post
SQL Server also supports XML. Note though that the use of XML is not in violation of normalisation. The XML datatype is still atomic to the database engine - shredding would be done by an application layer.
True, but it does make querying a lot harder when you want to filter on values that are held somewhere in the XML data.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #13 (permalink)  
Old 09-30-10, 07:45
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by andrewst View Post
True, but it does make querying a lot harder when you want to filter on values that are held somewhere in the XML data.
Isn't that the point in using XML inside a database?

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #14 (permalink)  
Old 09-30-10, 09:53
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by Pat Phelan View Post
Isn't that the point in using XML inside a database?
Of course!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #15 (permalink)  
Old 09-30-10, 23:19
Wholesalerworld Wholesalerworld is offline
Registered User
 
Join Date: Sep 2010
Posts: 1
Thank you all the answers. Very useful.
Reply With Quote
Reply

Tags
design, keypair, performance

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On