insert_select性能问题

4 views
Skip to first unread message

仲 项

unread,
Jul 20, 2011, 5:56:09 AM7/20/11
to MySQLer
使用的测试数据库是mysql官网的:employees。
想用memory引擎的表,来保存一个查询子集,如下:(salaries总共有2800000条,salary没有索引)
create table t4(id int,index using hash(id))engine=memory;
insert into t4(id) select emp_no from salaries where salary=90930;
执行insert操作,发现不管后面的子查询结果集为多大,整个insert 操作都要用4sec多。
当我单独把后面的select emp_no from salaries where salary=90930,拿来执行发现所用的时间
为2sec左右。那么这个就否可以说明前面的insert 操作用了另外2sec,但是这个insert其实可能只
insert 10几条记录。这个是为什么呢?查询是比较慢,但insert应该不至于。
所以我又测试了另外一种情况:(300000条,emp_no是索引)
insert into t4(id) select emp_no from employees;
该过程总共才用了1sec左右。
这是为什么?哪位同学帮忙解释一下。谢谢!

peter yue

unread,
Jul 25, 2011, 3:21:41 AM7/25/11
to mys...@googlegroups.com
这就是索引的用途,对查询结果字段进行索引可以大幅提高速度,特别是没有主键的表结构。

2011/7/20 仲 项 <xiangzh...@gmail.com>

--
MySQLer 邮件组
要发布讨论请发 Mail 至 邮件组邮件地址:mys...@googlegroups.com
要退订次邮件组请发 Email 至 mysqler+u...@googlegroups.com
要了解更多的信息,请访问:http://groups.google.com/group/mysqler?hl=en



--
您好,这是岳国华的邮件,感谢查看!

d

unread,
Jul 25, 2011, 4:52:37 AM7/25/11
to mys...@googlegroups.com
和锁表应该也有关系
insert into t4(id) select emp_no from salaries where salary=90930;
会把salaries 表锁住
select emp_no from salaries where salary=90930;
不会锁表。
要退订次邮件组请发 Email 至 mysqler+unsub...@googlegroups.com
要了解更多的信息,请访问:http://groups.google.com/group/mysqler?hl=en


Reply all
Reply to author
Forward
0 new messages