开发者

place condition in the read or in the code?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜