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

后端 未结 2 1149
太阳男子
太阳男子 2020-12-21 00:32

Which one the below approaches is more efficient:

  1. Single longer query:

    \"SELECT COUNT( * ) AS num
    FROM (
    
        SELECT users.UID, cnumber
                 
    
    
            
相关标签:
2条回答
  • 2020-12-21 01:06

    Actually I have somewhat different view on this. MYSQL support subqueries and one of the reasons being there is breaking the larger query into smaller. If you are using INNODB engine and are using the parameters correctly for example tmp table, heap table, bufferpool, or query cache then you may get better results then smaller queries. MYSQL was designed to execute alot of small queries so it would be better to have more small queries then a larger query.

    If a larger query was a better option then we did not had the need to create tool like Infinidb and INfobright that uses column based structure to run large queries, where as MYSQL is row based.

    So think about it, small queries would result better then a large single query and overhead is not of queries it is of connections and again MYSQL is very cheap with connections so no worries their either. BUt if your MYSQL is creating threads then you have nothing to worry about.

    In short small few queries vs giant large query would be my choice.

    0 讨论(0)
  • 2020-12-21 01:15

    measure the microtime difference ;-) I would go with the single query though, less connections, less wait time and it's designed to handle those kind of things.

    http://www.phpjabbers.com/measuring-php-page-load-time-php17.html

    <?php
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $start = $time;
    ?>
    "SELECT COUNT( * ) AS
    FROM (
    
        SELECT users.UID, cnumber
        FROM users
        JOIN identity ON users.UID = identity.UID
        WHERE  'abc'
        IN (google, facebook, twitter, linkedin)
    
    ) AS userfound
    JOIN jobs ON userfound.cnumber = jobs.cnumber
    WHERE JID = 24";
    
    
    if(resultfromquery == 1)
        //Some code here
    else
        //Some code here
    
    <?php
    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;
    $total_time = round(($finish - $start), 4);
    echo 'Page generated in '.$total_time.' seconds.';
    ?>
    
    0 讨论(0)
提交回复
热议问题