DROP TABLE IF EXISTS `biz`; CREATE TABLE `biz` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `description` varchar(4000) DEFAULT NULL, `created_ts` bigint(20) NOT NULL, `type` bigint(20) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`), KEY `biz_ct_idx` (`created_ts`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `biz_groups`; CREATE TABLE `biz_groups` ( `biz_id` bigint(20) NOT NULL, `group_uuid` varchar(255) NOT NULL, KEY `bi_idx` (`biz_id`), KEY `gubi_idx` (`group_uuid`,`biz_id`), CONSTRAINT `biz_groups_bi_fk` FOREIGN KEY (`biz_id`) REFERENCES `biz` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
select SQL_NO_CACHE distinct b.id
from biz b
inner join biz_groups g on g.biz_id = b.id
where g.group_uuid in ('group-uuid-01','group-uuid-03','group-uuid-05', 'group-uuid-07')
order by b.created_ts desc
limit 20 offset 20;
20 rows in set (1.30 sec)
+----+-------------+-------+--------+-----------------+----------+---------+-------------------+-------+----------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+--------+-----------------+----------+---------+-------------------+-------+----------+-----------------------------------------------------------+
| 1 | SIMPLE | g | range | bi_idx,gubi_idx | gubi_idx | 767 | NULL | 56074 | 100.00 | Using where; Using index; Using temporary; Using filesort |
| 1 | SIMPLE | b | eq_ref | PRIMARY | PRIMARY | 8 | appiandb.g.biz_id | 1 | 100.00 | |
+----+-------------+-------+--------+-----------------+----------+---------+-------------------+-------+----------+-----------------------------------------------------------+
select SQL_NO_CACHE b.id
from biz b
where b.id in (
select SQL_NO_CACHE g.biz_id
from biz_groups g
where g.group_uuid in ('group-uuid-01','group-uuid-03','group-uuid-05','group-uuid-07')
)
order by b.created_ts desc
limit 20 offset 20;
20 rows in set (0.02 sec)
+----+--------------------+-------+----------------+-----------------+------------+---------+------+------+-----------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+-------+----------------+-----------------+------------+---------+------+------+-----------+--------------------------+
| 1 | PRIMARY | b | index | NULL | biz_ct_idx | 8 | NULL | 40 | 496027.50 | Using where; Using index |
| 2 | DEPENDENT SUBQUERY | g | index_subquery | bi_idx,gubi_idx | bi_idx | 8 | func | 1 | 100.00 | Using where |
+----+--------------------+-------+----------------+-----------------+------------+---------+------+------+-----------+--------------------------+
| Subject | Views | Written By | Posted |
|---|---|---|---|
| Queries using JOIN and ORDER BY LIMIT | 1213 | Nikita Dubrovsky | 03/10/2012 01:11PM |
| Re: Queries using JOIN and ORDER BY LIMIT | 513 | irek kordirko | 03/12/2012 06:18PM |
| Re: Queries using JOIN and ORDER BY LIMIT | 549 | Rick James | 03/13/2012 11:07PM |
| Re: Queries using JOIN and ORDER BY LIMIT | 496 | Nikita Dubrovsky | 03/14/2012 08:45AM |
| Re: Queries using JOIN and ORDER BY LIMIT | 637 | Rick James | 03/15/2012 10:50AM |
Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.