title: SQL Query optimization to avoid temporary table
body: <p><strong>The Table:</strong></p>
  
  <pre><code>CREATE TABLE `T1` (
    `UserId` int(10) unsigned NOT NULL,
    `FriendUserId` int(10) unsigned NOT NULL,
    `IsDisplayed` tinyint(1) unsigned NOT NULL,
    `Created` datetime NOT NULL,
    KEY `FriendUserId` (`FriendUserId`,`IsDisplayed`,`UserId`,`Created`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p><strong>The Query:</strong></p>
  
  <pre><code>SELECT `UserId`, `FriendUserId`, UNIX_TIMESTAMP(`Created`) AS `Created`
  FROM `T1` WHERE `FriendUserId` = 22
    AND `IsDisplayed` = 0
    GROUP BY `UserId`
    ORDER BY `Created`
  </code></pre>
  
  <p><strong>EXPLAIN result:</strong></p>
  
  <pre><code>           id: 1
    select_type: SIMPLE
          table: T1
           type: ref
  possible_keys: FriendUserId
            key: FriendUserId
        key_len: 5
            ref: const,const
           rows: 1
          Extra: Using where; Using index; Using temporary; Using filesort
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>How can I optimize it so that no temporary table is used?</p>