XF 2.1 Multiple $finder->fetch vs raw query

abdfahim

Well-known member
Hi,

Is there any performance difference between the following two?

Objective: Get all records of the latest date

PHP:
$db = \XF::db();
$results = $db->fetchAll("SELECT * FROM my_table WHERE my_date IN (SELECT max(my_date) FROM my_table)");

PHP:
$max_date = $finder->order('my_date', 'DESC')->limit(1)->pluckFrom('my_date')->fetch();
$results = $finder->where('my_date', $max_date)->fetch();
 

Lukas W.

Well-known member
Yes, you're performing one database query versus two. The performance difference is negligible, but does exist. The bigger difference however is, that one will give you an array, where the other one will give you an ArrayCollection with entities. If you want to condense it into one query and have an ArrayCollection, you can use the following (untested) snippet:

Code:
\XF::finder('My:Finder')->whereSql('my_date = (SELECT MAX(my_date) FROM my_table')->fetch();
 
Top