Showing posts with label gurus. Show all posts
Showing posts with label gurus. Show all posts

Tuesday, February 14, 2012

Concurrency (recap and idea).....

With respect to my (now not so recent) thread on Concurrency, I would like
to run my idea past you gurus to see if its a runner. First, a brief recap:

I have a single user system (one user, one copy of the software, one copy of
MSDE, one machine) that I wish to convert into a multi-user/single database
networked system. The problem I had was that a lot of information is
fetched from the database and cached in the client program (the program
implements a tree structure, similar to a file system, and each of the nodes
in the system has properties). The concurrency issue revolved around having
multiple users updating these properties and possibly able to modify the
tree structure and there being no way to notify the other clients that they
need to refresh their data structures. Consider the system to be similar to
VSS to look at (and in VSS, people can make modifications to the tree
structure also!).

Ok, one of the suggestions was time stamping each record. So, when one user
modifies the record, a second user can detect whether their timestamp is
different and thus whether or not their update is invalid (and also whether
or not the client program needs to refresh the properties of the given
node). How about instead of a timestamp I simply use a reference counter.
ie. an integer that increments every time the record is modified (same
principle). I don't need to know when it was changed, just that the two
reference counters are different between when I fetched and when I am
updating the record.

Secondly, I think I have to distinguish between a change in properties and a
change in structure. For example, User A doesn't need to know about a
change in properties for a node he is not currently looking at. However,
that same user will want to be told about any change to the overall tree
structure. So, I was thinking that any operations involving modifications
to the tree structure should set a "structure changed" flag in the database
(increment a counter). After any operation is performed, the client
compares its "changed" flag to the database value to see if it needs to
reload the tree structure.

Do you think this is workable?

Thanks.

RobinRegarding your first question, this is known as optimistic concurrency and
is usually implemented with a timestamp data type, not to be confused with a
datetime column. The timestamp datatype contains a binary value unrelated
to date/time and is automatically updated by SQL Server whenever the row is
updated. Consequently, you can use a technique like:

UPDATE MyTable
SET MyData = @.NewMyDataValue
WHERE MyID = @.MyID AND
MyTimestamp = @.OldMyTimestamp
IF @.@.ROWCOUNT = 0
BEGIN
RAISERROR ('Data has been deleted or updated by another user', 16, 1)
END

You can use a similar technique to detect structure changes to your tree.
Save the current timestamp value in a program variable and check
periodically as desired.

SELECT LastUpdatedByUser, LastUpdateTime, TreeTimeStamp
FROM MyTrees
WHERE MyTreeID = 1

The above table can be updated as follows. You can also include this in
triggers or procs for convenience, depending on how you perform data
manipulation in your app.

UPDATE MyTrees
SET LastUpdatedByUser = SUSER_SNAME(),
LastUpdateTime = CURRENT_TIMESTAMP
WHERE MyTreeID = 1

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
message news:cp6lcq$ns4$1$8302bc10@.news.demon.co.uk...
> With respect to my (now not so recent) thread on Concurrency, I would like
> to run my idea past you gurus to see if its a runner. First, a brief
> recap:
> I have a single user system (one user, one copy of the software, one copy
> of MSDE, one machine) that I wish to convert into a multi-user/single
> database networked system. The problem I had was that a lot of
> information is fetched from the database and cached in the client program
> (the program implements a tree structure, similar to a file system, and
> each of the nodes in the system has properties). The concurrency issue
> revolved around having multiple users updating these properties and
> possibly able to modify the tree structure and there being no way to
> notify the other clients that they need to refresh their data structures.
> Consider the system to be similar to VSS to look at (and in VSS, people
> can make modifications to the tree structure also!).
> Ok, one of the suggestions was time stamping each record. So, when one
> user modifies the record, a second user can detect whether their timestamp
> is different and thus whether or not their update is invalid (and also
> whether or not the client program needs to refresh the properties of the
> given node). How about instead of a timestamp I simply use a reference
> counter. ie. an integer that increments every time the record is modified
> (same principle). I don't need to know when it was changed, just that the
> two reference counters are different between when I fetched and when I am
> updating the record.
> Secondly, I think I have to distinguish between a change in properties and
> a change in structure. For example, User A doesn't need to know about a
> change in properties for a node he is not currently looking at. However,
> that same user will want to be told about any change to the overall tree
> structure. So, I was thinking that any operations involving modifications
> to the tree structure should set a "structure changed" flag in the
> database (increment a counter). After any operation is performed, the
> client compares its "changed" flag to the database value to see if it
> needs to reload the tree structure.
>
> Do you think this is workable?
>
> Thanks.
>
> Robin|||Thanks, this is useful stuff.

"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:rHCtd.38154$bP2.3402@.newssvr12.news.prodigy.c om...
> Regarding your first question, this is known as optimistic concurrency and
> is usually implemented with a timestamp data type, not to be confused with
> a datetime column. The timestamp datatype contains a binary value
> unrelated to date/time and is automatically updated by SQL Server whenever
> the row is updated. Consequently, you can use a technique like:
> UPDATE MyTable
> SET MyData = @.NewMyDataValue
> WHERE MyID = @.MyID AND
> MyTimestamp = @.OldMyTimestamp
> IF @.@.ROWCOUNT = 0
> BEGIN
> RAISERROR ('Data has been deleted or updated by another user', 16, 1)
> END
> You can use a similar technique to detect structure changes to your tree.
> Save the current timestamp value in a program variable and check
> periodically as desired.
> SELECT LastUpdatedByUser, LastUpdateTime, TreeTimeStamp
> FROM MyTrees
> WHERE MyTreeID = 1
> The above table can be updated as follows. You can also include this in
> triggers or procs for convenience, depending on how you perform data
> manipulation in your app.
> UPDATE MyTrees
> SET LastUpdatedByUser = SUSER_SNAME(),
> LastUpdateTime = CURRENT_TIMESTAMP
> WHERE MyTreeID = 1
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Robin Tucker" <idontwanttobespammedanymore@.reallyidont.com> wrote in
> message news:cp6lcq$ns4$1$8302bc10@.news.demon.co.uk...
>> With respect to my (now not so recent) thread on Concurrency, I would
>> like to run my idea past you gurus to see if its a runner. First, a
>> brief recap:
>>
>> I have a single user system (one user, one copy of the software, one copy
>> of MSDE, one machine) that I wish to convert into a multi-user/single
>> database networked system. The problem I had was that a lot of
>> information is fetched from the database and cached in the client program
>> (the program implements a tree structure, similar to a file system, and
>> each of the nodes in the system has properties). The concurrency issue
>> revolved around having multiple users updating these properties and
>> possibly able to modify the tree structure and there being no way to
>> notify the other clients that they need to refresh their data structures.
>> Consider the system to be similar to VSS to look at (and in VSS, people
>> can make modifications to the tree structure also!).
>>
>> Ok, one of the suggestions was time stamping each record. So, when one
>> user modifies the record, a second user can detect whether their
>> timestamp is different and thus whether or not their update is invalid
>> (and also whether or not the client program needs to refresh the
>> properties of the given node). How about instead of a timestamp I simply
>> use a reference counter. ie. an integer that increments every time the
>> record is modified (same principle). I don't need to know when it was
>> changed, just that the two reference counters are different between when
>> I fetched and when I am updating the record.
>>
>> Secondly, I think I have to distinguish between a change in properties
>> and a change in structure. For example, User A doesn't need to know
>> about a change in properties for a node he is not currently looking at.
>> However, that same user will want to be told about any change to the
>> overall tree structure. So, I was thinking that any operations involving
>> modifications to the tree structure should set a "structure changed" flag
>> in the database (increment a counter). After any operation is performed,
>> the client compares its "changed" flag to the database value to see if it
>> needs to reload the tree structure.
>>
>>
>> Do you think this is workable?
>>
>>
>> Thanks.
>>
>>
>>
>> Robin
>>

Concurency: Oracle vs. MSSQLServer 2K

Hello Gurus,
Im currently reading a Software Engineering for Web Applications Course
which is published online at the MIT (Open CourseWare Site) and here it says
at least it suggests that concurrency behaviour is best in Oracle than in
MSSQLServer. Im SQL Programmer and essentially I love its ease of use and
Id like you guys to comment this transcript. Is it possible to get the same
results in SQLServer by fine tuning the DBMS?
"
Concurrency is Oracle's strongest suit relative to its commercial
competitors. In Oracle, readers never wait for writers and writers never
wait for readers. Suppose the publisher at a large site starts a query at
12:00 PM summarizing usage by user. Oracle might have to spend an hour
sifting through 200 GB of tracking data. The disk drives grind and one CPU
is completely used up until 1:30 PM. Further suppose that User #356712 comes
in at 12:30 PM and changes his email address, thus updating a row in the
users table. If the usage tracking query arrives at this row at 12:45 PM,
Oracle will notice that the row was last modified after the query started.
Under the "I" in ACID, Oracle is required to isolate the publisher from the
user's update. Oracle does this by reaching into the rollback segment and
producing data from user row #356712 as it was at 12:00 PM when the query
started. Here's the scenario in a table:
Time Publisher Public Web Application
12:00 PM Starts a 90-minute query summarizing usage for preceding
year --
12:30 PM usage summary continues to chug away User #356712 updates
email address from "joe@.foobar.com" to "joe@.yahoo.com"
12:45 PM usage summary arrives at User #356712; Oracle reaches into
rollback segment and pulls out "joe@.foobar.com" for the report, since that's
what the value was at 12:30 PM --
1:30 PM usage summary report completes --
How would this play out in Microsoft SQL Server? When you're reading, you
take read locks on the information that you're about to read. Nobody can
write until you release them. When you're writing, you take write locks on
the information that you're about to update. Nobody can read or write until
you release the locks. In the preceding example, User #356712 would submit
his request for the address change at 12:30 PM. The thread on the Web server
would be blocked waiting for the read locks to clear. How long would it
wait? A full hour with a spinning/waving "browser still receiving
information" icon in the upper right corner of the browser window. If you're
thoughtful, you can program around this locking architecture in SQL Server,
but most Internet service operators would rather just install Oracle than
train their programmers to think more carefully about concurrency.
"
--
Jorge Luzarraga C
"I can do it quick. I can do it cheap. I can do it well. Pick any two."SQL Server 2005 contains a feature called "snapshot isolation" that can help
you with this issue.
You really should evaluate the concurrency and data freshness needs of your
application to determine which model you want to use. I personally have
seen cases where the before image was not what the application really
needed. I've also seen cases where it was fine. So, please make sure you
pick what works for your application requirements.
Thanks,
Conor
"Jorge Luzarraga Castro" <jluzarraga@.fidens.cl> wrote in message
news:ubX1kzVFGHA.532@.TK2MSFTNGP15.phx.gbl...
> Hello Gurus,
> Im currently reading a Software Engineering for Web Applications Course
> which is published online at the MIT (Open CourseWare Site) and here it
> says at least it suggests that concurrency behaviour is best in Oracle
> than in MSSQLServer. Im SQL Programmer and essentially I love its ease of
> use and Id like you guys to comment this transcript. Is it possible to
> get the same results in SQLServer by fine tuning the DBMS?
> "
> Concurrency is Oracle's strongest suit relative to its commercial
> competitors. In Oracle, readers never wait for writers and writers never
> wait for readers. Suppose the publisher at a large site starts a query at
> 12:00 PM summarizing usage by user. Oracle might have to spend an hour
> sifting through 200 GB of tracking data. The disk drives grind and one CPU
> is completely used up until 1:30 PM. Further suppose that User #356712
> comes in at 12:30 PM and changes his email address, thus updating a row in
> the users table. If the usage tracking query arrives at this row at 12:45
> PM, Oracle will notice that the row was last modified after the query
> started. Under the "I" in ACID, Oracle is required to isolate the
> publisher from the user's update. Oracle does this by reaching into the
> rollback segment and producing data from user row #356712 as it was at
> 12:00 PM when the query started. Here's the scenario in a table:
> Time Publisher Public Web Application
> 12:00 PM Starts a 90-minute query summarizing usage for preceding
> year --
> 12:30 PM usage summary continues to chug away User #356712
> updates email address from "joe@.foobar.com" to "joe@.yahoo.com"
> 12:45 PM usage summary arrives at User #356712; Oracle reaches
> into rollback segment and pulls out "joe@.foobar.com" for the report, since
> that's what the value was at 12:30 PM --
> 1:30 PM usage summary report completes --
> How would this play out in Microsoft SQL Server? When you're reading, you
> take read locks on the information that you're about to read. Nobody can
> write until you release them. When you're writing, you take write locks on
> the information that you're about to update. Nobody can read or write
> until you release the locks. In the preceding example, User #356712 would
> submit his request for the address change at 12:30 PM. The thread on the
> Web server would be blocked waiting for the read locks to clear. How long
> would it wait? A full hour with a spinning/waving "browser still receiving
> information" icon in the upper right corner of the browser window. If
> you're thoughtful, you can program around this locking architecture in SQL
> Server, but most Internet service operators would rather just install
> Oracle than train their programmers to think more carefully about
> concurrency.
> "
> --
> Jorge Luzarraga C
> "I can do it quick. I can do it cheap. I can do it well. Pick any two."
>|||> Im currently reading a Software Engineering for Web Applications Course
> which is published online at the MIT (Open CourseWare Site) and here it
> says at least it suggests that concurrency behaviour is best in Oracle
> than in MSSQLServer. Im SQL Programmer and essentially I love its ease of
> use and Id like you guys to comment this transcript. Is it possible to
> get the same results in SQLServer by fine tuning the DBMS?
Well, Oracle and SQL Server both appear on this list three times.
http://www.tpc.org/tpcc/results/tpc...=5¤cyID=0
Yes, Oracle appears above Microsoft, but two notes:
(a) Oracle's most recent data is from April 2005
(b) Oracle' average cost is almost 19% higher than SQL Server (avg $7.31 vs.
$6.15), so yes, you can get a bit better performance (even if you believe
the data is valid after that much time) but it's going to cost you a lot
more up front to build the system.
DB2 is at the top of the list, so why does this article choose to only
compare Oracle and SQL Server? Wait, let me guess, it was written by
someone on Oracle's board, or Oracle has made huge investments in MIT's
labs, or...

> How would this play out in Microsoft SQL Server? When you're reading, you
> take read locks on the information that you're about to read. Nobody can
> write until you release them.
That's only with certain isolation levels. You can certainly do a lot of
reading without blocking anyone, either with dirty reads (a.k.a. NOLOCK) or
with the new snapshot isolation in 2005.

> but most Internet service operators would rather just install Oracle than
> train their programmers to think more carefully about concurrency.
Is there any evidence to back this up? How many service providers do you
know of, that provide Oracle as the back end and say, go ahead, build your
database and application around it? I've never heard of one.
There are plenty of ways to design a well-performing application, and while
database vendor choice is certainly a significant factor, it's not the only
one, and it's not the one you end up spending the most time on.
A