select ChannelID,count(*) as num from MemberChannels where ChannelID in(
select distinct ChannelID from GenderChannels where isfixed=0)
group by ChannelID这条语句居然执行了2分20秒!!拆开执行都是瞬间完成.
select distinct ChannelID from GenderChannels where isfixed=0 --返回14条select ChannelID,count(*) as num from MemberChannels
group by ChannelIDMemberChannels 表两万多条记录,ChannelID 上没有索引.但是同样的表结构、数据和SQL,在SQLServer上瞬间就完成了。MySQL在没有索引的情况下,用where in 子查询语句怎么会这么慢? 这个子查询的结果集只有14条而已。
select distinct ChannelID from GenderChannels where isfixed=0)
group by ChannelID这条语句居然执行了2分20秒!!拆开执行都是瞬间完成.
select distinct ChannelID from GenderChannels where isfixed=0 --返回14条select ChannelID,count(*) as num from MemberChannels
group by ChannelIDMemberChannels 表两万多条记录,ChannelID 上没有索引.但是同样的表结构、数据和SQL,在SQLServer上瞬间就完成了。MySQL在没有索引的情况下,用where in 子查询语句怎么会这么慢? 这个子查询的结果集只有14条而已。
解决方案 »
- mysql 存储过程怎么删不了呢?
- MySQL WorkBench的安装问题
- 组合查询的排序问题!向高手求助呀!还有按长度排序!
- MySql 插入数据 帮忙看一下方法有什么问题
- MySQL-Front2.5 能连接mysql5.0吗?
- 请教本人想用java+mysql在linux上作点东西,各位给推荐推荐mysql的书,谢谢!!!
- 为什么我的 mysql总是提示Can't connect to local MySQLserver through socket' /tmp/mysql.sock'
- 怎样把图片或者是视频类的文件存入数据库中?
- MySQL的一个小问题
- 如何把mysql中某个字段中的多个不同数一次性替换为一个数
- 如何使表锁定同时又能删除数据?
- 层级数据表树型列表展示。急急急!
ChannelID 上面建个索引。
select ChannelID,count(*) as num from MemberChannels where ChannelID in(
select distinct ChannelID from GenderChannels where isfixed=0)
group by ChannelID;
inner join (
select distinct ChannelID from GenderChannels where isfixed=0) b
on a.ChannelID= b.ChannelID
group by ChannelID改成 inner jion 瞬间就完成了。
select ChannelID,count(*) as num from MemberChannels where ChannelID in(
80,90,100,10,120,1,2,3,4,5,6,7,8,9)
group by ChannelID
瞬间完成了,感觉是mysql有问题。。
insert Temp select distinct ChannelID from GenderChannels where isfixed=0 ;
select ChannelID,count(*) as num from MemberChannels where ChannelID in(select ChannelID from Temp)
group by ChannelID;
drop TEMPORARY table Temp;
select distinct ChannelID from GenderChannels where isfixed=0)
group by ChannelID因为红色这部分对每条记录都会执行一次,虽然有缓存,但显然速度会慢。改成JOIN。另外DISTINCT会更慢。
inner join (
select distinct ChannelID from GenderChannels where isfixed=0) b
on a.ChannelID= b.ChannelID
group by ChannelID
你要是MemberChannels表上ChannelID没有索引的话,咋可能瞬间完成