Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Optimize a Query With GROUP BY and ORDER BY
    primarykey
    data
    text
    <p>I have got a <code>POSTS</code> table, the structure is like this:</p> <pre><code>CREATE TABLE IF NOT EXISTS `posts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8_turkish_ci DEFAULT NULL, `content` longtext COLLATE utf8_turkish_ci, `excerpt` longtext COLLATE utf8_turkish_ci, `link` longtext COLLATE utf8_turkish_ci, `original_link` longtext COLLATE utf8_turkish_ci, `mime_type` longtext COLLATE utf8_turkish_ci, `language_id` int(11) DEFAULT NULL, `user_id` int(11) DEFAULT NULL, `site_id` int(11) DEFAULT NULL, `type` varchar(255) COLLATE utf8_turkish_ci DEFAULT NULL, `created_at` datetime DEFAULT NULL, `modified_at` datetime DEFAULT NULL, `is_deleted` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `user_id` (`user_id`), KEY `type` (`type`), KEY `created_at` (`created_at`), ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=52487 ; </code></pre> <p>And a USERS table, structed like this:</p> <pre><code>CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) COLLATE utf8_turkish_ci NOT NULL, PRIMARY KEY (`id`), KEY `username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci AUTO_INCREMENT=9422 ; </code></pre> <p>I'm using this query to get latest "page, file or post" posts ordered by descending time and grouping by user to not show all latest posts from a user:</p> <pre><code> SELECT p.*, u.* FROM posts p LEFT JOIN users u ON p.user_id = u.id WHERE p.type IN ('post', 'page', 'file') GROUP BY p.user_id ORDER BY p.created_at DESC LIMIT 30 </code></pre> <p>But it is too slow, even limited to 30 records.</p> <p>now, how can i speed up this query? which columns to index or any other ideas? thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload