Showing posts with label transaction. Show all posts
Showing posts with label transaction. Show all posts

Friday, March 23, 2012

Exclusive Insert Lock on a Table

Hello All!

I want to perform 4 or 5 statements as a transaction but I need to make sure that during this complete transaction no one else inserts or deletes records from a table named SomeTable.

So how can I lock MyTable at the beggining of the transaction so that during my transaction no one else can insert or delete anything in table SomeTable?

Thanks!

David

You can open an transaction explicitly so other transactions are placed in a queue. Use BEGIN TRAN /COMMIT TRAN. Read up books on line for more info on transactions.

|||

Yup, I could read about this as you suggest, but it's certainly pretty complex so I was hoping someone experienced and knowleadgable would give me the answer.

Your post but it doesn't answer my question. I know how to create an explicit transaction.

My question is how do I lock a table during a transaction so that no one else inserts or deletes records from it.

I don't think that by just creating an explicit transaction and reading a row from MyTable this will lock the complete table.

|||

neutrino:

I don't think that by just creating an explicit transaction and reading a row from MyTable this will lock the complete table.

Yes it does. And thats all you have to do. Try this scenario:

(1) In a query analyzer window run this script

BEGIN TRAN

INSERT INTO TheTable ... <complete the rest of the INSERT>

(2) Open another query analyzer and do a SELECT * From TheTable. You will see that your query will be in "suspended" status waiting for the Insert to finish since you opened a Transaction explicitly.

(3) Now go back to the first window and run this:

Commit

(4) Check the second window and you will see results for your SELECT *...

Basically your BEGIN TRAN has opened a transaction --> locked the table for any other commited transactions (Even reads). As soon as you commit the transaction the lock on the table is released and others can read from the table. You can by pass this and do a dirty read by using NOLOCK hint. Not always suggested unless your business requirements allow you to.

|||

Thanks. This tells me how to lock a table by creating a transaction and doing and insert statement. However, in my scenario I need to lock the table innitially before doing any inserts to it and I want it to remain locked until the transaction ends (even if I don't do any inserts). I don't want any other transaction to be able to insert any rows until my transaction finishes.

I think that what I need is to set the transaction isolation level to SERIALIZABLE.

I found this: http://msdn2.microsoft.com/en-us/library/ms173763.aspx

I'll be trying it later and will post results.

David

|||

I do find your query analyzer excercise extremelly useful. I will use it to test my locking 'theories". Thanks!

|||

ndinakar:

neutrino:

I don't think that by just creating an explicit transaction and reading a row from MyTable this will lock the complete table.

Yes it does. And thats all you have to do. Try this scenario:

(1) In a query analyzer window run this script

BEGIN TRAN

INSERT INTO TheTable ... <complete the rest of the INSERT>

(2) Open another query analyzer and do a SELECT * From TheTable. You will see that your query will be in "suspended" status waiting for the Insert to finish since you opened a Transaction explicitly.

(3) Now go back to the first window and run this:

Commit

(4) Check the second window and you will see results for your SELECT *...

Basically your BEGIN TRAN has opened a transaction --> locked the table for any other commited transactions (Even reads). As soon as you commit the transaction the lock on the table is released and others can read from the table. You can by pass this and do a dirty read by using NOLOCK hint. Not always suggested unless your business requirements allow you to.

Actually, that doesn't lock the whole table. It locks a portion of the table, but your SELECT requires access to the entire table (including the locked portion), so it has to wait. If you have the table indexed, and the SELECT can use the index to determine that it doesn't need the locked portion then it won't delay the SELECT. In addition, a second INSERT should complete without being delayed.

To the original poster, what you are asking for is rather uncommon, and you are best to try and avoid doing what you are asking to do. Perhaps you need to rethink why you want the table locked, and what you are trying to accomplish by doing so. Usually there is a much better way of achieving that.

|||

Motley:

To the original poster, what you are asking for is rather uncommon, and you are best to try and avoid doing what you are asking to do. Perhaps you need to rethink why you want the table locked, and what you are trying to accomplish by doing so. Usually there is a much better way of achieving that.

You are totally right. This was actually what I did. I re-thought the process and found a better way that doesn't require the table lock.

But anyways it was a great learning experience.

Thanks all for your support.

David

|||

ndinakar:

neutrino:

I don't think that by just creating an explicit transaction and reading a row from MyTable this will lock the complete table.

Yes it does. And thats all you have to do. Try this scenario:

It actually doesn't but the rest of your post was really helpful. Thank you.

Wednesday, March 21, 2012

Exclude table from transaction log

Hi
Does anyone know if it is possible that changes to a specific table are not
recorded in the transaction log while changes to other tables are ?
If yes how can this be done?
Thanks
Daniel
Message posted via http://www.droptable.comThat's not possible. Why do you want to do this?
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
"DANR via droptable.com" <u26179@.uwe> wrote in message
news:662d81586fb67@.uwe...
Hi
Does anyone know if it is possible that changes to a specific table are not
recorded in the transaction log while changes to other tables are ?
If yes how can this be done?
Thanks
Daniel
Message posted via http://www.droptable.com|||Hi,
No. We can not do that. SQL Server engine by itself will log all the
transactions in to transaction log file.
Thanks
Hari
SQL Server MVP
"DANR via droptable.com" <u26179@.uwe> wrote in message
news:662d81586fb67@.uwe...
> Hi
> Does anyone know if it is possible that changes to a specific table are
> not
> recorded in the transaction log while changes to other tables are ?
> If yes how can this be done?
> Thanks
> Daniel
> --
> Message posted via http://www.droptable.com
>|||Hi Tom
Because I have a table were I register several informations with thousands o
f
records but the information is not really critical and if something goes
wrong I don't need to recover that information.
This information is increasing the size of the transaction log without being
really necessary.
Thanks
Daniel
Tom Moreau wrote:
>That's not possible. Why do you want to do this?
>Hi
>Does anyone know if it is possible that changes to a specific table are not
>recorded in the transaction log while changes to other tables are ?
>If yes how can this be done?
>Thanks
>Daniel
Message posted via http://www.droptable.com|||Ok Hari
Thanks
Daniel
Hari Prasad wrote:[vbcol=seagreen]
>Hi,
>No. We can not do that. SQL Server engine by itself will log all the
>transactions in to transaction log file.
>Thanks
>Hari
>SQL Server MVP
>
>[quoted text clipped - 5 lines]
Message posted via droptable.com
http://www.droptable.com/Uwe/Forum...server/200609/1|||If you don't need that this table is time-correlated with the rest of the ta
ble in your backups,
consider putting it in its own database. Also, this database can be in simpl
e recovery mode and you
can investigate if it is possible to input the data using a minimally logged
operation (bulk loading
is probably the best bet).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"DANR via droptable.com" <u26179@.uwe> wrote in message news:662ee8ef93f81@.uwe...reen">
> Hi Tom
> Because I have a table were I register several informations with thousands
of
> records but the information is not really critical and if something goes
> wrong I don't need to recover that information.
> This information is increasing the size of the transaction log without bei
ng
> really necessary.
> Thanks
> Daniel
>
> Tom Moreau wrote:
> --
> Message posted via http://www.droptable.com
>|||Hi Tibor
Thanks for your sugestion. I already thought using that approach.
I just asked because maybe someone know other way.
Thanks to all
Regards
Daniel
Tibor Karaszi wrote:[vbcol=seagreen]
>If you don't need that this table is time-correlated with the rest of the t
able in your backups,
>consider putting it in its own database. Also, this database can be in simp
le recovery mode and you
>can investigate if it is possible to input the data using a minimally logge
d operation (bulk loading
>is probably the best bet).
>
>[quoted text clipped - 16 lines]
Message posted via http://www.droptable.com

Exclude table from transaction log

Hi
Does anyone know if it is possible that changes to a specific table are not
recorded in the transaction log while changes to other tables are ?
If yes how can this be done?
Thanks
Daniel
--
Message posted via http://www.sqlmonster.comThat's not possible. Why do you want to do this?
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
"DANR via SQLMonster.com" <u26179@.uwe> wrote in message
news:662d81586fb67@.uwe...
Hi
Does anyone know if it is possible that changes to a specific table are not
recorded in the transaction log while changes to other tables are ?
If yes how can this be done?
Thanks
Daniel
--
Message posted via http://www.sqlmonster.com|||Hi,
No. We can not do that. SQL Server engine by itself will log all the
transactions in to transaction log file.
Thanks
Hari
SQL Server MVP
"DANR via SQLMonster.com" <u26179@.uwe> wrote in message
news:662d81586fb67@.uwe...
> Hi
> Does anyone know if it is possible that changes to a specific table are
> not
> recorded in the transaction log while changes to other tables are ?
> If yes how can this be done?
> Thanks
> Daniel
> --
> Message posted via http://www.sqlmonster.com
>|||Hi Tom
Because I have a table were I register several informations with thousands of
records but the information is not really critical and if something goes
wrong I don't need to recover that information.
This information is increasing the size of the transaction log without being
really necessary.
Thanks
Daniel
Tom Moreau wrote:
>That's not possible. Why do you want to do this?
>Hi
>Does anyone know if it is possible that changes to a specific table are not
>recorded in the transaction log while changes to other tables are ?
>If yes how can this be done?
>Thanks
>Daniel
--
Message posted via http://www.sqlmonster.com|||Ok Hari
Thanks
Daniel
Hari Prasad wrote:
>Hi,
>No. We can not do that. SQL Server engine by itself will log all the
>transactions in to transaction log file.
>Thanks
>Hari
>SQL Server MVP
>> Hi
>[quoted text clipped - 5 lines]
>> Thanks
>> Daniel
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200609/1|||If you don't need that this table is time-correlated with the rest of the table in your backups,
consider putting it in its own database. Also, this database can be in simple recovery mode and you
can investigate if it is possible to input the data using a minimally logged operation (bulk loading
is probably the best bet).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"DANR via SQLMonster.com" <u26179@.uwe> wrote in message news:662ee8ef93f81@.uwe...
> Hi Tom
> Because I have a table were I register several informations with thousands of
> records but the information is not really critical and if something goes
> wrong I don't need to recover that information.
> This information is increasing the size of the transaction log without being
> really necessary.
> Thanks
> Daniel
>
> Tom Moreau wrote:
>>That's not possible. Why do you want to do this?
>>Hi
>>Does anyone know if it is possible that changes to a specific table are not
>>recorded in the transaction log while changes to other tables are ?
>>If yes how can this be done?
>>Thanks
>>Daniel
> --
> Message posted via http://www.sqlmonster.com
>|||Hi Tibor
Thanks for your sugestion. I already thought using that approach.
I just asked because maybe someone know other way.
Thanks to all
Regards
Daniel
Tibor Karaszi wrote:
>If you don't need that this table is time-correlated with the rest of the table in your backups,
>consider putting it in its own database. Also, this database can be in simple recovery mode and you
>can investigate if it is possible to input the data using a minimally logged operation (bulk loading
>is probably the best bet).
>> Hi Tom
>[quoted text clipped - 16 lines]
>>Thanks
>>Daniel
--
Message posted via http://www.sqlmonster.comsql

Friday, March 9, 2012

Excessive Free Space in Large Table

If all that space gets consume after reindexing then its
the transaction log. Read up on truncate/ shrink TLog.
quote:

>--Original Message--
>Hello all,
>I'm having the following space allocation issue with a

large table in a SQL
quote:

>Server 2000 DB.
>I have a table which stores visits to a site, currently

with 95 million
quote:

>rows. This table currently has a total of 60GB space:

24GB used space, 4GB
quote:

>index space, and 32 GB unused space. The server has been

running out of
quote:

>space recently, and I would like to reclaim alot of the

unused space from
quote:

>this table. The table definition is as follows:
>visitor_id int 4
>visitor_settings varchar 7000
>short_settings tinyint 1
>visit_count int 4
>created_reason tinyint 1
>modified_by int 4
>modified_date datetime 8
>created_by int 4
>created_date datetime 8
>GUID_for_clustering uniqueidentifier 16
>There are 2 indexes, a PK index on visitor_id, and a

clustered index on
quote:

>GUID_for_clustering, both with a 50% fill factor.

Shrinking the table helps
quote:

>temporarily, but the unused space climbs back up to 30GB

after reindexing
quote:

>the table. Any thoughts on what is causing the

allocation of all the extra
quote:

>free space, and how I can reduce it? My initial thoughts

relate to
quote:

>adjusting the fill factor, or possibly modifying the

visitor_settings
quote:

>varchar 7000 field (perhaps 'text' would actually reduce

the space
quote:

>allocation?). Any thoughts would be much appreciated.
>Thanks!
>-Mark
>
>.
>
Because of space concerns, this database has no transaction log (it is
non-critical data, and is backed-up daily).
I can only think the extra space is being allocated for the visitor_settings
varchar 7000 on each row after reindexing, even though the average length is
probably only around 3000. Because it's a varchar type, the space would not
actually be used, but perhaps is still allocated and thus, unavailable?
-Mark
"chris" <anonymous@.discussions.microsoft.com> wrote in message
news:725101c3e793$8abb8620$a101280a@.phx.gbl...[QUOTE]
> If all that space gets consume after reindexing then its
> the transaction log. Read up on truncate/ shrink TLog.
>
> large table in a SQL
> with 95 million
> 24GB used space, 4GB
> running out of
> unused space from
> clustered index on
> Shrinking the table helps
> after reindexing
> allocation of all the extra
> relate to
> visitor_settings
> the space