I am checking if an item has a linked item in the database but cant decide where to put the condition.
option one: place the condition in the database read statement.
select * from item where id = id.
Option two check afterwards in code.
temp = select * from item
for(i = 0; i <sizeof(temp); i++;
{
if(id = temp.id)
{do stuff}
}
开发者_开发百科Is there a significant difference between the two ?
note code sample probably contains tons of errors, is for explanation purpose only.
Definitely do it in the database sql statement!
Otherwise you're going to return potentially millions of results just to get one result.
Yes. In the first, you're asking the database for a smaller result set, which may require less effort by the database, less network traffic, and less work by your code.
With the second, you're asking to be featured on thedailywtf.
精彩评论