| Author |
Topic |
|
Obelixous
Starting Member
3 Posts |
Posted - 2003-04-07 : 21:59:33
|
| Within my database, I have two tables (actually there are others as well).Folder table - contains information corresponding to foldersDocument table - contains information corresponding to documentsThe structure of the tables is as follows:Folder table{FolderIdFolderNameNextDocIdOther fields...}Document table{FolderIdDocIdDocumentNameOther fields...}Whenever a document is added to a folder, NextDocId corresponding to folder record is incremented and updated and a record is inserted in document table. The performance in such a setup degrades significantly if many clients try to add documents to the same folder.I have the following solution, make DocId in document table an identity column and remove NextDocIndex from folder table.Does somebody have another solution for this problem? |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2003-04-07 : 22:32:31
|
| What exactly does NextDocID mean/do?Is a FolderName unique?Is the DocumentName unique per Folder?DavidM"SQL-3 is an abomination.." |
 |
|
|
Obelixous
Starting Member
3 Posts |
Posted - 2003-04-08 : 00:45:12
|
| Regarding NextDocId, please consider the followingContents of Folder tableFolderId FolderName NextDocId1 Fol1 32 Fol2 5Contents of Document TableFolderId DocId DocumentName1 1 Doc111 2 Doc122 1 Doc212 2 Doc222 3 Doc232 4 Doc24When a document is to be added to folder with name Fol2, the corresponding record is locked, the current value of NextDocIndex, i.e. 5 is maintained in a local variable and the record is updated using incremented value 6.i.e. the updated folder table is 2 Fol2 6Then a record is inserted into the document table as follows:2 5 Doc25FolderName and DocumentName are not central to this issue, you can consider them to be unique or non-unique if you want. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-04-08 : 07:26:36
|
| I think the reason why byrmol is asking is: why do you even need a folderId or documentId field? why not just make the document name and folder name the primary keys? Then you don't have to worry about maintaining extra ID fields. Of course, if the folder names can or will change often, this may not be the best approach, but if they remain relatively constant I would recommend it.With the ID fields, and no other constraints, you may end up with two folders or documents with the same name -- and it doesn't seem to me (without knowing what all this is modelling) like you would want that.- Jeff |
 |
|
|
aiken
Aged Yak Warrior
525 Posts |
Posted - 2003-04-08 : 15:32:36
|
| Hey, what do you guys think about natural keys versus surrogate keys?[ducks]Cheers-b |
 |
|
|
adm
Starting Member
3 Posts |
Posted - 2003-04-08 : 16:49:10
|
| Maybe I'm missing something, but isn't NewDocID a derived value? It appears at first glance you could just omit it and derive it on-the-fly when necessary. |
 |
|
|
Obelixous
Starting Member
3 Posts |
Posted - 2003-04-08 : 20:55:54
|
| For queries raised by byrmol and jsmith8858i shall add more details, the structuring is slightly more complex, the folder structure can be hierarchical, so the actual table structure is like thisFolder table { FolderId ParentFolderIdFolderName NextDocId Other fields... } So, FolderName is unique for a particular ParentFolderId. Similarly DocumentName is unique for a particular FolderId. There are constraints defined on the Folder and Document Table to ensure this.Also, the FolderId, DocumentId combination is used in other tables to uniquely identify a document. For e.g. I have a page table, containing information about pages in a document. For aikenI don't understand the applicability of natuaral and surrogate keys in this system. Perhaps you could elaborate further.For admI could not understand your suggestion, more details / elaboration shall be greatly appreciated. |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-04-09 : 09:27:22
|
| I agree with adm, your NextDocID is actually a misnomer - it should have been named NextDocIndex. You need to know what should be DocID for the newly added document in this folder, right? It can be easily calculated when required, for example:DECLARE @NextDocIndex INTSELECT @NextDocIndex = COUNT(DocID) FROM Document WHERE FolderID = @FolderIDIF @NextDocIndex IS NULL SET @NextDocIndex = 1ELSE SET @NextDocIndex = @NextDocIndex + 1There you have it!OS |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-04-09 : 11:21:32
|
| I think the question is, then, why not use:Foldername / Documentnameas your unique key, and reference documents that way? then you don't have to worry about calculating the document ID for each document.Or, just create an indentity field for your documentID (like you mentioned) and use that. If you need to display nice "document numbers" from 1-n for each document in a folder, if you have an identity documentId you can do it this way:select document.*, (select count(*) from document a where document.folder = a.folder and document.documentId <= a.documentID) as DocumentNumberFromDocumentsor if you use the names as the primary key:select document.*, (select count(*) from document a where document.folder = a.folder and document.docName <= a.docName) as DocumentNumberFromDocumentsjust some ideas ...- Jeff |
 |
|
|
|