开发者

place condition in the read or in the code?

开发者 https://www.devze.com 2023-03-21 22:25 出处:网络
I am checking if an item has a linked item in the database but cant decide where to put the condition.

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

精彩评论

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