Guest User

Untitled

a guest
Jul 2nd, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. mysql> desc users;
  2. +-------+--------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------+--------------+------+-----+---------+-------+
  5. | id | int(11) | NO | | 0 | |
  6. | name | varchar(200) | YES | | NULL | |
  7. +-------+--------------+------+-----+---------+-------+
  8. 2 rows in set (0.00 sec)
  9.  
  10. mysql> desc accounts;
  11. +---------+---------+------+-----+---------+-------+
  12. | Field | Type | Null | Key | Default | Extra |
  13. +---------+---------+------+-----+---------+-------+
  14. | id | int(11) | NO | | 0 | |
  15. | user_id | int(11) | YES | | NULL | |
  16. | amount | int(11) | YES | | NULL | |
  17. +---------+---------+------+-----+---------+-------+
  18. 3 rows in set (0.00 sec)
  19.  
  20. mysql> explain select u.name from users as u join accounts as a on u.id = a.user_id where a.amount > 1000;
  21. +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
  22. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  23. +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
  24. | 1 | SIMPLE | u | ALL | NULL | NULL | NULL | NULL | 10 | |
  25. | 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 10 | Using where; Using join buffer |
  26. +----+-------------+-------+------+---------------+------+---------+------+------+--------------------------------+
  27. 2 rows in set (0.00 sec)
  28.  
  29. mysql> alter table users add primary key (id);
  30. Query OK, 0 rows affected (0.26 sec)
  31. Records: 0 Duplicates: 0 Warnings: 0
  32.  
  33. mysql> alter table accounts add primary key (id);
  34. Query OK, 0 rows affected (0.25 sec)
  35. Records: 0 Duplicates: 0 Warnings: 0
  36.  
  37. mysql> alter table accounts add key (user_id);
  38. Query OK, 0 rows affected (0.21 sec)
  39. Records: 0 Duplicates: 0 Warnings: 0
  40.  
  41. mysql> explain select u.name from users as u join accounts as a on u.id = a.user_id where a.amount > 1000;
  42. +----+-------------+-------+------+---------------+---------+---------+-----------+------+-------------+
  43. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  44. +----+-------------+-------+------+---------------+---------+---------+-----------+------+-------------+
  45. | 1 | SIMPLE | u | ALL | PRIMARY | NULL | NULL | NULL | 10 | |
  46. | 1 | SIMPLE | a | ref | user_id | user_id | 5 | test.u.id | 1 | Using where |
  47. +----+-------------+-------+------+---------------+---------+---------+-----------+------+-------------+
  48. 2 rows in set (0.00 sec)
Advertisement
Add Comment
Please, Sign In to add comment