Which method is more efficient, database query or taxonomy term controller? I tried to find a method to verify if the current taxonomy item page has a parent or children, now I get two but I don't know which one is better for the efficiency.

approach I: use taxonomy item controller

$tid = \Drupal::routeMatch()-&gt;getRawParameters('taxonomy_term');
$term = taxonomy_term_load($tid);
$items = \Drupal::entityTypeManager()-&gt;getStorage('taxonomy_term')-&gt;loadChildren($tid,$term-&gt;getVocabularyId());


approach II: database query

$db = \Drupal::database();
$query = $db-&gt;select('taxonomy_term__parent','ttp')-&gt;condition('ttp.parent_target_id',$tid)-&gt;fields('ttp',['entity_id']);
$result = $query-&gt;execute()-&gt;fetchAll();


Both method can satisfy my initial demand, but which one is better in
efficiency? And what about the general case, when we need to check
some data, using the data object method or database query, which is
better?
Do you need to load all of the children? Otherwise, loadChildren() will be performing the same query but also loading the full term -- which is a performance dent. The API on the entity storage won't make much of a difference here, honestly.

Have you looked at https://www.drupal.org/project/taxonomy_term_depth? This would be more performant. It caches the depth level in a new base field. If a term's depth is greater than one, you know it is a child.

I would even borrow the concepts from taxonomy_term_depth to add a new base field that is a boolean which caches if the term has children or not. That way you only need to work with the current term entity.