READ UNCOMMITTED isolation level behavior on more than one transaction - sql-server

I have a stored procedure to update a table, and i set the isolation level to READ UNCOMMITTED. In this sp i set the comment count (CommentCount+=1). If more than one user call this sp at the same time, is it possible that comment count increase less than number of user that added comment?

SQL Server still acquires locks on updated rows in the READ UNCOMMITTED isolation level. An UPDATE statement like this will not miss increments when executed by multiple READ UNCOMMITTED sessions concurrently:
UPDATE dbo.Post
SET CommentCount += 1
WHERE PostID = #PostID;
Here's a trace of locks by this statement, updating by a clustered primary key on PostID. The exclusive lock will block other concurrent updates to the row.
+---------------+----+--------------+----------+--+
| Lock:Acquired | 58 | 5 - OBJECT | 8 - IX | |
| Lock:Acquired | 58 | 6 - PAGE | 8 - IX | |
| Lock:Acquired | 58 | 7 - KEY | 5 - X | |
| Lock:Released | 58 | 7 - KEY | 0 - NULL | |
| Lock:Released | 58 | 6 - PAGE | 0 - NULL | |
| Lock:Released | 58 | 7 - KEY | 5 - X | |
| Lock:Released | 58 | 6 - PAGE | 8 - IX | |
| Lock:Released | 58 | 5 - OBJECT | 8 - IX | |
+---------------+----+--------------+----------+--+
And here's a trace where the row is located using a non-clustered primary key index. The update lock on the non-clustered key will serialize other update statements for the same key and the exclusive lock on the clustered key will block other data modifications.
+---------------+----+------------+----------+--+
| Lock:Acquired | 58 | 5 - OBJECT | 8 - IX | |
| Lock:Acquired | 58 | 6 - PAGE | 7 - IU | |
| Lock:Acquired | 58 | 7 - KEY | 4 - U | |
| Lock:Acquired | 58 | 6 - PAGE | 7 - IU | |
| Lock:Acquired | 58 | 7 - KEY | 4 - U | |
| Lock:Acquired | 58 | 6 - PAGE | 8 - IX | |
| Lock:Acquired | 58 | 7 - KEY | 5 - X | |
| Lock:Released | 58 | 7 - KEY | 0 - NULL | |
| Lock:Released | 58 | 6 - PAGE | 0 - NULL | |
| Lock:Released | 58 | 7 - KEY | 4 - U | |
| Lock:Released | 58 | 6 - PAGE | 7 - IU | |
| Lock:Released | 58 | 7 - KEY | 5 - X | |
| Lock:Released | 58 | 6 - PAGE | 8 - IX | |
| Lock:Released | 58 | 5 - OBJECT | 8 - IX | |
+---------------+----+------------+----------+--+

Related

is there any way to make vnode sync faster in TDengine database?

I created a database with two replicas, when I tried to write some data into the database, the vnode status is always syncing, how to make it sync faster?
taos> show vgroups;
vgId | tables | status | onlines | v1_dnode | v1_status | v2_dnode | v2_status | compacting |
=================================================================================================================
2 | 1000 | ready | 2 | 3 | master | 2 | slave | 0 |
3 | 1000 | ready | 2 | 2 | master | 1 | slave | 0 |
4 | 1000 | ready | 2 | 1 | master | 3 | slave | 0 |
5 | 1000 | ready | 2 | 3 | master | 2 | slave | 0 |
6 | 1000 | ready | 2 | 2 | master | 1 | slave | 0 |
7 | 1000 | ready | 2 | 1 | master | 3 | slave | 0 |
8 | 1000 | ready | 2 | 3 | master | 2 | slave | 0 |
9 | 1000 | ready | 2 | 2 | master | 1 | slave | 0 |
10 | 1000 | ready | 2 | 1 | master | 3 | syncing | 0 |
11 | 1000 | ready | 2 | 3 | master | 2 | slave | 0 |
Query OK, 10 row(s) in set (0.004323s)

Building index for specific value

I have a table that keeps inventory information for products in stores on daily basis. It is like:
|------------|-----------|---------|-----------------|
| Date | ProductId | StoreId | InventoryOnHand |
|------------|-----------|---------|-----------------|
| 2017-10-11 | 348 | 121 | 2 |
| 2017-10-11 | 110 | 200 | 0 |
| 2017-10-11 | 254 | 587 | -2 |
| 2017-10-12 | 311 | 875 | 26 |
| 2017-10-12 | 954 | 364 | 15 |
| 2017-10-12 | 348 | 121 | 0 |
| 2017-10-12 | 441 | 121 | 7 |
| . | . | . | . |
| . | . | . | . |
| . | . | . | . |
|------------|-----------|---------|-----------------|
In most queries I used have condition like WHERE InventoryOnHand > 0. I need to speed up these queries.
Therefore, I want to build and index that separates values on column InventoryOnHand whether they are greater than 0 or not.
Filtered Index does not solve my problem because if I use filtered index all values greater than 0 will be indexed and this increases index size. I only need to know if a value greater than 0 or not.
i.e. I want to build an index that only works when condition is InventoryOnHand > 0. Is there any way to do this on SQL-Server?

Powerpivot 2016 measure using DAX to sum an array

I want to sum the 7 preceding values of a row as a measure like so:
| Wk_number | Value A | Measure | Array |
-------------------------------------------
| 01 | 1 | N/A# | N/A# |
| 02 | 1 | 1 | {01} |
| 03 | 10 | 2 | {01-02} |
| 04 | 3 | 12 | {01-03} |
| 05 | 5 | 15 | {01-04} |
| 06 | 10 | 20 | {01-05} |
| 07 | 1 | 30 | {01-06} |
| 08 | 4 | 31 | {01-07} |
| 09 | -10 | 34 | {02-08} |
| 10 | 3 | 26 | {03-09} |
| 11 | 2 | 18 | {04-10} |
etc...
I added the array column just to clarify the example how of the sum is comprised, notice that from wk09 it's not simply a running total.
How to do this using DAX statements?
Two ways to do this: you can either create a calculated column, or a measure.
For the column:
=CALCULATE(SUM([Value A]),FILTER(Table,Table[Wk_number]<EARLIER(Table[Wk_number]) && Table[Wk_number] >= (EARLIER(Table[Wk_number])-7)))
For the measure, it's a very similar formula but instead of using EARLIER(), we use MAX():
=CALCULATE(SUM([Value A]),FILTER(ALL(Table3),Table3[Wk_number]<MAX(Table3[Wk_number]) && Table3[Wk_number] >= (MAX(Table3[Wk_number])-7)))
Below is a screenshot of the results. A few of the numbers in your example table seem to be off based on the math:

SQL Database Constraint | Multi-table Constraint

I need to make 2 database constraints that connect two different tables at one time.
1. The total score of the four quarters equals the total score of the game the quarters belong to.
2. The total point of all the players equals to the score of the game of that team.
Here is what my tables look like.
quarter table
+------+--------+--------+--------+
| gNum | Period | hScore | aScore |
+------+--------+--------+--------+
| 1 | 1 | 13 | 18 |
| 1 | 2 | 12 | 19 |
| 1 | 3 | 23 | 31 |
| 1 | 4 | 32 | 18 |
| | | Total | Total |
| | | 80 | 86 |
+------+--------+--------+--------+
Game Table
+-----+--------+--------+--------+
| gID | hScore | lScore | tScore |
+-----+--------+--------+--------+
| 1 | 86 | 80 | 166 |
+-----+--------+--------+--------+
Player Table
+-----+------+--------+--------+
| pID | gNum | Period | Points |
+-----+------+--------+--------+
| 1 | 1 | 1 | 20 |
| | | 2 | 20 |
| | | 3 | 20 |
| | | 4 | 20 |
+-----+------+--------+--------+
So Virtually I need to use CHECK I think to make sure that players points = score of their team ie (hScore, aScore) and also make sure that the hScore and aScore = the total score in the Game table.
I was thinking of creating a foreign key variable on one of the tables and setting up constraints on that would this be the best way of going about it?
Thanks

Transform ranged data in an Access table

I have a table in Access database as below;
Name | Range | X | Y | Z
------------------------------
A | 100-200 | 1 | 2 | 3
A | 200-300 | 4 | 5 | 6
B | 100-200 | 10 | 11 | 12
B | 200-300 | 13 | 14 | 15
C | 200-300 | 16 | 17 | 18
C | 300-400 | 19 | 20 | 21
I have trying write a query that convert this into the following format.
Name | X_100_200 | Y_100_200 | Z_100_200 | X_200_300 | Y_200_300 | Z_200_300 | X_300_400 | Y_300_400 | Z_300_400
A | 1 | 2 | 3 | 4 | 5 | 6 | | |
B | 10 | 11 | 12 | 13 | 14 | 15 | | |
C | | | | 16 | 17 | 18 | 19 | 20 | 21
After trying for a while the best method I could come-up with is to write bunch of short queries that selects the data for each Range and then put them together again using a Union query. The problem is that for this example I have shown 3 columns (X, Y and Z), but I actually have much more. Access is starting to strain with the amount of SQL I have come up with.
Is there a better way to achieve this?
The answer was simple. Just use Access Pivotview. Finding it hard to export the results to Excel though.

Resources