yhat / db.py

db.py is an easier way to interact with your databases
BSD 2-Clause "Simplified" License
1.22k stars 111 forks source link

Adding count rows feature to Table class #64

Closed maurobaraldi closed 9 years ago

maurobaraldi commented 9 years ago

This pull request solves issues 45 and 63.

selwin commented 9 years ago

I think this implementation is less than ideal. Doing len(self.all()) means we're fetching all rows from the table and performing the count in Python which could be an expensive operation, depending on how large the table is.

I'd think a better implementation of count would be to run a SELECT COUNT(ID) on the table to return the number of rows.

maurobaraldi commented 9 years ago

I agree with your argument. But i don't think that assume that every table will have an id column. And execute and select count(*) from table name would be too inefficient (or efficient) as len(self.all()).

Anyway I appreciate suggestions to improve implementation :-)

selwin commented 9 years ago

Ah yes, I forgot that not all tables have id column. In that case SELECT COUNT(1) would be best.

As far as I know, some databases recognize what the user really wants and only returns the rown coutn when SELECT COUNT(*) is performed. But even if they don't, it will still be more efficient than len(self.all()) as the counting is done by the DB, as opposed to having all the rows transferred (possibly over the network) to Python, having the content evaluated and then counted.

I think SELECT COUNT(1) is a safer choice.