Cómo funcionan las bases de datos relacionales (Parte 1)

Hola Habr! Le presento la traducción del artículo
"Cómo funciona una base de datos relacional" .


Cuando se trata de bases de datos relacionales, no puedo evitar pensar que falta algo. Se usan en todas partes. Hay muchas bases de datos diferentes: desde el pequeño y útil SQLite hasta el poderoso Teradata. Pero solo hay unos pocos artículos que explican cómo funciona la base de datos. Puede buscar "howdoesarelationaldatabaseworkwork" ("cómo funcionan las bases de datos relacionales") para ver qué pocos resultados hay. Además, estos artículos son cortos. Si está buscando las últimas tecnologías de moda (BigData, NoSQL o JavaScript), encontrará artículos más detallados que explican cómo funcionan.


¿Las bases de datos relacionales son demasiado antiguas y aburridas para ser explicadas fuera de los cursos universitarios, trabajos de investigación y libros?


imagen


, . 40 , . , - , . , . , , .


, , , . , , CRUD; . , , .


, (BigO). , , . , , : SQL . , , .


, , , . ; .


, 3 :




( , ...), , . , .


, . .


O(1) vs O(n2)


… !


( ) , . , ! , . (cost based optimization).



, . , . , , .


, " O (some_function() )", , some_function(a_certain_amount_of_data) .


**, , ** . , .


imagen


. , . , 1 1 . , :


  • O(1) ( ).
  • O(log(n)) .
  • O(n2), .
  • .


O(1) O(n2) . , , , 2000 .


  • O (1) 1
  • O (log (n)) 7
  • O (n) 2 000
  • O (n * log (n)) 14 000
  • O (n2) 4 000 000

O(1) O(n2) (4 ) 2 , . , . -.


, - . 1 000 000 ( ):


  • O (1) 1
  • O (log (n)) 14
  • O (n) 1 000 000
  • O (n * log (n)) 14 000 000
  • O (n2) 1 000 000 000 000

, , O (n2) ( !). 0 , , .



:


  • - O (1).
  • O (log (n)).
  • O (n).
  • O (n * log (n)).
  • O (n2).

: .


:



.


, :


  • /

, , n2, :


  • n4: ! .
  • 3n: ! , , ( ).
  • n: .
  • nn: , , ...

: « », . () .


MergeSort ( )


, ? ? sort ()… , … , sort ().


, : . , , , , . , join , merge join ( ).


Merge ()


, : 2 N / 2 N- N . .


, :


imagen


, 8 2 4- . 4- :


  • 1) ( = )
  • 2) , 8
  • 3) ,
  • 1,2,3, .
  • , 8 .

, 4- , «» .


, , merge:


array mergeSort(array a)
   if(length(a)==1)
      return a[0];
   end if

   //recursive calls
   [left_array right_array] := split_into_2_equally_sized_arrays(a);
   array new_left_array := mergeSort(left_array);
   array new_right_array := mergeSort(right_array);

   //merging the 2 small ordered arrays into a big one
   array result := merge(new_left_array,new_right_array);
   return result;

, , (: ). , ; , . , :


  • ,
  • , ( ), .

Division phase ( )


imagen


3 . — log(N) ( N=8, log(N) = 3).


?


! — . , 2. — , . ( 2).


Sorting phase ( )


imagen


() . , N = 8 :


  • 4 , 2
  • 2 , 4
  • 1 , 8

log (N) , N * log(N) .


merge sort


?


:


  • , , , , .

: in-place ( ).


  • /. , , . , 100 .

: .


  • / / .

, Hadoop ( ).


  • (!).

( ) , . , , .


, -


, , 3 . , . .



— . . :


imagen


2- :


  • , .
  • (integer, string, date …).

, , .


, , , , , . N , N — , , ? .


: : heap-organizedtables index-organizedtables. .



— , :


  • , ,
  • ,

,



imagen


N = 15 . , 208:


  • , 136. 136<208, 136.
  • 398>208, , 398
  • 250>208, , 250
  • 200<208, , 200. 200 , ( , , 200).

, , 40


  • , 136. 136 > 40, 136.
  • 80 > 40, , 80
  • 40= 40, . ( ) .
  • , , , .

. , , log (N) . , log(N), !



, . integer, , - . , , "country" (column 3) :


  • ,
  • , ,
  • "UKnode" .

log(N) N . , — .


(, , 2 , , …) , (.. ) ( ).


B+TreeIndex


, , . O(N) , (, ). , -, . . B+Tree. B+Tree :


  • () ( )
  • .

imagen


, ( ). , , « », ( ). O(log(N)) ( ). , .


B+Tree, 40 100:


  • 40 ( 40, 40 ), .
  • 40, , 100.

, M , N . log(N) . , , M M . M+log(N) N . , ( M + log (N) ), . (, 200 ) N (1 000 000 ), .


(!). (, , B+Tree):


  • B+Tree, .
  • B+Tree, O (log (N)) O (N).

, B+Tree . , . : B+ O (log (N)). , . , / / , O (log (N)) . , ( ).


, B+Tree. B+Tree , MySQL. , InnoDB ( MySQL) .


: , - B+ .


Hashtable (-)


— -. , . , - , - ( hash join). (, , ).


- — , . - :


  • - . ( ).
  • . , , , .


:


imagen


- 10 . , 5 , , , 5 . - 10 . , , :


  • 0, 0,
  • 1, 1,
  • 2, 2,

, , .


, 78:


  • - - 78, 8.
  • - 8, , , 78.
  • 78
  • 2 ( -, ).

, , 59:


  • - - 59, 9.
  • - 9 , — 99. 99!=59, 99 .
  • , (9), (79), …, (29).
  • .
  • 7 .

-


, , , !


- 1 000 000 ( , 6 ), 1 , 000059 . — -, , .


- . , - , :


  • ( — )
  • 2 ( — )
  • 2 ( — , )

- - O(1).


vs -


?


, .


  • Una tabla hash puede cargarse parcialmente en la memoria , y los segmentos restantes pueden permanecer en el disco.
  • Con una matriz, debe usar espacio contiguo en la memoria. Si está cargando una mesa grande, es muy difícil encontrar suficiente espacio continuo .
  • Para una tabla hash, puede seleccionar la clave deseada (por ejemplo, el país y el nombre de la persona).

Para obtener más información, puede leer el artículo sobre Java HashMap , que es una implementación eficiente de una tabla hash; No necesita comprender Java para comprender los conceptos presentados en este artículo.

Source: https://habr.com/ru/post/undefined/


All Articles