Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mysql> desc users;
- +-------+--------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+--------------+------+-----+---------+-------+
- | id | int(11) | NO | | 0 | |
- | name | varchar(200) | YES | | NULL | |
- +-------+--------------+------+-----+---------+-------+
- 2 rows in set (0.00 sec)
- mysql> desc accounts;
- +---------+---------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +---------+---------+------+-----+---------+-------+
- | id | int(11) | NO | | 0 | |
- | user_id | int(11) | YES | | NULL | |
- | amount | int(11) | YES | | NULL | |
- +---------+---------+------+-----+---------+-------+
- 3 rows in set (0.00 sec)
- mysql> explain select u.name from users as u join accounts as a on u.id = a.user_id where a.amount > 1000;
- +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
- | 1 | SIMPLE | u | ALL | NULL | NULL | NULL | NULL | 10 | |
- | 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 10 | Using where; Using join buffer |
- +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
- 2 rows in set (0.00 sec)
- mysql> alter table users add primary key (id);
- Query OK, 0 rows affected (0.26 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> alter table accounts add primary key (id);
- Query OK, 0 rows affected (0.25 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> alter table accounts add key (user_id);
- Query OK, 0 rows affected (0.21 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> explain select u.name from users as u join accounts as a on u.id = a.user_id where a.amount > 1000;
- +----+-------------+-------+------+---------------+---------+---------+-----------+------+-------------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+-------+------+---------------+---------+---------+-----------+------+-------------+
- | 1 | SIMPLE | u | ALL | PRIMARY | NULL | NULL | NULL | 10 | |
- | 1 | SIMPLE | a | ref | user_id | user_id | 5 | test.u.id | 1 | Using where |
- +----+-------------+-------+------+---------------+---------+---------+-----------+------+-------------+
- 2 rows in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment