create table User( Id int not null PRIMARY key auto_increment , NickName varchar(50) comment "用户昵称", Sex int comment "性别", Sign varchar(50) comment "用户签名", Birthday datetime comment "用户生日", CreateTime datetime comment "创建时间") default charset=utf8 comment "用户表";create table UserGroup( Id int not null PRIMARY key auto_increment , UserId int not null comment "user Id", GroupId int not null comment "用户组Id", CreateTime datetime comment "创建时间", -- key index_groupid(GroupId) using btree, key index_userid(groupid, UserId) using btree) default charset=utf8 comment "用户组表";2. 准备数据
var conStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();using (IDbConnection conn = new MySqlConnection(conStr)){ Stopwatch watch = new Stopwatch(); var sql = string.Empty; var names = new string[] { "非", "想", "红", "帝", "德", "看", "梅", "插", "兔" }; Random ran = new Random();var insertSql = @" insert into User(NickName,Sex,Sign, Birthday, CreateTime) values(@NickName,@Sex,@Sign, @Birthday, @CreateTime);INSERT INTO usergroup (UserId, GroupId, CreateTime ) VALUES (LAST_INSERT_ID() , @GroupId, @CreateTime);"; watch.Start(); if (conn.State == ConnectionState.Closed) {conn.Open(); } var tran = conn.BeginTransaction(); for (int i = 0; i < 100000; i++) {var param = new { NickName = names[ran.Next(9)] + names[ran.Next(9)] + i, Sign = names[ran.Next(9)] + names[ran.Next(9)], CreateTime = DateTime.Now, Birthday = DateTime.Now.AddYears(ran.Next(10, 30)), Sex = i % 2, GroupId = ran.Next(1, 100) };conn.Execute(insertSql, param, tran); } tran.Commit(); conn.Dispose(); watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds);}这里我插入了5000条数据, group分了99个组, 随机的.
explainselect user.id, user.nickname from usergroup left join user on usergroup.UserId = user.Idwhere usergroup.groupid = 1 order by usergroup.UserId desclimit 100, 20; explainselect user.id, user.nicknamefrom (select id, userid from usergroup where groupid = 1 order by userid limit 100, 20) tleft join user on t.UserId = user.id ; explainselect user.id, user.nicknamefrom (select id, userid from usergroup where groupid = 1 order by userid ) tleft join user on t.UserId = user.id limit 100, 20;第二句和第三句都使用到了子查询, 不同之处再与, 第二句是先得到20条数据, 然后以此来与user表关联的
再看第二句
第三句
从上面三幅图看, 好像能看出点什么了.
首先看他们的 rows, 第二句最多, 加起来有1000多了, 另两句加起来都是996. 但是我想说的是, 这里并不是看rows的和是多少. 正确的方式是, 从id大的语句开始看, id相同的语句, 从上到下依次执行.
那先看第二句的id=2的语句和第一句的id=1的语句, 一模一样的. 他们都是从usergroup表中筛选数据, 并且能得到相同的结果集A.
看来他们都是基于相同的结果集去进行操作, 接下来就有区别了.
先看第一句, 再结果集A的基础上, 去左连接表user, 并筛选出最后的数据, 返回给客户端.
那第二句呢, 是在A的基础上, 再次筛选数据, 得到需要的数据, 然后拿这些数据, 去与user表左连接, 得到最终结果.
从上面来看, 执行计划中, 第二种执行计划, 更加高效.
如果能够通过子查询, 大幅度缩小查询范围, 可以考虑使用子查询语句.
以上所述是小编给大家介绍的Mysql数据库性能优化之子查询,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!