i have a query use PDO, count the row first, if row >1 than fetch data
SELECT * WHERE id=:id
$row=$SQL->rowCount();
if($row>0){
while($data=$SQL-&
Performance difference should be negligible to null, since you are issuing only one query in both cases. The 2nd query has to fetch an extra column with the same value for every row matching id
, hence it might have a large memory footprint. Even without the COUNT(*)
the row count should be available, hence you should go with the 1st solution.
About your 2nd question, AFAIK either COUNT(id)
or COUNT(*)
will be faster with the index on id
, since the db engine will have to perform a range scan to retrieve the rows in question, and range scans are faster with indexes when filtering on the indexed column (in your case id = SOME_ID
).