Is it important to use base table in query? Is it important to use base table for entity in query? For example if I want to get all user's id of specific role, should I do:

  $query = db_select('users', 'u');
  $query-&gt;distinct();
  $query-&gt;fields('u', array('uid'));
  $query-&gt;innerJoin('users_roles', 'r', 'u.uid = r.uid');
  $query-&gt;condition('r.rid', 3);


or

  $query = db_select('users_roles', 'r');
  $query-&gt;fields('r', array('uid'));
  $query-&gt;condition('r.rid', 3);
No, it's not important, nor necessary.

There's no API method for getting this information, so going directly into the database is fair game. Once you're going directly into the database, you only need to include the tables and fields that your data requires - anything else is a waste of resources.

In this case, you don't need to select or filter anything from the users table, so there's no need to join it in (unless you're worried about referential integrity, and want to make sure the user record actually exists).