473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Unwanted Sort

Hi All,
I am trying to delete rows from a table using a SQL statement similar
to this:

DELETE FROM TableA where ID1 IN
(Select ID1 From TableB where ID2>= @min and ID2<=@max)

Basically I want to delete all rows from TableA that have an ID in a
range in TableB. This is done in a stored proc.
When I look at the execution plan, it is using the indexes as I would
hope for. The problem is that it is doing a sort which accounts for
73% of the cost. I do not need to sort the results. I don't care what
order they are deleted in.

How can I prevent the sort from occuring? I need this delete to occur
as fast as possible.

Thanks In Advance

Mar 21 '06 #1
4 1438
ha*****@yahoo.com wrote:
Hi All,
I am trying to delete rows from a table using a SQL statement similar
to this:

DELETE FROM TableA where ID1 IN
(Select ID1 From TableB where ID2>= @min and ID2<=@max)

Basically I want to delete all rows from TableA that have an ID in a
range in TableB. This is done in a stored proc.
When I look at the execution plan, it is using the indexes as I would
hope for. The problem is that it is doing a sort which accounts for
73% of the cost. I do not need to sort the results. I don't care what
order they are deleted in.

How can I prevent the sort from occuring? I need this delete to occur
as fast as possible.

Thanks In Advance


This is just a guess. Try:

DELETE FROM TableA
WHERE EXISTS
(SELECT *
FROM TableB
WHERE id2 BETWEEN @min AND @max
AND TableB.id1 = TableA.id1) ;

That's untested. Make sure you have a backup and test it out before you
try it against real data.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Mar 21 '06 #2
I thought about this. I went ahead and tested it. No real difference
in either the execution plan or the actual results.

Mar 21 '06 #3
ha*****@yahoo.com (ha*****@yahoo.com) writes:
I am trying to delete rows from a table using a SQL statement similar
to this:

DELETE FROM TableA where ID1 IN
(Select ID1 From TableB where ID2>= @min and ID2<=@max)

Basically I want to delete all rows from TableA that have an ID in a
range in TableB. This is done in a stored proc.
When I look at the execution plan, it is using the indexes as I would
hope for. The problem is that it is doing a sort which accounts for
73% of the cost. I do not need to sort the results. I don't care what
order they are deleted in.

How can I prevent the sort from occuring? I need this delete to occur
as fast as possible.


Probably the sorting occurs, because it is used for something, presumably
a merge join.

I don't know the exact rules for your purge, but I think you should get
all ids for TableA into one table with an IDENTITY column likes:

INSERT PurgeA (id1)
SELECT Id1
FROM TableA
WHERE ....
ORDER BY Id1

Add an index on the identity column as well as on id1. Then:

SELECT @last = 0
SELECT @first = min(ident) FROM PurgeA
WHERE ident > @last
SELECT @firstid = id1 FROM PurgeA WHERE ident = @first
SELECT @last = @firstid + 100000
SELECT @lastid = id1 FROM PurgeA WHERE ident = @last
IF @@rowcount = 0
SELECT @lastid = MAX(ident) FROM PurgeA

DELETE TableA
FROM TableA T
WHERE id1 BETWEEN @firstid AND @lastid
AND EXISTS (SELECT *
FROM PurgeA p
WHERE p.id1 AND T.id1)

I think it is important to have the chunk condition on the target
table, and not on a second table.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 21 '06 #4
the first thing the engine will do is grab all the valid id1's from
tableb, adn stuff them into a "temp table."

Then, because Table A has an index on ID1, it makes sense to sort that
temp table so you can go through TableA efficiently.

A question. Do you have a clustered index on TableA? Try getting rid
of the clustered index, and just having a non-clustered index.

regards,
doug

Mar 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Zupan | last post by:
I have a list that includes files and directories ie: list = I want to sort it so it looks like this I'm just wondering if there is an easy way to do this
3
by: StopBsod | last post by:
Hello group, I use XSLT to output a unix shell script based on the content of an XML file : The XSLT : <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">...
1
by: Denzil | last post by:
Hi, I am using the DataView.Sort Property (on a DataView column) to sort the View based on some condition. Now the column that I need to sort contains characters and HTML code that should not...
4
by: David Beck | last post by:
I donwnload some files for processing every day that have unwanted characters in them. In VB6 I use the InputB to read in the text and the StrConv. vLinesFromFile =...
3
by: Raymond Lewallen | last post by:
I have a modal dialog displaying a datagrid control. Click on the column header to sort the datagrid opens a new window with the following in the location: ...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
5
by: Tom | last post by:
I am having a problem where an extra gap (whitespace, padding) appears in a table cell where I don't want it. The data fed in the table is via dynamic HTML, so the amount in the middle cell may...
4
by: ljsmith91 | last post by:
I am reading in a file that has a list that of items. The items I place into a scalar for print but when i do, there is an extra unwanted charachter that shows up at the end of each item. It looks to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.