Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which is better one big query or multiple small query? [closed]

which is better and efficient? one big query then just process the fetched query in the php or from php function will just create a loop function that queries small data. Please consider also that the table can be big (thousands of raw). Thanks.

Comments table

id | parent | msg ---+--------+---------       1  |   0    | hello    2  |   1    | hi       3  |   2    | whats up        4  |   3    | yow        5  |   1    | hellow    6  |   2    | nice        7  |   0    | great    

Expected output is this:

        Array         (             [0] => Array                 (                     [id] => 1                     [parent] => 0                     [value] => hello                     [child] => Array                         (                             [0] => Array                                 (                                     [id] => 2                                     [parent] => 1                                     [value] => hi                                     [child] => Array                                         (                                             [0] => Array                                                 (                                                     [id] => 3                                                     [parent] => 2                                                     [value] => whats up                                                     [child] => Array                                                         (                                                             [0] => Array                                                                 (                                                                     [id] => 4                                                                     [parent] => 3                                                                     [value] => yow                                                                 )                                                         )                                                 )                                             [1] => Array                                                 (                                                     [id] => 6                                                     [parent] => 2                                                     [value] => nice                                                 )                                         )                                 )                             [1] => Array                                 (                                     [id] => 5                                     [parent] => 1                                     [value] => hellow                                 )                         )                 )             [1] => Array                 (                      [id] => 7                     [parent] => 0                     [value] => great                 ) 
like image 242
Bry Avatar asked May 23 '16 01:05

Bry


People also ask

What is faster one big query or many small queries?

In Postgres (and probably any RDBMS to a similar extent, MySQL to a lesser extent), fewer queries are almost always much faster.

Which query is faster in SQL?

Use CASE instead of UPDATE UPDATE statement takes longer than CASE statement due to logging. On the other hand, CASE statement determines what needs to be updated and makes your SQL queries faster.


Video Answer


1 Answers

As a rule of thumb, the less queries the better.

This is a good question but this has been answered multiple times.

Is it better to return one big query or a few smaller ones?

Multiple small queries vs a single long query. Which one is more efficient?

One big query vs. many small ones?

Which one is faster single big query or few small queries?

What is faster, a big joined query with more PHP or multiple small selects with less PHP?

Should I use one big SQL Select statement or several small ones?

https://dba.stackexchange.com/questions/76973/what-is-faster-one-big-query-or-many-small-queries

https://dba.stackexchange.com/questions/35277/is-it-better-to-separate-a-big-query-into-multiple-smaller-queries

like image 61
Techie Avatar answered Sep 19 '22 13:09

Techie