In my SQL query I just need to check whether data exists for a particular userid.
I always only want one row that will be returned when data exist.
I have two options
1. select count(columnname) from table where userid=:userid
2. select count(1) from tablename where userid=:userid
I am thinking second one is the one I should use because it may have a better response time as compared with first one.
There can be differences between
count(*)andcount(column).count(*)is often fastest for reasons discussed here. Basically, withcount(column)the database has to check ifcolumnis null or not in each row. Withcount(column)it just returns the total number of rows in the table which is probably has on hand. The exact details may depend on the database and the version of the database.Short answer: use
count(*)orcount(1). Hell, forget the count andselect userid.You should also make sure the
whereclause is performing well and that its using an index. Look intoEXPLAIN.