N
N
Nikita Stechkin2021-02-27 19:01:18
ORM
Nikita Stechkin, 2021-02-27 19:01:18

What is the big O of database fetch methods?

I was wondering how the database selection methods work,
i.e. if you take laravel:

User::find($id);

Or take node js: Then these methods in both laravel and node js bypass each field in the database (O (n)) or access this field , how to array element(O(1))?

db.collection('users').find({"email": email})

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2021-02-27
@VAMPIRE37

These methods neither in Laravel nor in Node bypass any fields, all the work is done by the DBMS. How exactly it does this depends on the DBMS and on the data schema.

S
Stalker_RED, 2017-11-19
@Stalker_RED

If without foreach, then array_filter() can be used.
If the data is taken from the database, then why not immediately select only those where not null?

I
ipokos, 2017-11-19
@ipokos

<?php
$array = array(
    [
        'id' => '1',
        'user_id' => 1,
        'child' => [
            [
                'id' => 1,
                'item_id' => 1,
                'project_id' => 3
                ],
             [
                'id' => 2,
                'item_id' => 2,
                'project_id' => 0
                ]
            ]
        ]
    );
    
    var_dump(array_filter($array[0]['child'], function($v, $k) {
    return $k == 'project_id' && $v != 0;
}, ARRAY_FILTER_USE_BOTH));

Result:
array(1) {
  [0]=>
  array(3) {
    ["id"]=>
    int(1)
    ["item_id"]=>
    int(1)
    ["project_id"]=>
    int(3)
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question