Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 locking issue

Author  Topic 

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-07-09 : 13:29:46
is there a way to setup a table so that selecting data off it, doesnt cause locks when another user is inserting into it?

i basically have a table that stores session variables for a website, every time the user toggles an option, it stores that value into a table, but the values have to be selected @ the same time by other users... any times the selects take more than a few seconds the site starts tripping...

any thoughts would be helpful, thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-09 : 13:34:00
You should look into READ_COMMITTED_SNAPSHOT isolation level. The default isolation level, READ_COMMITTED, uses shared locks on select queries. That caused writes to be blocked (INSERT/UPDATE/DELETE). But with READ_COMMITTED_SNAPSHOT, a shared lock is not taken and instead a snapshot of the data is used. This means that reads (SELECT) can not block writes.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-07-09 : 14:26:02
worked PERFECTLY! really appreciate it... now question, this is a table of under 1 million rows, what kind of overhead can i expect from changing this...?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-09 : 14:27:26
That's a small table. Wait until you get into the billions!

The only overhead is the tempdb utilization, so watch disk space in tempdb. Make sure your tempdb is optimized. You should have one tempdb data file per CPU (per core, not per socket). You should also have tempdb on its own set of disks.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-09 : 15:22:02
So...why didn't M$ just make THAT problem go away with another HACK?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-09 : 15:41:37
Well you have to understand the difference between READ_COMMITTED and READ_COMMITTED_SNAPSHOT. If you are okay with getting a snapshot of the data that can change while you are reading it, then READ_COMMITTED_SNAPSHOT would be good. You don't get a dirty read with it, but you need to understand the difference. A banking system would need to use READ_COMMITTED, plus ecommerce sites. READ_COMMITTED_SNAPSHOT is perfect for the systems that I work on.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-09 : 15:42:28
who would EVER want a dirty read????

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-09 : 15:43:02
It's like a chick (or guy) in a bar who lies to your face...

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-09 : 15:44:44
It's not a dirty read with READ_COMMITTED_SNAPSHOT. You don't get a data value that might get rolled back. The data that you get is correct at the time you read it.

A dirty read works well in situations where the data is read-only, you just want a quick COUNT, etc... I don't ever use it in production code.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-09 : 15:45:51
Read this discussion for more information: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=145130

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-07-09 : 16:05:14
quote:
Originally posted by tkizer
I don't ever use it in production code.

Tara Kizer



If I'm gonna use it in PROD, there is NO Sense in using it ANY other way...noobs get used to shortcuts too easily

'Nough said

In a nutshell that is my prob w/ M$, where IBM is painful but Direct..there is only 1 right way..being German...I tend to believe that...it's in my genes

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-07-20 : 18:48:18
Tara, it seems like two different insert commands cannot run at the same time, one will block the other... is this normal behavior? and if so, is there anything (aside from waiting) around it?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-20 : 18:53:32
You are going to need to provide more information, like DDL for the tables and sample insert statements. Plus what you are seeing.

We have hundreds of inserts happening per second and do not have this contention.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-07-21 : 02:39:25
the data is partitioned accross 4 drives, 8 files...

CREATE PARTITION FUNCTION [pf_Supp](varchar(5)) AS RANGE RIGHT FOR VALUES (N'0', N'1', N'24067', N'38614', N'60131', N'78259', N'95133')
CREATE PARTITION SCHEME [ps_Supp] AS PARTITION [pf_Supp] TO ([PRIMARY], [FileGroup2], [FileGroup3], [FileGroup4], [FileGroup5], [FileGroup6], [FileGroup7], [FileGroup8])
GO

CREATE TABLE [dbo].[Suppression](
[LN] [varchar](30) default '',
[Addr] [varchar](50) default '',
[zip] [varchar](5) default '',
[phone] [varchar](10) default '',
[CustomerID] [uniqueidentifier] NOT NULL,
[Date_Added] [date] default getdate(),
[IsPerm] [char](1) default '' on ps_supp (zip)
)
GO

CREATE NONCLUSTERED INDEX [ix_dboSupp_001] ON [dbo].[Suppression]
(
[LN] ASC,
[phone] ASC,
[CustomerID] ASC,
[Date_Added] ASC
)
GO

CREATE NONCLUSTERED INDEX [ix_dboSupp_002]
ON [dbo].[Suppression] ([CustomerID])
GO



when i'm doing inserts it's kinda like this one:

insert into Suppression
select
LEFT(LN,30),
LEFT(addr,50),
LEFT(Zip,5),
LEFT(phone,10),
CustomerID,
Date_Added,
IsPerm
from supp.dbo.SuppImport


this table was 2,016,651 records, and it took 32 minutes to insert the data...

also, lets say this insert is going on, and i had another 10k insert i needed to run off another table into
supp.dbo.suppression, that process would be blocked by the insert running above until it has completed.

anything else you need? Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-21 : 13:31:01
You should consider using an SSIS package to move the data so that a bulk load can be used. Moving 2 million rows should take perhaps a minute.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-07-22 : 00:01:41
only thing is, it can be slightly different on every query... can u bulk-load using t-sql? or no, just packages?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-22 : 00:44:01
Yes you can use T-SQL in a package.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-07-22 : 00:47:16
what i ment was can i use normal t-sql to do the bulk-load... rather then an SSIS package.

is there a way to pass parameters to an ssis package from either command-line, or though SSIS it self? if i was able to call the package, and give it a hand-ful of parameters, that might work.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-22 : 00:49:34
T-SQL doesn't support except through files and BULK INSERT.

Yes SSIS packages allow parameters. You should do some research over at sqlis.com which has some great info on SSIS.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2010-07-22 : 01:01:30
thanks for pointing me in the right direction. really appreciate it.
Go to Top of Page
   

- Advertisement -