How do I get the number of rows selected from the query?
Use $query->countQuery()->execute()->fetchField(); This is fast.

See detail example.

$number = $this-&gt;connection-&gt;select(static::TABLE_NAME, 'f')
    -&gt;condition('event', $name)
    -&gt;condition('identifier', $identifier)
    -&gt;condition('timestamp', REQUEST_TIME - $window, '&gt;')
    -&gt;countQuery()
    -&gt;execute()
    -&gt;fetchField();