0

it performs the same function in the 2 queries I wrote below, but which one should be preferred in terms of performance, can you please answer by explaining a reason.

Option 1

$user= DB::getRow('SELECT * FROM users WHERE id=1');
if($user){
$cv= DB::getRow('SELECT * FROM cvs WHERE userid=?',array($user->id));
}

Option 2

$user= DB::getRow('SELECT users.id,cvs.userid FROM users inner join cvs ON cvs.userid= users.id WHERE id=1');

Thanks in advance for the help

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
gagacode
  • 9
  • 5
  • If you need to get all the data, a JOIN isn't the worst idea. If you only care about two fields, even more so, but you're selecting, effectively, the same value twice. – tadman Jan 17 '23 at 21:37
  • 1
    In general, reducing the number of queries you send to the DB is preferred, so the JOIN is usually the better option. – Barmar Jan 17 '23 at 21:39
  • option 2 is better because you return the primary key and join also on the primary key. however it doesn't make sense to return twice the same value, unless you want to for example count them later in your code. in option 1 you are retrieving all columns in both tables and you perform two queries. – lefred Jan 17 '23 at 21:52

0 Answers0