On Wed, July 6, 2005 10:43 am, Richard Davey said: > Hello Marek, > > Wednesday, July 6, 2005, 3:15:58 PM, you wrote: > > MK> You can select just the id, and provided that the query returns just > MK> zero or one row, you can spare one function call. > > Sure that will work fine - but I fail to see how it will save a > function call. You either select, check there was no mysql error and > then check numrows. Or you select count, check there was no mysql > error and check the count value. I fail to see how your method > provides on less function call, but please elaborate :) (and hey, > select count = less bytes transferred from MySQL to PHP, regardless ;) [pedantic] Actually, I think the "SELECT id" will transfer less data in the cases where no rows are returned. There are no rows to return, after all, whereas the count(*) will always return exactly one row. And in the case case where a row is returned, the id will probably be the same number of bytes as a count(*): a 32-bit integer. What's more, the num_rows is always available, so is PROBABLY pushed through the MySQL/PHP pipe from the get-go. So count(*) has the 32-bits for the number of rows, which is always 1, and 32-bits for the result, which is either 1 or 0. The "SELECT id" has the number of rows, which is 1 or 0, and either 32-bits more for the id, or nothing more, ignoring the overhead of a returned row, which should be the same in either case. [/pedantic] MySQL's "select count(*) " are optimized, but I believe that does not apply if you have a WHERE clause. Presumably, your id field is indexed. So it boils down to MySQL having to count the one (1) row returned, rather than just return the row, if had to "find" anyway, PLUS sometimes shoving 4 bytes "extra" through the MySQL/PHP connection, when the count(*) is 0. I really don't have any idea which of these is "faster", and I suspect you'd have a hell of a time making any significant improvement to any real-world application by choosing one over the other. But I'm guessing that if you actually managed to measure it accurately, "SELECT id" would be a gnat's whisker "faster" in cases where no rows were returned, and barely perceptible "faster" when there is a row, since MySQL doesn't have to "count()" the result set -- Which really means just copying the num_rows it has already calculated, most likely, in place of the actual result. Do feel free to benchmark on your hardware under your load conditions to find out, and use the one that saves you 0.00000001 seconds, if you've got nothing better to work on. :-) I'm not particularly interested in the answer, myself, as I'm confident that neither method is going to make a perceptible difference in performance at the level that a real user would notice. :-) :-) :-) Disclaimer: I could be 100% wrong in my analysis of the internal workings of software whose source I've never read. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php