how to maintain wait list in database table.Hi i want maintain wait list on database table.Just like linked list.
Page 1 of 1
14 Replies - 2984 Views - Last Post: 08 September 2008 - 10:35 AM
#1
how to maintain wait list in database table.
Posted 22 August 2008 - 03:47 AM
Just like linked list.
I need to maintain the wait list position,whenever i want add new entry to wait list with postion,then i need to arrange rearrange the existing position. how can you do that in better performance way.
Ex: wait list have below entries.
1
2
3
4
5
i need to add the new entry to the position 3 and need to move the existing 3 and4 to 4 and 5.
Replies To: how to maintain wait list in database table.
#2
Re: how to maintain wait list in database table.
Posted 23 August 2008 - 03:22 AM
1: lock the table / start transaction
2: renumber the entries that are equal or above to the item you want to insert
3: insert the new item
4: release the lock / commit.
Dont forget to think about how to remove items. Perhaps you will need to do that in a similar way, unless the continuity of the numbers are not important (but i guess they are)
#3
Re: how to maintain wait list in database table.
Posted 23 August 2008 - 06:34 AM
kemparaju, on 22 Aug, 2008 - 06:47 AM, said:
My first question would be why?
Understood that you want an ordered list, but is 1,2,3,4..N somehow meaningful? The easiest way to insert between 3 and 4 is to have a float field and add 3.5. Between 3 and 3.5, 3.25, and so on. Is this sufficient?
If you must see 1 through whatever as integers, you could probably fake it with a view, depending on your database.
#4
Re: how to maintain wait list in database table.
Posted 24 August 2008 - 05:28 AM
#5
Re: how to maintain wait list in database table.
Posted 24 August 2008 - 07:39 AM
Trogdor, on 24 Aug, 2008 - 08:28 AM, said:
If you work with ints, you'll eventually run out of numbers... renumbering is just not a natural function for the data layer to perform. If it is a requirment, you need to reconsider your schema.
Another approach would be to have a id number and a prior id number in a table. This will avoid a precision crash, but is real ugly to display with sql and requires touching two records.
#6
Re: how to maintain wait list in database table.
Posted 24 August 2008 - 05:35 PM
BEGIN TRANSACTION UPDATE YourTable SET Rank = Rank + 1 WHERE RANK > 3 INSERT INTO YourTable (Rank) VALUES (3) COMMIT TRANSACTION
And that's all there is to it. This isn't great for performance, but if it's what you need to do, it's what you need to do.
#7
Re: how to maintain wait list in database table.
Posted 25 August 2008 - 06:50 AM
Only in a SP it would come natural to pass the rank value as a parameter, i would see it as a more elegant sollution.
If worried about the performance hit (depends mostly on the size of the list) you can do a mix-form of baavgai's float idea with a renumbering if the value before and after are too close. But then arrises the question: how are you going to determine the rank of something you insert?
Is it only depending on the ranks of other values, or do you have another way to calculate a new rank?
This post has been edited by Trogdor: 25 August 2008 - 06:56 AM
#8
Re: how to maintain wait list in database table.
Posted 28 August 2008 - 06:41 PM
#9
Re: how to maintain wait list in database table.
Posted 29 August 2008 - 03:11 AM
#10
Re: how to maintain wait list in database table.
Posted 31 August 2008 - 07:29 AM
The disadvantages are well-documented; perhaps the most important is the execution plan going stale over time.
Perhaps the viStored procedures go stale and need to be occasionally recompiled. This is a problem for databases that are growing or changing. Recompiles of the procedure cause metadata locks on the objects, so simple solutions don't really get around this issue.
There are also issues surrounding option contexts; the stored procedure takes SQL Server's configuration as something of a snapshot at the time the procedure is created. If that configuration changes--either globally or in the context of the calling batch--then the procedure might behave unexepctedly.
It's hard for me to see anything substantially more elegant about stored procedures in general, and impossible for me to do so in this case. The batch in question has no local or temporary variables and doesn't invovle any conditional or procedural code.
As is frustratingly typical for these forums, we haven't heard back from kemparaju after his original post. His problem is vaguely described, but as far as I can tell, using a database for the task he describes is the wrong idea.
This post has been edited by mikeblas: 31 August 2008 - 07:32 AM
#11
Re: how to maintain wait list in database table.
Posted 31 August 2008 - 09:09 AM
mikeblas, on 31 Aug, 2008 - 10:29 AM, said:
All the issues you offer for stored procedures are issues with just passing SQL. The execution plan of a stored procedure is cached until it must be recompiled while statements are cached for an indeterminate amount of time. Portraying this behavior as a negative of stored procedures seems disingenuous.
The stored procedure allows logic to be maintained in the database. Allows the database to enforce that logic at the source. They almost always result in less traffic, as a parameterized call will be smaller than the equivalent block of code being passed.
The advantages of stored procedures are well-documented...
#12
Re: how to maintain wait list in database table.
Posted 05 September 2008 - 07:49 PM
baavgai, on 31 Aug, 2008 - 09:09 AM, said:
baavgai, on 31 Aug, 2008 - 09:09 AM, said:
In this case, the command to execute the stored proc would be about 20 characters (depending on the name chosen for the proc), and about 80 characters. Both of these are exclusive of the parameters themselves. I guess this is a savings, but is it relevant by any stretch of the imagination? And how is it any more elegant?
Is the database really the right spot to implement application logic?
baavgai, on 31 Aug, 2008 - 09:09 AM, said:
This post has been edited by mikeblas: 05 September 2008 - 07:52 PM
#13
Re: how to maintain wait list in database table.
Posted 06 September 2008 - 09:23 AM
mikeblas, on 6 Sep, 2008 - 04:49 AM, said:
Is the database really the right spot to implement application logic?
I would say yes.
You can shield the whole renumbering process off from the application, just offer it an insert / delete mechanism.
I would say that both makes a lot of sense, is probably less error-prone, and is way more elegant then doing it in the application code or the application-generated sql.
#14
Re: how to maintain wait list in database table.
Posted 07 September 2008 - 09:07 PM
Thanks for your inputs, unfortunately we are not suppose to use the stored procedure in our frame work.
Anyway I have tried double linked list implementation. It works, but still I am trying for best possible solution.
#15
Re: how to maintain wait list in database table.
Posted 08 September 2008 - 10:35 AM
|
|

New Topic/Question
Reply




MultiQuote





|