SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 database architecture help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nirnir2
Starting Member

15 Posts

Posted - 06/08/2013 :  09:01:42  Show Profile  Reply with Quote
I have a table remarks with two fields (ID:int, text:ntext)

Lets say I have four more tables (clients,policy,claims,documents)
for the simplicity they also have two fields (id:integer,name:nvarchar)

each one of the four tables can have one-to-many relation with the remarks table
(one row in clients can have few rows in remarks)
one row in remarks can belong to one row in clients and also to one row in policy,claims,documents tables)

usage statistics
95% of the remarks belong to clients
40% of the remarks belong to policy
10% of the remarks belong to claims
1% of the remarks belong to document

Instead of having clientID,policyID,claimID,documentID fields in remarks table I created
relation table (remarkID int,tableType smallint ,recID int)
(tableType 1=clients, 2=policy, 3=claims,4=document)
(Actually I can have some more tables uses remarks)

if I want to get all the remarks of clientid=3 I do :

select * from @TBL_relation r left outer join @TBL_remarks m on r.remarkID = m.id
where r.tableType=1 and recID =3

1. is this the best approach for that situation ?

2: In the application I need to see list of remarks belong to
specific client with the columns
text,policyName,claimName,documentName

what is the most efficient way to get the other columns ?
(policyName,claimName,documentName)




DECLARE @TBL_clients TABLE (ID INT,description VARCHAR(50))
INSERT INTO @TBL_clients VALUES (1, 'clients 1'), (2, 'clients 2' )


DECLARE @TBL_Policy TABLE (ID INT,description VARCHAR(50))
INSERT INTO @TBL_Policy VALUES (1, 'Policy 1'), (2, 'Policy 2' )

DECLARE @TBL_Claim TABLE (ID INT,description VARCHAR(50))
INSERT INTO @TBL_Claim VALUES (1, 'Claim 1'), (2, 'Claim 2' )

DECLARE @TBL_document TABLE (ID INT,description VARCHAR(50))
INSERT INTO @TBL_document VALUES (1, 'document 1'), (2, 'document 2' )

DECLARE @TBL_remarks TABLE (ID INT,description VARCHAR(50))
INSERT INTO @TBL_remarks VALUES (1, 'remarks 1'), (2, 'remarks 2' ),(3, 'remarks 3'), (4, 'remarks 4' )



DECLARE @TBL_relation TABLE (remarkID int,tableType smallint ,recID int)
INSERT INTO @TBL_relation VALUES (1, 1,1) ,(1,2,1),(1,3,2),(2,3,2)


bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/10/2013 :  03:21:10  Show Profile  Reply with Quote
quote:
Instead of having clientID,policyID,claimID,documentID fields in remarks table I created
relation table (remarkID int,tableType smallint ,recID int)
(tableType 1=clients, 2=policy, 3=claims,4=document)
(Actually I can have some more tables uses remarks)

I think this is not the efficient way(approach).... 
because You have to insert huge number of records to @TBL_Relation table instead of 1 row (for 1-to-many relation)....
--Example
RemarkId, Table Type, RecID
1, 1,1) ,
(1,2,1),
(1,3,2)

Instead of above 3 records you can do like this
RemarkID, ClientId, PolicyID, ClaimID, DocumentId
--------------------------------------------------
1,        1,        1,         2,      Null

You can get respective name by joining master tables together


--
Chandu
Go to Top of Page

nirnir2
Starting Member

15 Posts

Posted - 06/10/2013 :  03:40:42  Show Profile  Reply with Quote
quote:
Originally posted by bandi

quote:
Instead of having clientID,policyID,claimID,documentID fields in remarks table I created
relation table (remarkID int,tableType smallint ,recID int)
(tableType 1=clients, 2=policy, 3=claims,4=document)
(Actually I can have some more tables uses remarks)

I think this is not the efficient way(approach).... 
because You have to insert huge number of records to @TBL_Relation table instead of 1 row (for 1-to-many relation)....
--Example
RemarkId, Table Type, RecID
1, 1,1) ,
(1,2,1),
(1,3,2)

Instead of above 3 records you can do like this
RemarkID, ClientId, PolicyID, ClaimID, DocumentId
--------------------------------------------------
1,        1,        1,         2,      Null

You can get respective name by joining master tables together


--
Chandu



The problem with this way is that I'll need to define another index per field .
most of the fields will be empty most of the times .
Do you know for sure that sqlserver ignores null values in index memory usage ?

Another thing is that actually I have more tables which uses
the remarks table which will force me to add more fields to
remarks table and add index to each one .
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/10/2013 :  04:40:00  Show Profile  Reply with Quote
why another index per field? You can create index on 1 column and include other columns over there....

--
Chandu
Go to Top of Page

nirnir2
Starting Member

15 Posts

Posted - 06/10/2013 :  04:57:51  Show Profile  Reply with Quote
quote:
Originally posted by bandi

why another index per field? You can create index on 1 column and include other columns over there....

--
Chandu


if I have ONE index on clientID,policyID,claimID and I want to
select all records with claimID=190 , regardless of clientID,policyID values .
How does this index can help me ?
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/10/2013 :  05:21:55  Show Profile  Reply with Quote
Check this one
http://blog.sqlauthority.com/2010/03/09/sql-server-improve-performance-by-reducing-io-creating-covered-index/

--
Chandu
Go to Top of Page

nirnir2
Starting Member

15 Posts

Posted - 06/10/2013 :  15:54:20  Show Profile  Reply with Quote
quote:
Originally posted by bandi

Check this one
http://blog.sqlauthority.com/2010/03/09/sql-server-improve-performance-by-reducing-io-creating-covered-index/

--
Chandu



Thanks
using this way will be more efficeint in fields which are often filled .
I'll want to use the first way (relation table)for fields that are rarly filled .
can you help me with the second part of my question,
how to display the documentName when using the relation table .
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/11/2013 :  01:40:57  Show Profile  Reply with Quote
quote:
Originally posted by nirnir2

quote:
Originally posted by bandi

Check this one
http://blog.sqlauthority.com/2010/03/09/sql-server-improve-performance-by-reducing-io-creating-covered-index/

--
Chandu



Thanks
using this way will be more efficeint in fields which are often filled .
I'll want to use the first way (relation table)for fields that are rarly filled .
can you help me with the second part of my question,
how to display the documentName when using the relation table .



Basic and efficient way to get documnetName is as usual JOIN operation
SELECT d.Description DocumnetName, other columns
FROM @TBL_relation r
JOIN @TBL_document d ON r.DocId = d.DocId

--
Chandu

Edited by - bandi on 06/11/2013 01:41:35
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/11/2013 :  02:27:44  Show Profile  Reply with Quote
For rarely filled fields, you can optimize the NULL value storage by declaring them as SPARSE columns. And you could organize them by creating a columnset XML column for easily managing DML operations like INSERT,UPDATE etc

More details here

http://visakhm.blogspot.in/2010/03/sparse-columns-and-column-sets-in-sql.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

nirnir2
Starting Member

15 Posts

Posted - 06/11/2013 :  03:22:45  Show Profile  Reply with Quote
quote:
Originally posted by bandi

quote:
Originally posted by nirnir2

quote:
Originally posted by bandi

Check this one
http://blog.sqlauthority.com/2010/03/09/sql-server-improve-performance-by-reducing-io-creating-covered-index/

--
Chandu



Thanks
using this way will be more efficeint in fields which are often filled .
I'll want to use the first way (relation table)for fields that are rarly filled .
can you help me with the second part of my question,
how to display the documentName when using the relation table .



Basic and efficient way to get documnetName is as usual JOIN operation
SELECT d.Description DocumnetName, other columns
FROM @TBL_relation r
JOIN @TBL_document d ON r.DocId = d.DocId

--
Chandu


Thanks but it is not so simple.
TBL_relation doesn't have docId,
it has fileType,RecId columns and it need to join all the relevant tabels .
let's assume all the four tables are stored using the TBL_relation way .
while selecting list of remarks I want to get the description field
from all four tables (if included in TBL_relation )
The basic select to get list of client remarks without description field would be :

select * from @TBL_relation r left outer join @TBL_remarks m on r.remarkID = m.id
where r.tableType=1 and recID =3

how do I get the description field of the other three tables ?

Edited by - nirnir2 on 06/11/2013 03:30:44
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/11/2013 :  05:11:25  Show Profile  Reply with Quote
select *
from @TBL_relation r
left outer join @TBL_remarks m on r.remarkID = m.id
JOIN @TBL_clients c ON r.tableType=1 AND r.recID =c.ClientID
JOIN @TBL_Policy p ON r.tableType=2 AND r.recID =p.PolicyID
JOIN @TBL_Claim cl ON r.tableType=3 AND r.recID =cl.ClaimID
JOIN @TBL_Document d ON r.tableType=4 AND r.recID = d.docID

--
Chandu
Go to Top of Page

nirnir2
Starting Member

15 Posts

Posted - 06/11/2013 :  09:43:10  Show Profile  Reply with Quote
Thanks you both
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 06/11/2013 :  09:51:27  Show Profile  Reply with Quote
quote:
Originally posted by nirnir2

Thanks you both


Welcome

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000