Юра Гуляев. Oracle tables
-
Upload
aleksandr-motsjonov -
Category
Technology
-
view
1.203 -
download
2
description
Transcript of Юра Гуляев. Oracle tables
![Page 1: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/1.jpg)
Juri Guljajev
ORACLE TABLES
![Page 2: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/2.jpg)
WHAT WILL WE TALK ABOUT?
Tabl
es
Relational
External
Temporary
Heap organized (“normal”)
Index organized
(IOT)
Object
![Page 3: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/3.jpg)
LITTLE THEORY - HOW DATABASE STORE DATA?
• Smallest unit of data in Oracle is data block.
• Default size of data block is 8 kilobytes.
• Normally only one table rows can be added into one data block
• Database tries to store the whole row in one block
Data block
Customer id = 1 Customer id = 2
CUSTOMER
![Page 4: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/4.jpg)
LITTLE THEORY - ROWID
ROWID looks like AAAAaoAATAAABrXAAA
OOOOOOFFFBBBBBBRRR
• OOOOOO: The data object number that identifies the database segment (AAAAao in the example).
• FFF: The tablespace-relative datafile number of the datafile that contains the row (file AAT in the example).
• BBBBBB: The data block that contains the row (block AAABrX in the example).
• RRR: The row in the block. (AAA in the example)
![Page 5: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/5.jpg)
LITTLE THEORY - INDEX IN HEAP TABLE
Index is using ROWID to point on data from table.
SELECT * FROM customer WHERE id = 1;
![Page 6: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/6.jpg)
HEAP ORGANISED - KEY FEATURES
• Unordered collection of rows
• Retrieval order is not guaranteed
• Rows are inserted where they fit best
• Generally better DML performance (in compare to IOT)
• Worse query performance by PK (in compare to IOT)
• Can be stored in table cluster
![Page 7: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/7.jpg)
HEAP ORGANIZED - PCTFREE
The PCTFREE parameter sets the minimum percentage
of a data block to be reserved as free space for possible
updates to rows that already exist in that block.
Data block
PCTFREE 10
FREE
CREATE TABLE test
( id NUMBER)
PCTFREE 10;
![Page 8: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/8.jpg)
HEAP ORGANISED - PCTFREE
INSERT (example)
• 1 row size is: 5 bytes + 1900 bytes = 1905 bytes
• PCTFREE 10: (8192B – 10%) / 1905B = 3 rows
PCTFREE 10
3 rows
PCTFREE 50
2 rows
FREE
FREE
![Page 9: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/9.jpg)
HEAP ORGANISED - PCTFREE
UPDATE (example)
Rows, that can’t fit into block after update were moved to
new data block and references were left in old data block.
ROWID of migrated row is not changed.
Ref left here
PCTFREE 10
2 rows left
NEW BLOCK
PCTFREE 10
ROW
PCTFREE 10
First row
updated
3 rows
OLD BLOCK
1 row from old
block
UPDATE UPDATE
![Page 10: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/10.jpg)
HEAP ORGANISED - PCTFREE
UPDATE
Table, that have PCTFREE 50 have enough storage room
to fit added data into the same data block.
PCTFREE 50
2 rows
PCTFREE 50
2 rows
FREE
UPDATE
![Page 11: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/11.jpg)
HEAP ORGANISED - PCTFREE
• Better query performance
• Better storage usage
• Use with caution (set PCTFREE 50, but rows are filled
by 80% with insert)
![Page 12: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/12.jpg)
HEAP/INDEX ORGANISED - PARTITIONING
Partitioning helps to improve performance with high data
volumes and allows DBAs to manage data in more
convenient way.
Non-partitioned table Partitioned table
Partition 1
Partition 2
Partition n
![Page 13: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/13.jpg)
HEAP/INDEX ORGANISED - PARTITIONING
Table can be partitioned by
• Range (for historical data, often partitioned by date
column)
• Hash (no obvious partitioning column)
• List (to group data specifically by some known values)
Range and List partitions may have subpartitions.
![Page 14: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/14.jpg)
HEAP/INDEX ORGANISED - PARTITIONING
![Page 15: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/15.jpg)
HEAP/INDEX ORGANISED - PARTITIONING
Indexes on partitioned table can be global or local partitioned.
• Local
• Maps on table partitions, so easier to maintain
• Unique index can be local, but partitioning key must be part of index
• Global
• Has own partitions definitions
• Can be partitioned by range or hash
![Page 16: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/16.jpg)
INDEX ORGANISED - KEY FEATURES
• Stored in a variation of a B-tree index structure
• Primary key stores all rows
• Fast access by primary key (specially range scan)
• DML performance might be worse (in compare to HEAP)
• Take less room on disk (no separate primary key storage required)
• Cannot be stored in table cluster
![Page 17: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/17.jpg)
INDEX ORGANISED - KEY FEATURES
![Page 18: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/18.jpg)
INDEX ORGANISED - KEY FEATURES
PROS
• Fast access by PK
• Less storage for the same amount of data
• Data is ordered by PK
CONS
• Slower DML
• Secondary index can be slower and take more storage in compare to index in HEAP table
![Page 19: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/19.jpg)
TABLE CLUSTER
Group of tables that share common columns and store
related data in the same blocks.
CUSTOMER
customer_id
IP_RESTRICTION
customer_id
TIME_RESTRICTION
customer_id
![Page 20: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/20.jpg)
TABLE CLUSTER
SELECT* FROM customer WHERE customer_id = :id;
SELECT * FROM ip_restriction JOIN time_restriction USING(customer_id) WHERE customer_id = :id;
SELECT * FROM customer JOIN ip_restriction USING(customer_id) JOIN time_restriction USING(customer_id) WHERE customer_id = :id;
![Page 21: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/21.jpg)
TABLE CLUSTER
In cluster single data block contains rows from several
clustered tables.
customer customer
customer
clustered
customer
ip_retstr.
time_retstr.
unclustered
![Page 22: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/22.jpg)
TABLE CLUSTER
And all data, that contains the same clustered key value
are stored together.
unclustered CUSTOMER_ID FIRST_NAME LAST_NAME …
1 AAA BBB
2 CCC DDD
CUSTOMER_ID IP
1 192.168.1.1
1 192.168.1.2
2 68.1.1.1
CUSTOMER_ID TIME
1 MON 8-18
1 WEN 10-15
2 MON 8-18
![Page 23: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/23.jpg)
TABLE CLUSTER
And all data, that contains the same clustered key value
are stored together.
clustered
CUSTOMER_ID FIRST_NAME LAST_NAME …
1 AAA BBB
IP
192.168.1.1
192.168.1.2
TIME
MON 8-18
2 CCC DDD
![Page 24: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/24.jpg)
TABLE CLUSTER
What you will get
• Disk I/O is reduced for joins of clustered tables.
• Access time improves for joins of clustered tables.
• Less storage is required to store related table and index data because the cluster key value is not stored repeatedly for each row.
But be careful (do not use when)
• The tables are frequently updated.
• The tables frequently require a full table scans.
• The tables require truncating.
![Page 25: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/25.jpg)
TABLE CLUSTER
There are 2 types of clusters
Index cluster
• Data is co-located within a single block based on a common
column index, so separate cluster index should be created
before adding table into cluster
Hash cluster
• Data is co-located within a single block based on a hash
key and a hash function (own hash function can be used)
![Page 26: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/26.jpg)
TABLE CLUSTER
Index VS Hash clusters
• Hash is faster
• Hash take less storage
• For hash you should know number of hash keys
• Hash cluster size and keys number can’t be changed
• Hash cluster loads CPU more, than index cluster
![Page 27: Юра Гуляев. Oracle tables](https://reader033.fdocuments.in/reader033/viewer/2022051013/546f9e52b4af9f440b8b45ec/html5/thumbnails/27.jpg)