Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
nirnir2
Starting Member
20 Posts |
Posted - 2013-06-08 : 09:01:42
|
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 statistics95% of the remarks belong to clients40% of the remarks belong to policy10% of the remarks belong to claims1% of the remarks belong to documentInstead of having clientID,policyID,claimID,documentID fields in remarks table I createdrelation 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 =31. 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
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-10 : 03:21:10
|
quote: Instead of having clientID,policyID,claimID,documentID fields in remarks table I createdrelation 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)....--ExampleRemarkId, Table Type, RecID1, 1,1) ,(1,2,1),(1,3,2)Instead of above 3 records you can do like thisRemarkID, ClientId, PolicyID, ClaimID, DocumentId--------------------------------------------------1, 1, 1, 2, NullYou can get respective name by joining master tables together --Chandu |
|
|
nirnir2
Starting Member
20 Posts |
Posted - 2013-06-10 : 03:40:42
|
quote: Originally posted by bandi
quote: Instead of having clientID,policyID,claimID,documentID fields in remarks table I createdrelation 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)....--ExampleRemarkId, Table Type, RecID1, 1,1) ,(1,2,1),(1,3,2)Instead of above 3 records you can do like thisRemarkID, ClientId, PolicyID, ClaimID, DocumentId--------------------------------------------------1, 1, 1, 2, NullYou 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 usesthe remarks table which will force me to add more fields to remarks table and add index to each one . |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-10 : 04:40:00
|
why another index per field? You can create index on 1 column and include other columns over there....--Chandu |
|
|
nirnir2
Starting Member
20 Posts |
Posted - 2013-06-10 : 04:57:51
|
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 toselect all records with claimID=190 , regardless of clientID,policyID values .How does this index can help me ? |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
nirnir2
Starting Member
20 Posts |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-11 : 01:40:57
|
quote: Originally posted by nirnir2
quote: Originally posted by bandi Check this onehttp://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 operationSELECT d.Description DocumnetName, other columnsFROM @TBL_relation r JOIN @TBL_document d ON r.DocId = d.DocId--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
nirnir2
Starting Member
20 Posts |
Posted - 2013-06-11 : 03:22:45
|
quote: Originally posted by bandi
quote: Originally posted by nirnir2
quote: Originally posted by bandi Check this onehttp://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 operationSELECT d.Description DocumnetName, other columnsFROM @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 fieldfrom 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 =3how do I get the description field of the other three tables ? |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-11 : 05:11:25
|
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.ClientIDJOIN @TBL_Policy p ON r.tableType=2 AND r.recID =p.PolicyIDJOIN @TBL_Claim cl ON r.tableType=3 AND r.recID =cl.ClaimIDJOIN @TBL_Document d ON r.tableType=4 AND r.recID = d.docID--Chandu |
|
|
nirnir2
Starting Member
20 Posts |
Posted - 2013-06-11 : 09:43:10
|
Thanks you both |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-11 : 09:51:27
|
quote: Originally posted by nirnir2 Thanks you both
Welcome--Chandu |
|
|
|
|
|
|
|