You are here

public function Schema::calculateClusteredIndexRowSizeBytes in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php \Drupal\Driver\Database\sqlsrv\Schema::calculateClusteredIndexRowSizeBytes()

Estimates the row size of a clustered index.

See also

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

2 calls to Schema::calculateClusteredIndexRowSizeBytes()
Schema::addIndex in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Override DatabaseSchema::addIndex().
Schema::createPrimaryKey in drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php
Create a Primary Key for the table, does not drop any prior primary keys neither it takes care of cleaning technical primary column. Only call this if you are sure the table does not currently hold a primary key.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Schema.php, line 504
Definition of Drupal\Driver\Database\sqlsrv\Schema

Class

Schema

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
    ->queryColumnInformation($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;
}