|Think about Loose Coupling|
Re: Odd Database Behaviorby grinder (Bishop)
|on Dec 15, 2003 at 20:15 UTC||Need Help??|
If you just want to see whether a value appears in a column of a table, performing a count(*) is a fearfully expensive way of going about that. At the worst, you will perform a full table scan.
The query would be better formulated as select 1 from members where nickname='...'
If this is just a once-off, the simplest approach would be to use the do method, with something (error-checking notwithstanding) like:
If, on the other hand, you know you'll be pounding on this routine in an inner loop, it might then be worthwhile recasting it as a prepared statement:
The beauty of this approach is that you don't have to change your code. The access to the database is abstracted away in the nick_exists routine.
And as if that wasn't enough, you could further add
... although you want to be careful with that. The program will continue to assume that a nick that has just been taken is still available. That would be a bug. But, used with care, this can provide a healthy speed boost.