Showing posts with label basic. Show all posts
Showing posts with label basic. Show all posts

Friday, March 30, 2012

long transaction

Hi,

I am still not very proficient in SQLServer. So apology if the
question sounds basic.

We have a script to clean old unwanted data. It basically deletes
all rows which are more than 2 weeks old. It deletes data from
33 tables and the number of rows in each table runs into few millions.
What I see in the script (not written by me :-) ) is that all data is
deleted within a single BEGIN TRANSACTION and COMMIT TRANSACTION. As
I have background in informix, such an action in Informix may result
in "LONG TRANSACTION PROBLEM". Does SQLServer have a similar concept.

Also won't it have performance problem if all rows are marked locked
till they are committed.

TIA."rkusenet" <rkusenet@.sympatico.ca> wrote in message
news:bq071h$1tbn6v$1@.ID-75254.news.uni-berlin.de...
> Hi,
> I am still not very proficient in SQLServer. So apology if the
> question sounds basic.
> We have a script to clean old unwanted data. It basically deletes
> all rows which are more than 2 weeks old. It deletes data from
> 33 tables and the number of rows in each table runs into few millions.
> What I see in the script (not written by me :-) ) is that all data is
> deleted within a single BEGIN TRANSACTION and COMMIT TRANSACTION. As
> I have background in informix, such an action in Informix may result
> in "LONG TRANSACTION PROBLEM". Does SQLServer have a similar concept.

I'm not sure what the "LONG TRANSACTION PROBLEM" in Informix is, but yes,
you basically don't want transactions to run for long periods of time.

As you point out, this can cause blocking on reads depending on your
isolation level. In addition, it can make recovery that much harder.

Assume your transaction takes 20 minutes to run (that's a really long time
admittedly) and your server gets rebooted 19 minutes into it. You're
looking at probably at least 19 minutes of rollback time when you reboot.
During this time the database will be completely inaccessible.

You have a couple of options that depend on your environment.

One is to simply break this up into multiple transactions. Of course then
it depends on what happens if one fails. Does this matter to other
transactions? Are their any dependencies?

It may also be possible to copy the needed data into a temp table, truncate
the original table and move data back.

But regardless, I would try to redesign this.

> Also won't it have performance problem if all rows are marked locked
> till they are committed.
> TIA.|||"rkusenet" <rkusenet@.sympatico.ca> wrote in message news:<bq071h$1tbn6v$1@.ID-75254.news.uni-berlin.de>...
> Hi,
> I am still not very proficient in SQLServer. So apology if the
> question sounds basic.
> We have a script to clean old unwanted data. It basically deletes
> all rows which are more than 2 weeks old. It deletes data from
> 33 tables and the number of rows in each table runs into few millions.
> What I see in the script (not written by me :-) ) is that all data is
> deleted within a single BEGIN TRANSACTION and COMMIT TRANSACTION. As
> I have background in informix, such an action in Informix may result
> in "LONG TRANSACTION PROBLEM". Does SQLServer have a similar concept.
> Also won't it have performance problem if all rows are marked locked
> till they are committed.
> TIA.

If the data is unwanted, it seems unusual to delete it like that,
unless there really is a genuine requirement to do it inside a single
transaction. A large transaction like that can easily cause
performance and locking problems.

Assuming there isn't a real need to use a single transaction, then you
could do batch deletes instead, perhaps as an overnight maintenance
job to prevent users seeing unexpected results if they run queries
during the deletion.

Simon|||"Simon Hayes" <sql@.hayes.ch> wrote
> If the data is unwanted, it seems unusual to delete it like that,
> unless there really is a genuine requirement to do it inside a single
> transaction.

I don't see any such requirement. It can always continue next time if
the delete fails for some reason.

> A large transaction like that can easily cause
> performance and locking problems.

That's what I thought.

> Assuming there isn't a real need to use a single transaction, then you
> could do batch deletes instead, perhaps as an overnight maintenance
> job to prevent users seeing unexpected results if they run queries
> during the deletion.

I plan to remove BEGIN and COMMIT. This way each delete will be atomic
by itself.

thanks.

Wednesday, March 7, 2012

Login vs. User (basic questions)

In the Books Online, it is stated that '...a single login is mapped to one u
ser account created in each database the login is accessing'. As long as on
ly one login can be used in a db and a user is associated with a login, what
is the purpose of having a
distinction between a User and a Login? Why can't you just add a Login to a
database and skip the User? Also, what is the purpose of allowing the user
name to be different than the login name (I realize it defaults to the login
name)? Since connection
strings use the login name, what purpose is there to have the username poten
tially different?
I'm assuming this has something to do with database (as opposed to server) r
oles, in which case multiple users can be added to a role. However, why are
n't Logins added directly to roles instead of Users?Sorry, but you should try and keep this as simple as possible.
A login is a security mechanism to control access to SQL server.
A user account is a security mechanism to control access to databases once a
connection has been established to SQL Server with a login. That is why
logins are mapped to user accounts.
Logins can be added to server roles to give that login rights to perform
server level operations -- adding logins/users, adding databases, etc.
Users are added to database roles since you need a user account to connect
to the database. DBO is an important one -- database owner.
Don't forget to read up on groups -- especially public.
****************************************
***************************
Andy S.
MCSE NT/2000, MCDBA SQL 7/2000
andymcdba1@.NOMORESPAM.yahoo.com
Please remove NOMORESPAM before replying.
Always keep your antivirus and Microsoft software
up to date with the latest definitions and product updates.
Be suspicious of every email attachment, I will never send
or post anything other than the text of a http:// link nor
post the link directly to a file for downloading.
This posting is provided "as is" with no warranties
and confers no rights.
****************************************
***************************
"mt" <anonymous@.discussions.microsoft.com> wrote in message
news:55BC9EF1-E300-46D3-8EC8-D69BADF4843B@.microsoft.com...
quote:

> In the Books Online, it is stated that '...a single login is mapped to one

user account created in each database the login is accessing'. As long as
only one login can be used in a db and a user is associated with a login,
what is the purpose of having a distinction between a User and a Login? Why
can't you just add a Login to a database and skip the User? Also, what is
the purpose of allowing the user name to be different than the login name (I
realize it defaults to the login name)? Since connection strings use the
login name, what purpose is there to have the username potentially
different?
quote:

> I'm assuming this has something to do with database (as opposed to server)

roles, in which case multiple users can be added to a role. However, why
aren't Logins added directly to roles instead of Users?|||"mt" <anonymous@.discussions.microsoft.com> wrote in message
news:55BC9EF1-E300-46D3-8EC8-D69BADF4843B@.microsoft.com...
quote:

> In the Books Online, it is stated that '...a single login is mapped to one

user account created in each database the login is accessing'. As long as
only one login can be used in a db and a user is associated with a login,
what is the purpose of having a distinction between a User and a Login? Why
can't you just add a Login to a database and skip the User? Also, what is
the purpose of allowing the user name to be different than the login name (I
realize it defaults to the login name)? Since connection strings use the
login name, what purpose is there to have the username potentially
different?<
Thing of accessing SQL Server as a two-tier process. You need a login to
connect to SQL Server, then you need to grant a login access to a specific
database or databases. Where possible it's recommended to use Windows
Authentication, grant login authentication to NT groups, and assign specific
database access to those groups. Once this mechanism is in place, it's
simple a matter of adding a user or users to the particular group and you're
done!
Please read this paper for a more in-depth discussion:
http://www.microsoft.com/technet/tr...chnet/prodtechn
ol/sql/maintain/security/sp3sec/default.asp
quote:

> I'm assuming this has something to do with database (as opposed to server)

roles, in which case multiple users can be added to a role. However, why
aren't Logins added directly to roles instead of Users?<
Roles were designed to be used by applications that could connect via a
given account/password (hidden in the application) that have specific rights
on a database.|||> "mt" <anonymous@.discussions.microsoft.com> wrote in message
quote:

> news:55BC9EF1-E300-46D3-8EC8-D69BADF4843B@.microsoft.com...
one[QUOTE]
> user account created in each database the login is accessing'. As long as
> only one login can be used in a db and a user is associated with a login,
> what is the purpose of having a distinction between a User and a Login?

Why
quote:

> can't you just add a Login to a database and skip the User? Also, what is
> the purpose of allowing the user name to be different than the login name

(I
quote:

> realize it defaults to the login name)? Since connection strings use the
> login name, what purpose is there to have the username potentially
> different?<
>

Thing of accessing SQL Server as a two-tier process. You need a login to
connect to SQL Server, then you need to grant a login access to a specific
database or databases. Where possible it's recommended to use Windows
Authentication, grant login authentication to NT groups, and assign specific
database access to those groups. Once this mechanism is in place, it's
simple a matter of adding a user or users to the particular group and you're
done!
Please read this paper for a more in-depth discussion (watch line wrap):
http://www.microsoft.com/technet/tr...chnet/prodtechn
ol/sql/maintain/security/sp3sec/default.asp
quote:

>
server)[QUOTE]
> roles, in which case multiple users can be added to a role. However, why
> aren't Logins added directly to roles instead of Users?<
>

Roles were designed to be used by applications that could connect via a
given account/password (hidden in the application) that have specific rights
on a database.
quote:

>

Steve|||MT,
As a newbie on SQL server ( I'm an Oracle DBA ) I found out that the online
docs where prety clear on how to log into the server / database(s) Please re
ad the "Permissions Validation" part in the "Administring SQLserver/Managing
security /Security levels"