I'm looking through some code an old developer wrote, who is no longer around. I found something that I'm not quite sure makes sense to be doing, but I don't think I have enough knowledge or good enough google fu to find the answer, so here goes:
The code looks like this:
var queryCount = SELECT COUNT(Col1) FROM MyTable WHERE ColumnInIndex = 'SomeValue'
if(queryCount == 0)
return [];
var results = SELECT Col1, Col2, Col3,... FROM MyTable WHERE ColumnInIndex = 'SomeValue'
return results;
So my first thought was that seems redundant and should just use the results from the select query in the first place. I dug a little farther because the commit suggests its for perf tuning.
From what I understand from googling is that count in fact would be faster because it doesn't have to return any data or pull any rows & can rely solely on the index.
However, in the pseudocode I've written above, the case in which the where clause wouldn't return any rows it seems like we wouldn't get a performance boost because neither count nor select would return any results. Plus, when there are actually results we're doing two of these queries instead of just the one.
Additionally, it seems like since we're using the rows in the first place, we should just get the data in one query vs making two trips to the db.
Am I completely off base here?
We're using MySql v5.6.46