Welcome Guest, Not a member yet? Register   Sign In
Database Num_rows()?
#10

(03-02-2020, 09:23 PM)kilishan Wrote: The result returned from `getRows()` will always be an array. So a simple count() is all you need to see how many rows were returned.
Unless I'm missing something, it's not always "a simple count()". In some cases, queries can be enormously complicated with many tables joined and distinct or group by or sum/avg/max specified. The db method countAllResults does seem to take a lot of this into account, but the extra query that must be run also appears to obliterate any prior query results which might exist in the Builder object. And I'm not sure, but mightn't be some (probably very small) risk of a name collision between the numrows used as an alias for the COUNT(*) in the other query.

Perhaps most critically, this countAllResults function clearly contains more PHP code than a single mysqli_num_rows, sqlsrv_num_rows, or pg_num_rows function call and it requires another query to be run on the db server. In my experience, a db server is quite likely to be your bottleneck on a busy server. If the PHP client has some simple built-in function to return the number of rows corresponding to a query, why not use it? The coding effort to expose this function for the most popular database engines is probably trivial, and you can just throw an exception ("use countAllResults instead!") if the DBMS does not support such a function. I could be dead wrong, but I expect the vast majority of CI developers are using MySQLi, MS SQL, or PostGres, and all of those support this function.

To bolster my point, consider the PHP source code for the mysqli_num_rows function. It goes through a few macros but I think they end up just returning the value from a struct.
Reply


Messages In This Thread
Database Num_rows()? - by stlake2011 - 03-02-2020, 11:15 AM
RE: Database Num_rows()? - by tweenietomatoes - 03-02-2020, 12:48 PM
RE: Database Num_rows()? - by abatrans - 03-02-2020, 01:04 PM
RE: Database Num_rows()? - by InsiteFX - 03-02-2020, 02:18 PM
RE: Database Num_rows()? - by stlake2011 - 03-02-2020, 03:21 PM
RE: Database Num_rows()? - by tweenietomatoes - 03-02-2020, 03:38 PM
RE: Database Num_rows()? - by stlake2011 - 03-02-2020, 04:09 PM
RE: Database Num_rows()? - by kilishan - 03-02-2020, 09:23 PM
RE: Database Num_rows()? - by sneakyimp - 12-28-2020, 04:32 PM
RE: Database Num_rows()? - by nonebeliever - 03-03-2020, 02:55 AM
RE: Database Num_rows()? - by lukmim - 07-20-2021, 12:49 AM
RE: Database Num_rows()? - by ojmichael - 12-29-2020, 04:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB