So, SELECT count(*)... or mysql_num_rows(...)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FFMG

    So, SELECT count(*)... or mysql_num_rows(...)


    Hi,

    I was running a test on a table with 50000 rows.
    When I do:

    $sql = "SELECT * FROM TABLE";
    $result = mysql_query($sq l);
    $total = mysql_num_rows( result);

    I get a 'run out of memory error', (the limit is set low on the test
    server).

    But when I do:

    $sql = "SELECT count(*) as num FROM TABLE";
    $result = mysql_query($sq l);
    $result = mysql_fetch_ass oc( $result );
    $total = $result['num'];

    Everything works fine.

    or is
    $sql = "SELECT FOUND_ROWS() AS num FROM TABLE";
    ...
    even better?

    So What is the 'preferred' method of getting a row count?

    FFMG


    --

    'webmaster forum' (http://www.httppoint.com) | 'webmaster Directory'
    (http://www.webhostshunter.com/) | 'Recreation Vehicle insurance'
    (http://www.insurance-owl.com/other/car_rec.php)
    'Free URL redirection service' (http://urlkick.com/)
    ------------------------------------------------------------------------
    FFMG's Profile: http://www.httppoint.com/member.php?userid=580
    View this thread: http://www.httppoint.com/showthread.php?t=18782

    Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

  • ZeldorBlat

    #2
    Re: So, SELECT count(*)... or mysql_num_rows( ...)

    On Jul 26, 2:14 am, FFMG <FFMG.2ub...@ no-mx.httppoint.co mwrote:
    Hi,
    >
    I was running a test on a table with 50000 rows.
    When I do:
    >
    $sql = "SELECT * FROM TABLE";
    $result = mysql_query($sq l);
    $total = mysql_num_rows( result);
    >
    I get a 'run out of memory error', (the limit is set low on the test
    server).
    >
    But when I do:
    >
    $sql = "SELECT count(*) as num FROM TABLE";
    $result = mysql_query($sq l);
    $result = mysql_fetch_ass oc( $result );
    $total = $result['num'];
    >
    Everything works fine.
    >
    or is
    $sql = "SELECT FOUND_ROWS() AS num FROM TABLE";
    ..
    even better?
    >
    So What is the 'preferred' method of getting a row count?
    >
    FFMG
    >
    --
    >
    'webmaster forum' (http://www.httppoint.com) | 'webmaster Directory'
    (http://www.webhostshunter.com/) | 'Recreation Vehicle insurance'
    (http://www.insurance-owl.com/other/car_rec.php)
    'Free URL redirection service' (http://urlkick.com/)
    ------------------------------------------------------------------------
    FFMG's Profile:http://www.httppoint.com/member.php?userid=580
    View this thread:http://www.httppoint.com/showthread.php?t=18782
    >
    Message Posted via the webmaster forumhttp://www.httppoint.c om, (Ad revenue sharing).
    Always use "select count(*)" for this type of thing.

    The database doesn't necessarily need to go and actually get all the
    rows to figure it out this way. If you "select *" and then ask for
    the row count he actually needs to go get all those rows, which is why
    you're running out of memory.

    Comment

    • Rik

      #3
      Re: So, SELECT count(*)... or mysql_num_rows( ...)

      On Thu, 26 Jul 2007 08:14:09 +0200, FFMG <FFMG.2ubxux@ no-mx.httppoint.co m
      wrote:
      >
      Hi,
      >
      I was running a test on a table with 50000 rows.
      When I do:
      >
      $sql = "SELECT * FROM TABLE";
      $result = mysql_query($sq l);
      $total = mysql_num_rows( result);
      >
      I get a 'run out of memory error', (the limit is set low on the test
      server).
      >
      But when I do:
      >
      $sql = "SELECT count(*) as num FROM TABLE";
      $result = mysql_query($sq l);
      $result = mysql_fetch_ass oc( $result );
      $total = $result['num'];
      >
      Everything works fine.
      >
      or is
      $sql = "SELECT FOUND_ROWS() AS num FROM TABLE";
      ..
      even better?
      >
      So What is the 'preferred' method of getting a row count?
      If you don't need the rows themselves, keep use of resources at a low by
      using a COUNT() construct. If you do need all the rows, use
      mysql_num_rows( ), underneath the surface it is essentially the same as
      FOUND_ROWS(), with the added bonus of getting an integer straight back.
      --
      Rik Wasmus

      Comment

      • Captain Paralytic

        #4
        Re: So, SELECT count(*)... or mysql_num_rows( ...)

        On 26 Jul, 14:20, Rik <luiheidsgoe... @hotmail.comwro te:
        | mysql_num_rows( ), underneath the surface it is essentially the same
        as
        | FOUND_ROWS(), with the added bonus of getting an integer straight
        back.

        I don't think so Rik!

        mysql_num_rows( ) will tell you how many rows have been returned in
        this particular request.

        FOUND_ROWS() will tell you how many rows would have been returned if
        you did not have a LIMIT clause on the previous query and it also
        requires the previous query to have had SQL_CALC_FOUND_ ROWS in it.

        SELECT FOUND_ROWS() AS num FROM TABLE

        will return you the value of 0 for as many rows as there are in the
        table.

        Comment

        • Webrickco

          #5
          Re: So, SELECT count(*)... or mysql_num_rows( ...)

          On Jul 26, 3:03 pm, Captain Paralytic <paul_laut...@y ahoo.comwrote:
          On 26 Jul, 14:20, Rik <luiheidsgoe... @hotmail.comwro te:
          | mysql_num_rows( ), underneath the surface it is essentially the same
          as
          | FOUND_ROWS(), with the added bonus of getting an integer straight
          back.
          >
          I don't think so Rik!
          >
          mysql_num_rows( ) will tell you how many rows have been returned in
          this particular request.
          >
          FOUND_ROWS() will tell you how many rows would have been returned if
          you did not have a LIMIT clause on the previous query and it also
          requires the previous query to have had SQL_CALC_FOUND_ ROWS in it.
          >
          SELECT FOUND_ROWS() AS num FROM TABLE
          >
          will return you the value of 0 for as many rows as there are in the
          table.
          For compatibility purposes use something standard like select count.

          Comment

          • Captain Paralytic

            #6
            Re: So, SELECT count(*)... or mysql_num_rows( ...)

            On 27 Jul, 14:24, Webrickco <webric...@gmai l.comwrote:
            On Jul 26, 3:03 pm, Captain Paralytic <paul_laut...@y ahoo.comwrote:
            >
            >
            >
            >
            >
            On 26 Jul, 14:20, Rik <luiheidsgoe... @hotmail.comwro te:
            | mysql_num_rows( ), underneath the surface it is essentially the same
            as
            | FOUND_ROWS(), with the added bonus of getting an integer straight
            back.
            >
            I don't think so Rik!
            >
            mysql_num_rows( ) will tell you how many rows have been returned in
            this particular request.
            >
            FOUND_ROWS() will tell you how many rows would have been returned if
            you did not have a LIMIT clause on the previous query and it also
            requires the previous query to have had SQL_CALC_FOUND_ ROWS in it.
            >
            SELECT FOUND_ROWS() AS num FROM TABLE
            >
            will return you the value of 0 for as many rows as there are in the
            table.
            >
            | For compatibility purposes use something standard like select count.

            What has this got to do with my post?

            Comment

            Working...