Home > Net >  inserting big random data into blob column
inserting big random data into blob column

Time:12-18

I need to debug some flow and to do that, I need to generate at least some 100k rows, using say:

insert into a.b(DATA, ID, ORDER_ID) 
SELECT UTL_RAW.CAST_TO_RAW('too short!'), 
       raw_to_guid(sys_guid()),
       level 
FROM dual CONNECT BY level <= 5;

but that data are too small! I don't care what data is it, it can be all ones. I can generate random bytes base64 encoded, but I have no idea how to pass it into oracle, but it would be even better to fix somehow the code above and generate it on db side. Can someone advice?

CodePudding user response:

You can define a function to generate a BLOB from random data; and since Oracle 12c that can be an on-the-fly function defined in a with clause (read more):

insert /*  WITH_PLSQL */ into b (data, id, order_id)
with
  function generate_blob return blob as
    l_blob blob;
  begin
    dbms_lob.createtemporary(l_blob, false);
    for i in 1..50 loop
      dbms_lob.append(
        l_blob,
        to_blob(utl_raw.cast_to_raw(dbms_random.string('p', 2000)))
      );
    end loop;
    return l_blob;
  end generate_blob;
select generate_blob,
       raw_to_guid(sys_guid()),
       level
from dual connect by level <= 5;

which inserts five rows with a random 100-byte BLOB value; this query shows the length and the first 20 bytes to show they are random:

select order_id, id, lengthb(data), rawtohex(dbms_lob.substr(data, 20, 1)) from b
ORDER_ID ID LENGTHB(DATA) RAWTOHEX(DBMS_LOB.SUBSTR(DATA,20,1))
1 AAFAB7EF-11F8-EED5-E053-182BA8C00F30 100000 773C2F707E7D406D712D575B3C3D3135273F276B
2 AAFAB7EF-12F8-EED5-E053-182BA8C00F30 100000 676E282367235D455E2F4F547872315A59705A31
3 AAFAB7EF-13F8-EED5-E053-182BA8C00F30 100000 422D5C503F64733A623C215A49696D4640643B3F
4 AAFAB7EF-14F8-EED5-E053-182BA8C00F30 100000 5426716867607C203F6D253A2843666F4E203472
5 AAFAB7EF-15F8-EED5-E053-182BA8C00F30 100000 2E353E5228723356407D566026374E4B5E617447

fiddle

You can make the BLOBs much bigger if you want, just by change the loop's upper bound from 50 to something larger.

  • Related