Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Query for only specific rows in a table? Need Help with Qu..

 
   Database Forums (Home) -> MSEQ RSS
Next:  SQL 2005 Cluster Maintenance  
Author Message
Gordon10

External


Since: Oct 28, 2004
Posts: 2



(Msg. 1) Posted: Tue Oct 09, 2007 8:39 am
Post subject: Query for only specific rows in a table? Need Help with Query
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

We have a stored procedure that removes old data from a very large (millions
of rows) table and puts it into an archive table. It grabs the top 5000 rows
and moves them.

B/C the database is in production and is heavily used, the job is run every
5 minutes
to avoid performance issues. When first started, the query ran quickly b/c
it would find 5000 rows fast. now that it has been running for a week it
takes longer for the query to execute and is locking the tables for too long.

Is there a way to query only a specific set of rows in the query? We could
store the last known row that was deleted in a temp table and start the query
over from that last known row. The problem is we don't know how to do this
with code in the stored procedure. Our code is below. Can anyone help us
with this or point us in the right direction to get this accomplished?


INSERT INTO gbdb_arch..tests_to_archive

select top 5000 p.test_id from gbdb..tests p (NOLOCK) LEFT OUTER JOIN
gbdb_arch..tests_to_archive a (NOLOCK) ON

p.test_id = a.test_id where var_id in (select var_id from gbdb..variables
(NOLOCK) where pu_id <> 0)

AND Result_On < DATEADD(year, -2, getdate()) AND a.test_id IS NULL

go

gbdb_arch..bow_ArchiveHistoricalTestData 5000

 >> Stay informed about: Query for only specific rows in a table? Need Help with Qu.. 
Back to top
Login to vote
Russell Fields

External


Since: Feb 21, 2007
Posts: 457



(Msg. 2) Posted: Tue Oct 09, 2007 4:41 pm
Post subject: Re: Query for only specific rows in a table? Need Help with Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Gordon,

A few questions that could affect your performance.
1 - Do you have indexes to support the TOP 5000 select? If not, create an
index to support the select and delete.
2 - Are your statistics up-to-date? If not you should update those?
(sp_updatestats)
3 - Is your data becoming fragmented as you delete rows? If so you should
defragment the table?
(SS2000 & 2005 DBCC DBREINDEX, SS2005 ALTER INDEX)

Another observation is that your TOP 5000 (in the sample) does not have an
ORDER BY. Therefore, the 5000 rows being deleted are an undefined selection
from the qualifying rows. TOP makes much more sense with an ORDER BY.

How are you deleting the rows? If you are deleting by joining
gbdb_arch..tests_to_archive to gbdb..tests you join set it getting bigger
and bigger as you build up the archive table. For example
DELETE ts
FROM gbdb..tests ts JOIN gbdb_arch..tests_to_archive ar
ON ts.test_id = ar.test_id
If this is the problem, then you might need an index on tests_to_archive to
support the join.

RLF


"Gordon" wrote in message

> We have a stored procedure that removes old data from a very large
> (millions
> of rows) table and puts it into an archive table. It grabs the top 5000
> rows
> and moves them.
>
> B/C the database is in production and is heavily used, the job is run
> every
> 5 minutes
> to avoid performance issues. When first started, the query ran quickly
> b/c
> it would find 5000 rows fast. now that it has been running for a week it
> takes longer for the query to execute and is locking the tables for too
> long.
>
> Is there a way to query only a specific set of rows in the query? We
> could
> store the last known row that was deleted in a temp table and start the
> query
> over from that last known row. The problem is we don't know how to do
> this
> with code in the stored procedure. Our code is below. Can anyone help us
> with this or point us in the right direction to get this accomplished?
>
>
> INSERT INTO gbdb_arch..tests_to_archive
>
> select top 5000 p.test_id from gbdb..tests p (NOLOCK) LEFT OUTER JOIN
> gbdb_arch..tests_to_archive a (NOLOCK) ON
>
> p.test_id = a.test_id where var_id in (select var_id from gbdb..variables
> (NOLOCK) where pu_id <> 0)
>
> AND Result_On < DATEADD(year, -2, getdate()) AND a.test_id IS NULL
>
> go
>
> gbdb_arch..bow_ArchiveHistoricalTestData 5000

 >> Stay informed about: Query for only specific rows in a table? Need Help with Qu.. 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 31, 2006
Posts: 335



(Msg. 3) Posted: Tue Oct 09, 2007 10:58 pm
Post subject: Re: Query for only specific rows in a table? Need Help with Query [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Tue, 9 Oct 2007 08:39:00 -0700, Gordon wrote:

>We have a stored procedure that removes old data from a very large (millions
>of rows) table and puts it into an archive table. It grabs the top 5000 rows
>and moves them.
>
>B/C the database is in production and is heavily used, the job is run every
>5 minutes
>to avoid performance issues. When first started, the query ran quickly b/c
>it would find 5000 rows fast. now that it has been running for a week it
>takes longer for the query to execute and is locking the tables for too long.
>
>Is there a way to query only a specific set of rows in the query? We could
>store the last known row that was deleted in a temp table and start the query
>over from that last known row. The problem is we don't know how to do this
>with code in the stored procedure. Our code is below. Can anyone help us
>with this or point us in the right direction to get this accomplished?
>
>
>INSERT INTO gbdb_arch..tests_to_archive
>
>select top 5000 p.test_id from gbdb..tests p (NOLOCK) LEFT OUTER JOIN
>gbdb_arch..tests_to_archive a (NOLOCK) ON
>
>p.test_id = a.test_id where var_id in (select var_id from gbdb..variables
>(NOLOCK) where pu_id <> 0)
>
>AND Result_On < DATEADD(year, -2, getdate()) AND a.test_id IS NULL
>
>go
>
>gbdb_arch..bow_ArchiveHistoricalTestData 5000

Hi Gordon,

In addition to Russell's reply, some more points.

1. What version of SQL Server are you using? SQL 2005 has a new option
(the OUTPUT option) that you can leverage for a tremendous performance
boost.

2. Why are you using (NOLOCK). Are you aware of the risks of reading
dirty data, missing rows, or reading rows twice? Will you really risk
archiving dirty data for a performance gain?

3. I assume that the stored proc bow_ArchiveHistoricalTestData does the
actual delete. That means that copying to archive and purging the
original are not only in seperate transactions; they are even in
seperate batches. You run the risk that the insert succeeds, but the
delete fails - and you even run the risk that the insert fails and the
delete succeeds, causing you to lose data permanently!!

4. I agree with Russell that the real problem is probably in the
bow_ArchiveHistoricalTestData procedure. Can you please post that code?

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Query for only specific rows in a table? Need Help with Qu.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Query returning multiple rows - Hi, I have a query that returns back rows that have multiple entries for a given UniqueID. I want the query to be expanded so that it then only returns 1 of the multiple rows based on the maximum date. Thefore, my return set looks like this: ..

Insert Multiple Rows into a Table Without Using Select - Hi. I want to insert 4 rows into a SQL table. Can I do this without using 4 Insert statements? This is my SQL statement: Insert Into MyTable Values (1,2) (3,4) (5,6)...

Merging mutliple rows from one table into columns in an ot.. - Hello, I have two tables. One is an export from a different database and one is the destination table. The Export table looks something like this: EmpID = the ID of an employee Contract = Code for the contract(s) the employee might have MainContract ...

Table Query - I have a table that has the following three fields and values that I am trying to build a query on; Column name-FEDERAL, FICA1, FICA2 Values- 1 , 100 , 200 2 , 300 , 400 How can I get a query to...

sql query single table - I am trying to run a report. The requestor has specifics on the way it should be formatted on output. I'm going to try and explain what he's looking for as well as give the example of output which was given me. The "Central Host" is the s...
   Database Forums (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]