cancel
Showing results for 
Search instead for 
Did you mean: 

replace function performance

Former Member
0 Kudos
1,817

replace function,on over 64000 bytes length string , takes 35 ms at ver 9.0.2 same procedure same function, less then 64000 bytes, takes 1.2 sec at ver 17

if command out the replace function line, there are no differences.

any suggestions?

Accepted Solutions (0)

Answers (1)

Answers (1)

Breck_Carter
Participant

When benchmarking how long SQL takes to run, be careful not to include unrelated overhead costs like how long ISQL takes to process SQL statements.

In this case, 10 UPDATE statements using REPLACE took 250 milliseconds, or 25 milliseconds per REPLACE plus UPDATE.

The benchmark SQL was put inside a BEGIN block to exclude ISQL overhead.

Conclusion: The long string may be expensive to handle, but it may not be the fault of REPLACE... maybe it is the two concatenation "+" operations, both of which require the entire string to be copied.

CREATE TABLE t (
   pkey INTEGER NOT NULL DEFAULT AUTOINCREMENT PRIMARY KEY,
   data LONG VARCHAR NOT NULL );
INSERT t ( data ) VALUES ( 
'oSL8qNuMEKAfGO266USE 
 ... Vlad's string ...
AIAgwL7TQhETRXgSShUf' );
COMMIT;
CHECKPOINT;

BEGIN
DECLARE @started TIMESTAMP;
SET @started = CURRENT TIMESTAMP;
UPDATE t SET data = REPLACE ( data, ' ', '[' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '[', '/' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '/', '*' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '*', '&' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '&', '^' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '^', '!' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '!', '+' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '+', '=' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '=', '~' ) WHERE pkey = 1;
UPDATE t SET data = REPLACE ( data, '~', ' ' ) WHERE pkey = 1;
SELECT DATEDIFF ( MILLISECOND, @started, CURRENT TIMESTAMP ) AS elapsed_msec;
END;

        elapsed_msec 
-------------------- 
                 250