473,382 Members | 1,745 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,382 software developers and data experts.

How to DELETE with COMMIT COUNT

I figure someone else may have use for this toy, so here goes:
Prereq: DB2 V8.1 FP4

CREATE PROCEDURE DELETE_MANY_ROWS
(tabschema VARCHAR(128),
tabname VARCHAR(128),
predicate VARCHAR(1000),
commitcount INTEGER)
BEGIN
DECLARE SQLCODE INTEGER;
DECLARE txt VARCHAR(10000);
DECLARE stmt STATEMENT;
SET txt = 'DELETE FROM (SELECT 1 FROM "'
|| tabschema || '"."' || tabname || '" WHERE '
|| predicate || ' FETCH FIRST ' ||
RTRIM(CHAR(commitcount)) || ' ROWS ONLY) AS D';
PREPARE stmt FROM txt;
l: LOOP
EXECUTE stmt;
IF SQLCODE = 100 THEN LEAVE l; END IF;
COMMIT;
END LOOP;
END
%

CALL DELETE_MANY_ROWS('SRIELAU', 'T1', 'c1 > 0', 1000)
%

Quiz!
How can performance be improved if the table has e.g. a known primary key?

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #1
5 28021
Serge Rielau wrote:
I figure someone else may have use for this toy, so here goes:
Prereq: DB2 V8.1 FP4

CREATE PROCEDURE DELETE_MANY_ROWS
(tabschema VARCHAR(128),
tabname VARCHAR(128),
predicate VARCHAR(1000),
commitcount INTEGER)
BEGIN
DECLARE SQLCODE INTEGER;
DECLARE txt VARCHAR(10000);
DECLARE stmt STATEMENT;
SET txt = 'DELETE FROM (SELECT 1 FROM "'
|| tabschema || '"."' || tabname || '" WHERE '
|| predicate || ' FETCH FIRST ' ||
RTRIM(CHAR(commitcount)) || ' ROWS ONLY) AS D';
PREPARE stmt FROM txt;
l: LOOP
EXECUTE stmt;
IF SQLCODE = 100 THEN LEAVE l; END IF;
COMMIT;
END LOOP;
I think you need an extra "COMMIT;" here, in case you're not in
auto-commit mode. Otherwise, the last few rows may not get committed.
END
%

CALL DELETE_MANY_ROWS('SRIELAU', 'T1', 'c1 > 0', 1000)
%
Thanks for posting this. I've been looking for a way to do this for
awhile. I have a question, though: why the "AS D" syntax?
Quiz!
How can performance be improved if the table has e.g. a known primary key?


The primary key name could be an argument to the procedure?

Thanks,
Dave
Nov 12 '05 #2
Dave Benjamin wrote:
Thanks for posting this. I've been looking for a way to do this for
awhile. I have a question, though: why the "AS D" syntax?

You got me. I forgot we made the correlation clause optional.
It is mandatory for derived selects in the from clause....
Quiz!
How can performance be improved if the table has e.g. a known primary
key?

The primary key name could be an argument to the procedure?

Well, looking up the key in the catalog would have yielded bonus points,
but that wasn't the point, so yes, assume a the column name for the PK
is given.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
I've been trying to modify this to use a date field rather than a numeric
as my predicate by adding a second predicate and more concats. However,
it always returns 'SQL0180N The syntax of the string representation of a
datetime value is incorrect. SQLSTATE=22007'. Is there a way to see how
the procedure parses the txt to find out how this date is being
interpreted?

create procedure DTF.DELETE_BY_DATE
(tabschema VARCHAR(128),
tabname VARCHAR(128),
predicate VARCHAR(1000),
deldate VARCHAR(1000),
commitcount INTEGER)
BEGIN
DECLARE SQLCODE INTEGER;
DECLARE txt VARCHAR(10000);
DECLARE stmt STATEMENT;
SET txt='DELETE FROM (SELECT 1 FROM
' || tabschema || '.' || tabname || ' WHERE
' || predicate ||'''||deldate||''' ||
' FETCH FIRST ' || RTRIM(CHAR(commitcount))
|| ' ROWS ONLY) AS D' ;
PREPARE stmt FROM txt;
I:LOOP
EXECUTE stmt;
IF SQLCODE = 100 THEN LEAVE I; END IF;
COMMIT;
END LOOP;
COMMIT;
END
%

Apr 19 '07 #4
Froth wrote:
I've been trying to modify this to use a date field rather than a numeric
as my predicate by adding a second predicate and more concats. However,
it always returns 'SQL0180N The syntax of the string representation of a
datetime value is incorrect. SQLSTATE=22007'. Is there a way to see how
the procedure parses the txt to find out how this date is being
interpreted?
I would add an exit exception handler that SIGNALs the TXT as MESSAGE_TXT
SIGNAL SQLSTATE '78000' SET MESSAGE_TEXT = txt;
something like it anyway...

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 19 '07 #5
Thanks.

Apr 19 '07 #6

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

Similar topics

1
by: F. GEIGER | last post by:
I've troubles to let my app take off using pysqlite. What I wonder most for now is that "pysqlite2.dbapi2.OperationalError: cannot commit transaction - SQL statements in progress" when I do this:...
3
by: Giganews | last post by:
I presently use an SQL SP with cursors to cycle through records to update, insert etc. Within this I maintain a variable for a commit count, as in before I FETCH the next record I increment...
2
by: heming_g | last post by:
drop procedure sp_DelTable_CommitCount@ CREATE PROCEDURE sp_DelTable_CommitCount(IN as_TableName VARCHAR(128) , IN as_Condition VARCHAR(1024) , IN abi_CommitCount BIGINT) LANGUAGE SQL...
4
by: Ian | last post by:
Hi, I have a problem with delete using where in clause. This is a query: delete from tab1 where id not in (select id from tab2) I calculated costs using select instead of delete: select...
4
by: Sam Durai | last post by:
Hello, I have a requirement to delete millions of rows from a table which has billions of rows. Hence I have coded a korn script to delete rows recursively and with a commit interval of 10000....
7
by: annecarterfredi | last post by:
I need to increase the length of CD column from char(5) to char(7) in DB2 V8.2. I am doing it this way: 1. create new_table with CD char(7) 2. do 'insert into new_table select * from...
5
by: toshibasat | last post by:
I have used the below procedure Serge posted long time back and used between date1 and date2 in the where clause witha a commit count of 1 million. But it is deleting only 250K rows / min. At this...
5
by: Roger | last post by:
backup log testdb with truncate_only DBCC SHRINKFILE (testdb_log, 100) WITH NO_INFOMSGS backup database testdb to disk = '\\DC01\Backups\DB01\testdb.bak' with init and does the shrinkfile...
8
by: ananthaisin | last post by:
How to reduce the table size for any table while using truncate or delete statements. In oracle 8i it was truncating the storage space but in 10g it is not .... I have given truncate statement in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.