473,406 Members | 2,439 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,406 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 28032
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.