Chinaunix首页 | 论坛 | 博客
  • 博客访问: 492110
  • 博文数量: 99
  • 博客积分: 3621
  • 博客等级: 中校
  • 技术积分: 1089
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-22 16:29
文章存档

2012年(21)

2011年(28)

2010年(50)

分类: Mysql/postgreSQL

2012-04-12 11:16:56

在memory引擎中,只支持hash index,hash index 对于等值查询效率是非常高的,但是对范围查询无能无力,从它的实现原理就能得到,每个值经过hash算法之后,得到一个hash值,通过比较hash值得到具体的数据,很明显hash值是没有任何规律的。
 

点击(此处)折叠或打开

  1. mysql> create table tb(id int) engine=memory;
  2. Query OK, 0 rows affected (0.02 sec)

  3. mysql> insert into tb values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
  4. Query OK, 10 rows affected (0.00 sec)
  5. Records: 10 Duplicates: 0 Warnings: 0

  6. mysql> create index idx_id on tb(id);
  7. Query OK, 10 rows affected (0.01 sec)
  8. Records: 10 Duplicates: 0 Warnings: 0

  9. mysql> explain select * from tb where id=1;
  10. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  11. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  12. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  13. | 1 | SIMPLE | tb | ref | idx_id | idx_id | 5 | const | 2 | Using where |
  14. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  15. 1 row in set (0.00 sec)
  16. #等值查询,用到了索引
  17. mysql> explain select * from tb where id=5;
  18. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  19. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  20. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  21. | 1 | SIMPLE | tb | ref | idx_id | idx_id | 5 | const | 2 | Using where |
  22. +----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
  23. 1 row in set (0.00 sec)

  24. mysql> explain select * from tb where id>5;
  25. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
  26. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  27. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
  28. | 1 | SIMPLE | tb | ALL | idx_id | NULL | NULL | NULL | 10 | Using where |
  29. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
  30. 1 row in set (0.00 sec)
  31. #范围查询,全表扫描了
  32. mysql> explain select * from tb where id<2;
  33. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
  34. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  35. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
  36. | 1 | SIMPLE | tb | ALL | idx_id | NULL | NULL | NULL | 10 | Using where |
  37. +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
  38. 1 row in set (0.00 sec)

  39. mysql> show create table tb;
  40. +-------+---------------------------------------------------------------------------------------------------------------+
  41. | Table | Create Table |
  42. +-------+---------------------------------------------------------------------------------------------------------------+
  43. | tb | CREATE TABLE `tb` (
  44.   `id` int(11) DEFAULT NULL,
  45.   KEY `idx_id` (`id`)
  46. ) ENGINE=MEMORY DEFAULT CHARSET=latin1 |
  47. +-------+---------------------------------------------------------------------------------------------------------------+
  48. 1 row in set (0.00 sec)

mysql> show index from tb;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tb    |          1 | idx_id   |            1 | id          | NULL      |           5 |     NULL | NULL   | YES  | HASH       |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

 


阅读(1122) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

9094133352012-04-12 14:22:17

mysql> create index idx_id_1 on tb(id) using BTREE
    -> ;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    7
Current database: test

Query OK, 10 rows affected (0.02 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> show index from tb;
+-------+------------+----------+--------------+-------------+-----------+---