You are here

public function Scheme::calculateClusteredIndexRowSizeBytes in Drupal driver for SQL Server and SQL Azure 8.2

Estimates the row size of a clustered index.

Return value

int

See also

https://msdn.microsoft.com/en-us/library/ms178085.aspx

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Scheme.php, line 668

Class

Scheme

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function calculateClusteredIndexRowSizeBytes($table, $fields, $unique = true) {

  // The fields must already be in the database to retrieve their real size.
  $info = $this
    ->TableDetailsGet($table);

  // Specify the number of fixed-length and variable-length columns
  // and calculate the space that is required for their storage.
  $num_cols = count($fields);
  $num_variable_cols = 0;
  $max_var_size = 0;
  $max_fixed_size = 0;
  foreach ($fields as $field) {
    if ($this
      ->IsVariableLengthType($info['columns'][$field]['type'])) {
      $num_variable_cols++;
      $max_var_size += $info['columns'][$field]['max_length'];
    }
    else {
      $max_fixed_size += $info['columns'][$field]['max_length'];
    }
  }

  // If the clustered index is nonunique, account for the uniqueifier column.
  if (!$unique) {
    $num_cols++;
    $num_variable_cols++;
    $max_var_size += 4;
  }

  // Part of the row, known as the null bitmap, is reserved to manage column nullability. Calculate its size.
  $null_bitmap = 2 + ($num_cols + 7) / 8;

  // Calculate the variable-length data size.
  $variable_data_size = empty($num_variable_cols) ? 0 : 2 + $num_variable_cols * 2 + $max_var_size;

  // Calculate total row size.
  $row_size = $max_fixed_size + $variable_data_size + $null_bitmap + 4;
  return $row_size;
}