Showing posts with label states. Show all posts
Showing posts with label states. Show all posts

Thursday, March 22, 2012

Book recommendation on performance

I have the Transact-SQL Programming book from O'Reilly. It was
published in 1999. It states that "SELECT ... INTO" statements end up
locking the entire database of the target table. Since the tempdb is
also involved (in many cases), this creates major deadlocks for the
entire database and all users. It suggests using the "INSERT ...
SELECT" form instead.

Considering that the book is somehow dated, is this recommendation
still valid, especially on target sizes of up to 5 million records?php newbie (newtophp2000@.yahoo.com) writes:
> I have the Transact-SQL Programming book from O'Reilly. It was
> published in 1999. It states that "SELECT ... INTO" statements end up
> locking the entire database of the target table. Since the tempdb is
> also involved (in many cases), this creates major deadlocks for the
> entire database and all users. It suggests using the "INSERT ...
> SELECT" form instead.
> Considering that the book is somehow dated, is this recommendation
> still valid, especially on target sizes of up to 5 million records?

To a large extent, no. The author seems to have had SQL 6.5 in mind, where
SELECT INTO a temptable, indeed to bring a server to a stand still. The
problem is that SELECT INTO creates the temp table, and then goes on to
fill it with data. Since the query is one transaction, it keeps a lock on
the system tables until the query has completed. This was fatal in SQL 6.5
which only had page locks. In SQL7 and SQL2000 where you have row locks,
the impact on other processes is much smaller.

The advantage of SELECT INTO is that is that is minimally logged, so
SELECT INTO #tmp for five million rows can be faster and take less
toll on the server than CREATE TABLE INSERT INTO.

Nevertheless, there are contexts where SELECT INTO is an inferior choice
over CREATE TABLE not talking about a table variable. Say that you already
have a transaction in progress, and you creating many small tables
repeatedly within this transaction. SELECT INTO takes out more locks on
than CREATE TABLE, so you acquire a whole lot more locks with SELECT
INTO, and this can have an impact on performance. Case in point: I had
a procedure which suddently started to perform much slower than before.
This procedure performs some complex data updating in an iterative
fashion. My profiling pointed to a seeminginly innocent query which
appeared to take longer and longer time as the procedure proceeded.
Eventually I found the answer in a trigger (which was not affected by
this query). I had replaced direct deferences to "inserted" with temp
table created through "SELECT * INTO #inserted FROM inserted".

To summarize: SELECT INTO is fine for single-time queries on large
tables. It is bad to have in triggers in tables which maninly are
updated one row at a time.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Monday, March 19, 2012

BOL

Hello All,
BOL states that :-
Before you create a view, consider these guidelines and one of the
guideline is
"The query defining the view cannot include the ORDER BY, COMPUTE, or
COMPUTE BY clauses or the INTO keyword."
But we can include Order By in A view if we use TOP like this
create view v1 as
select top 100 percent * from tablename order by columnName
Any Thoughts
With warm regards
Jatinder SinghWhat's your question? I am not sure.
Have you checked the "CREATE VIEW" page in BOL, which states:
There are a few restrictions on the SELECT clauses in a view definition. A
CREATE VIEW statement cannot:
a.. Include ORDER BY clause, unless there is also a TOP clause in the
select list of the SELECT statement.
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"jsfromynr" <jatinder.singh@.clovertechnologies.com> wrote in message
news:1123504963.100055.120900@.f14g2000cwb.googlegroups.com...
Hello All,
BOL states that :-
Before you create a view, consider these guidelines and one of the
guideline is
"The query defining the view cannot include the ORDER BY, COMPUTE, or
COMPUTE BY clauses or the INTO keyword."
But we can include Order By in A view if we use TOP like this
create view v1 as
select top 100 percent * from tablename order by columnName
Any Thoughts
With warm regards
Jatinder Singh|||There are issues with doing that.
Yes you can, but doing will force SQL Server to execute the entire view and
perform the order by before using the information. There are other better
ways to accomplish the same thing...
If your view really needs to be sorted, then you should create an index on
the view. As the first index must be clustered, the provides a sort order.
That aside, back to your workaround. By creating a view without
specifying a top and order by, SQL Server has a chance to optimise the
combined select and view query. By doing this it can filter the data before
applying any sort operations. Don't forget that a view (unless indexed)
does not contain any data, it's effectively a pre-prepared SQL statement.
Hope this helps a little.
Regards
Colin Dawson
www.cjdawson.com
"jsfromynr" <jatinder.singh@.clovertechnologies.com> wrote in message
news:1123504963.100055.120900@.f14g2000cwb.googlegroups.com...
> Hello All,
> BOL states that :-
> Before you create a view, consider these guidelines and one of the
> guideline is
> "The query defining the view cannot include the ORDER BY, COMPUTE, or
> COMPUTE BY clauses or the INTO keyword."
> But we can include Order By in A view if we use TOP like this
> create view v1 as
> select top 100 percent * from tablename order by columnName
> Any Thoughts
> With warm regards
> Jatinder Singh
>|||Hi
The guidelines might be for Updatable Views. However you can use Order
By clause in a view.
please let me know if u have any questions
best Regards,
Chandra
http://www.SQLResource.com/
http://chanduas.blogspot.com/
---
*** Sent via Developersdex http://www.examnotes.net ***|||Some people do suggest using this "trick" to order views. I believe
that there are good reasons to avoid doing this.
This behaviour of TOP in a view is undocumented or at least,
under-documented. The ORDER BY is valid only for the purpose of
defining the TOP x PERCENT so intuitively you would not expect it to
apply to the result of a SELECT from the view. 99% of the time it *may*
work but there is no guarantee that it will always continue to work.
A view is supposed to behave like a table - without a logical order.
Sometimes you don't want the view to be sorted. Consider this example:
CREATE TABLE foo (x INTEGER PRIMARY KEY NONCLUSTERED, y INTEGER NOT
NULL)
GO
CREATE VIEW foo_view
AS
SELECT TOP 100 PERCENT x,y
FROM foo
ORDER BY y
GO
SELECT x FROM foo_view
The most efficient plan for the SELECT x query is an index scan of the
nonclustered index. The optimizer therefore has a choice either to
ignore the ORDER BY and retrieve an unsorted result set or to force a
sort which gives a sub-optimal execution plan. So TOP 100 PERCENT
either hurts performance, or doesn't work at all!
As always with specific engine behaviour, results could change between
different installations, service packs or versions of SQLServer, which
could break your code if it relies on an undefined feature.
In short, don't use undocumented tricks as a substitute for good design
and if you do use this feature be aware of its limitations and risks.
The "correct" and safe solution is simple:
SELECT ... FROM view ORDER BY ...
Why would you need to order the view itself?
Hope this helps.
David Portas
SQL Server MVP
--|||Hi All,
Thanks Naryan for your informative Answer
In Transact-SQL Reference (Location)
Include ORDER BY clause, unless there is also a TOP clause in the
select list of the SELECT statement.
In Creating and Maintaining Databases (Location)
The query defining the view cannot include the ORDER BY, COMPUTE, or
COMPUTE BY clauses or the INTO keyword.
(The ORDER BY clause is invalid in views, inline functions, derived
tables, and subqueries, unless TOP is also specified.)
Try this
select * fom (select * from tablename order by columnname) AA
The point you mentioned appear even in this case Derived Table . My
Question was simple why there is inconsitency while mentioning
information in BOL. Now prehaps I made myself clear. Thanks again for
showing interest .
David
Excellent post !!
As always your answer give something new to learn .
"So TOP 100 PERCENT either hurts performance, or doesn't work at all! "
The first part "So TOP 100 PERCENT hurts performance" is clear ( a bit)
As my assumption was that Query Optimizer will first expand the query
of view then it will rearrange the whole stuff and the execute it . My
assumption can be totally wrong .
I could not understand "doesn't work at all" part could you please
explain it further.
With Warm regards
Jatinder Singh|||"The optimizer therefore has a choice either to ignore the ORDER BY and
retrieve an unsorted result set [= TOP 100 doesn't work at all] or to force
a
sort which gives a sub-optimal execution plan [= hurts performance]". In
other words, both options are logically possible; neither is very
satisfactory.
David Portas
SQL Server MVP
--
"jsfromynr" wrote:

> Hi All,
> Thanks Naryan for your informative Answer
> In Transact-SQL Reference (Location)
> Include ORDER BY clause, unless there is also a TOP clause in the
> select list of the SELECT statement.
> In Creating and Maintaining Databases (Location)
> The query defining the view cannot include the ORDER BY, COMPUTE, or
> COMPUTE BY clauses or the INTO keyword.
> (The ORDER BY clause is invalid in views, inline functions, derived
> tables, and subqueries, unless TOP is also specified.)
> Try this
> select * fom (select * from tablename order by columnname) AA
> The point you mentioned appear even in this case Derived Table . My
> Question was simple why there is inconsitency while mentioning
> information in BOL. Now prehaps I made myself clear. Thanks again for
> showing interest .
> David
> Excellent post !!
> As always your answer give something new to learn .
> "So TOP 100 PERCENT either hurts performance, or doesn't work at all! "
> The first part "So TOP 100 PERCENT hurts performance" is clear ( a bit)
> As my assumption was that Query Optimizer will first expand the query
> of view then it will rearrange the whole stuff and the execute it . My
> assumption can be totally wrong .
> I could not understand "doesn't work at all" part could you please
> explain it further.
> With Warm regards
> Jatinder Singh
>|||On Mon, 08 Aug 2005 12:53:34 GMT, Colin Dawson wrote:
(snip)
>If your view really needs to be sorted, then you should create an index on
>the view.
Hi Colin,
That won't work. Clustered indexes govern how data is stored on the
disk, not in which order it is processed. The optimizer might still pick
an unexpected plan. The order might even change between two consecutive
executions of the same query. See repro below for a proof that an
indexed view doesn't guarantee that the data is returned in order.
The only way to make sure you get the data in the order you need is to
provide an ORDER BY clause in the SELECT statement actually used to
retrieve the data.
create table Beatles (ID int NOT NULL IDENTITY PRIMARY KEY,
fname varchar(20) NOT NULL,
lname varchar(20) NOT NULL)
go
create view SortedBeatles with schemabinding
as
select fname, lname from dbo.Beatles
go
create unique clustered index BeatleIndex on SortedBeatles (fname)
go
insert into Beatles
values ('John', 'Lennon')
insert into Beatles
values ('Paul', 'McCartney')
insert into Beatles
values ('George', 'Harrison')
insert into Beatles
values ('Ringo', 'Starr')
go
select fname, lname from SortedBeatles
go
drop view SortedBeatles
go
drop table Beatles
go
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:9kjff159m2i4qtb39a50hganommi87qgid@.
4ax.com...
> On Mon, 08 Aug 2005 12:53:34 GMT, Colin Dawson wrote:
> (snip)
> Hi Colin,
> That won't work. Clustered indexes govern how data is stored on the
> disk, not in which order it is processed. The optimizer might still pick
> an unexpected plan. The order might even change between two consecutive
> executions of the same query. See repro below for a proof that an
> indexed view doesn't guarantee that the data is returned in order.
> The only way to make sure you get the data in the order you need is to
> provide an ORDER BY clause in the SELECT statement actually used to
> retrieve the data.
> create table Beatles (ID int NOT NULL IDENTITY PRIMARY KEY,
> fname varchar(20) NOT NULL,
> lname varchar(20) NOT NULL)
> go
> create view SortedBeatles with schemabinding
> as
> select fname, lname from dbo.Beatles
> go
> create unique clustered index BeatleIndex on SortedBeatles (fname)
> go
> insert into Beatles
> values ('John', 'Lennon')
> insert into Beatles
> values ('Paul', 'McCartney')
> insert into Beatles
> values ('George', 'Harrison')
> insert into Beatles
> values ('Ringo', 'Starr')
> go
> select fname, lname from SortedBeatles
> go
> drop view SortedBeatles
> go
> drop table Beatles
> go
>
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)
I had a feeling that it wasn't guarenteed. Your dead right that the only
way to guarentee the sort order is to use the Order by clause. Bad on me
for trying to cut a corner.
Regards
Colin Dawson
www.cjdawson.com

Blocking...

Hi just read an article on the internet that states
"A quick way to resolve a blocking problem is to disconnect the
connection blocking the rest of the users. All you have to do is ask
your database administrator to execute the KILL statement. Obviously,
this is not a permanent solution, and won't really help anything except
take care of an immediate need."
Now when i run sp_who ... i see that the spid that is blocking is the
same as the spid itself...
Now the above article states that killing the one that is blocking the
rest of the other spids is ok ...
But what if its the same SPID that is blocking ...what does it signify
& whats the solution
ThanksI'm not too sure what version of SQL Server you're using, but if it is SQL
2000 sp4, this might be related to latch waits which now has enhanced
reporting: http://support.microsoft.com/default.aspx/kb/906344.
Alternatively it might be a parallel operation across multiple processors.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||On 31 Aug 2006 06:59:03 -0700, Double_B wrote:
>Hi just read an article on the internet that states
>"A quick way to resolve a blocking problem is to disconnect the
>connection blocking the rest of the users. All you have to do is ask
>your database administrator to execute the KILL statement. Obviously,
>this is not a permanent solution, and won't really help anything except
>take care of an immediate need."
Hi Double_B,
Also keep in mind that the transaction you kill has to rollback all
changes before it will release any locks. If it''s mainly a reporting
job, that will probably be pretty fast - but if you rollback a job that
has been changing table data for the last two hours, you can expect the
rollback to take as least as long!
--
Hugo Kornelis, SQL Server MVP

Blocking...

Hi just read an article on the internet that states
"A quick way to resolve a blocking problem is to disconnect the
connection blocking the rest of the users. All you have to do is ask
your database administrator to execute the KILL statement. Obviously,
this is not a permanent solution, and won't really help anything except
take care of an immediate need."
Now when i run sp_who ... i see that the spid that is blocking is the
same as the spid itself...
Now the above article states that killing the one that is blocking the
rest of the other spids is ok ...
But what if its the same SPID that is blocking ...what does it signify
& whats the solution
ThanksI'm not too sure what version of SQL Server you're using, but if it is SQL
2000 sp4, this might be related to latch waits which now has enhanced
reporting: http://support.microsoft.com/default.aspx/kb/906344.
Alternatively it might be a parallel operation across multiple processors.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||On 31 Aug 2006 06:59:03 -0700, Double_B wrote:

>Hi just read an article on the internet that states
>"A quick way to resolve a blocking problem is to disconnect the
>connection blocking the rest of the users. All you have to do is ask
>your database administrator to execute the KILL statement. Obviously,
>this is not a permanent solution, and won't really help anything except
>take care of an immediate need."
Hi Double_B,
Also keep in mind that the transaction you kill has to rollback all
changes before it will release any locks. If it''s mainly a reporting
job, that will probably be pretty fast - but if you rollback a job that
has been changing table data for the last two hours, you can expect the
rollback to take as least as long!
Hugo Kornelis, SQL Server MVP