По следам Highload++ Siberia 2019 — 8 задач по Oracle

?v=1

Приведем выдержки из документации (12.1.0.2) по хранению различных типов данных в Oracle.

CHAR Data Type
The CHAR data type specifies a fixed-length character string in the database character set. You specify the database character set when you create your database. Oracle ensures that all values stored in a CHAR column have the length specified by size in the selected length semantics. If you insert a value that is shorter than the column length, then Oracle blank-pads the value to column length.

VARCHAR2 Data Type
The VARCHAR2 data type specifies a variable-length character string in the database character set. You specify the database character set when you create your database. Oracle stores a character value in a VARCHAR2 column exactly as you specify it, without any blank-padding, provided the value does not exceed the length of the column.

NUMBER Data Type
The NUMBER data type stores zero as well as positive and negative fixed numbers with absolute values from 1.0×10–130 to but not including 1.0×10126. If you specify an arithmetic expression whose value has an absolute value greater than or equal to 1.0×10126, then Oracle returns an error. Each NUMBER value requires from 1 to 22 bytes. Taking this into account, the column size in bytes for a particular numeric data value NUMBER (p), where p is the precision of a given value, can be calculated using the following formula: ROUND ((length (p)+s)/2))+1 where s equals zero if the number is positive, and s equals 1 if the number is negative.

Кроме того, возьмем выдержку из документации насчет хранения Null–значений.

A null is the absence of a value in a column. Nulls indicate missing, unknown, or inapplicable data. Nulls are stored in the database if they fall between columns with data values. In these cases, they require 1 byte to store the length of the column (zero). Trailing nulls in a row require no storage because a new row header signals that the remaining columns in the previous row are null. For example, if the last three columns of a table are null, then no data is stored for these columns.

Исходя из этих данных, строим рассуждения. Считаем, что в БД используется кодировка AL32UTF8. В этой кодировке русские буквы будут занимать 2 байта.

1) A и X, значение поля a 'Y' занимает 1 байт, значение поля x «Д» — 2 байта
2) B и Y, «Вася» в b значение дополнится пробелами до 10 символов и займёт 14 байт, «Вася» в d — займет 8 байт.
3) C и K. Оба поля имеют значение NULL, после них есть значащие поля, поэтому занимают по 1 байту.
4) C и Z. Оба поля имеют значение NULL, но поле Z — последнее в таблице, поэтому места не занимает (0 байт). Поле С занимает 1 байт.
5) K и Z. Аналогично предыдущему случаю. Значение в поле K занимает 1 байт, в Z — 0.
6) I и J. Согласно документации, оба значения займут по 2 байта. Длину считаем по взятой из документации формулы: round ((1 + 0)/2) +1 = 1 + 1 = 2.
7) J и X. Значение в поле J займет 2 байт, значение в поле X займет 2 байта.

Итого, правильные варианты: С и Z, I и J, J и X.

© Habrahabr.ru