28
CHAPTER 11 Large Objects

CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Embed Size (px)

Citation preview

Page 1: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

CHAPTER 11Large Objects

Page 2: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Need for Large Objects• Data type to store objects that contain large amount of

text, log, image, video, or audio data.• Most companies have a need to store this type of data.• Oracle provides the following data types:

• LONG and LONG RAW• Character large object (CLOB)• National character large object (NCLOB)• Binary large object (BLOB)• Binary file (BFILE)

Page 3: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Understanding the Lob Architecture

Page 4: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Understanding BFILEs

Page 5: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

BasicFiles versus SecureFiles• SecureFile is a new LOB architecture introduced in Oracle

Database 11g.• In Oracle Database 12c, default is to use SecureFiles• The SecureFile architecture has many new

enhancements that improve the manageability and performance of LOBs.

• If you’re not using Oracle Database 12c or 11g, then your only option is to use the BasicFile architecture.

• This is the default type of LOB created, and it’s been available since Oracle version 8.

Page 6: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

SecureFile LOB Enhancements• Encryption (requires Oracle Advanced Security Option)• Compression (requires Oracle Advanced Compression

Option)• Deduplication (requires Oracle Advanced Compression

Option)

Page 7: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Prerequisites for SecureFiles• A SecureFile LOB must be stored in a tablespace using

the automated segment space management feature (ASSM).

• The DB_SECUREFILE initialization setting must be either PERMITTED or ALWAYS.

Page 8: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Creating a BasicFile LOB Column• Basic example:

create table patchmain(

patch_id number

,patch_desc clob);

• In prior code, table and LOB stored in the same tablespace.

Page 9: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Default Behavior of LOBs• LOBs by default are created as BasicFiles.• Oracle creates a LOB segment and a LOB index for each LOB column.• The LOB segment has a name of this format: SYS_LOB<string>.• The LOB index has a name of this format: SYS_IL<string>.• The <string> is the same for each LOB segment and its associated

index.• The LOB segment and index are created in the same tablespace as the

table unless you specify a different tablespace.• By default, nearly 4000 bytes of a LOB are stored in the table row

(inline).• With Oracle Database 11g release 2 and higher, a LOB segment and a

LOB index aren’t created until a record is inserted into the table (the so-called deferred segment creation feature). This means DBA/ALL/USER_SEGMENTS and DBA/ALL/USER_EXTENTS have no information in them until a row is inserted into the table.

Page 10: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Creating a LOB in a Different Tablespace from the Table• Allows you to maintain and manage LOB segment

separately from table segment and extents.

create table patchmain

(patch_id number

,patch_desc clob

,patch blob

) tablespace users

lob (patch_desc) store as (tablespace clob_data)

,lob (patch) store as (tablespace blob_data);

Page 11: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Creating a SecureFile LOB• Use the SECUREFILE clause• Must be in an ASSM managed tablespace

create table patchmain(

patch_id number

,patch_desc clob)

lob(patch_desc) store as securefile (tablespace lob_data);

Page 12: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Partitioning a LOB• Allows you to spread out data across several tablespaces.

CREATE TABLE patchmain(

patch_id NUMBER

,region VARCHAR2(16)

,patch_desc CLOB)

LOB(patch_desc) STORE AS (TABLESPACE patch1)

PARTITION BY LIST (REGION) (

PARTITION p1 VALUES ('EAST')

LOB(patch_desc) STORE AS SECUREFILE

(TABLESPACE patch1 COMPRESS HIGH)

TABLESPACE inv_data1

,

PARTITION p2 VALUES ('WEST')

LOB(patch_desc) STORE AS SECUREFILE

(TABLESPACE patch2 DEDUPLICATE NOCOMPRESS)

TABLESPACE inv_data2

,

PARTITION p3 VALUES (DEFAULT)

LOB(patch_desc) STORE AS SECUREFILE

(TABLESPACE patch3 COMPRESS LOW)

TABLESPACE inv_data3

);

Page 13: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Moving a LOB• Allows you to move a LOB to a separate tablespace from

the table.

alter table patchmain

move lob(patch_desc)

store as basicfile (tablespace inv_clob);

Page 14: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Adding a LOB Column• Business requirements frequently change.• You can add a column with a LOB datatype just like other

datatypes.• Use ALTER TABLE ... ADD

SQL> alter table inv add(inv_image blob);

Page 15: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Removing a LOB Column• Business requirements frequently change.• Might want to rename the column before dropping it (to

better determine if an application is using the column):

SQL> alter table patchmain rename column patch_desc to patch_desc_old;

SQL> alter table patchmain drop(patch_desc_old);

Page 16: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Caching LOBs• By default, LOBs are not placed in the SGA.• Use CACHE to place LOB in buffer cache for

reads/writes.• Use CACHE READS to place LOB in buffer cache for only

reads.• NOCACHE is the default, the LOB is not placed in the

buffer cache.

create table patchmain(

patch_id number

,patch_desc clob)

lob(patch_desc) store as (tablespace lob_data cache);

Page 17: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Storing LOBs In Line• By default, up to approximately 4000 characters of a LOB column are stored

in line with the table row. • If the LOB is over 4000 characters, then Oracle automatically stores the LOB

outside of the row data. • Main advantage of storing a LOB in row is that small LOBs (less than 4000

characters) require less I/O, because Oracle doesn’t have to search out of row for the LOB data.

create table patchmain(

patch_id number

,patch_desc clob

,log_file blob)

lob(patch_desc, log_file)

store as (

tablespace lob_data

enable storage in row);

Page 18: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Storing LOBs Out of Line• You can explicitly instruct Oracle to store the LOB outside

of the row with the DISABLE STORAGE IN ROW.

create table patchmain(

patch_id number

,patch_desc clob

,log_file blob)

lob(patch_desc, log_file)

store as (

tablespace lob_data

disable storage in row);

Page 19: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

SecureFile Compression• If you’re using SecureFile LOBs, then you can specify a

degree of compression.• The benefit is that the LOBs consume much less space in

the database.• The downside is that reading and writing the LOBs may

take longer.

ALTER TABLE patchmain

MODIFY LOB(patch_desc)

(COMPRESS HIGH);

Page 20: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

SecureFile Deduplicating• If you have an application where identical LOBs are

associated with two or more rows, you should consider using the SecureFile deduplication feature.

• When enabled, this instructs Oracle to check when a new LOB is inserted into a table and see whether that LOB is already stored in another row (for the same LOB column).

• If it’s already stored, then Oracle stores a pointer to the existing identical LOB.

• This can dramatically reduce space for applications that tend to store the same LOB value multiple times in multiple rows.

Page 21: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

SecureFile Deduplicating

CREATE TABLE patchmain(

patch_id NUMBER

,patch_desc CLOB)

LOB(patch_desc) STORE AS SECUREFILE

(DEDUPLICATE)

TABLESPACE inv_clob;

Page 22: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

SecureFile Encryption• You can transparently encrypt a SecureFile LOB column

(just like any other column).• Before you use encryption features, you must set up an

encryption wallet.

CREATE TABLE patchmain(

patch_id number

,patch_desc clob)

LOB(patch_desc) STORE AS SECUREFILE (encrypt)

tablespace inv_clob;

Page 23: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Migrating BasicFiles to SecureFiles• Create a new table, load the data from the old table, and

rename the tables.• Move the table.• Redefine the table online.

Page 24: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Viewing LOB Metadata• Troubleshooting often begins by looking at LOB metadata.

select table_name,column_name,index_name,tablespace_namefrom all_lobsorder by table_name;

• select• segment_name• ,segment_type• ,tablespace_name• from user_segments• where segment_name like 'SYS_LOB%' • or segment_name like 'SYS_IL%';

Page 25: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Loading a CLOB or BLOB

1. Create directory

2. Create table

3. Load LOB into table using DBMS_LOB package

Page 26: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Troubleshooting LOB space related issues

select

a.table_name

,a.column_name

,a.segment_name

,a.index_name

,b.bytes/1024/1024 meg_bytes

from user_lobs a

,user_segments b

where a.segment_name = b.segment_name;

Page 27: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Determining Blocks used by LOB• DBMS_SPACE.SPACE_USAGE for blocks being used by

a LOB.• Overloaded procedure, one form for BasicFile the other

for SecureFile

Page 28: CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most

Summary• LOBs are a datatype you can use to store large objects

such as text files, images, video, and so on.• Most companies have a need to store LOBs.• Use the SecureFile feature from 11g forward.• SecureFile is the new architecture and allows for

advanced LOB management.