Mysql SQL查询/速度慢
我有下面的SQL代码,这是一个MySQL数据库。现在它给出了我期望的结果,但是查询速度很慢,我认为在继续之前应该加快查询速度 表agentstatusinformation包含: PKEY(主键)、userid(整数)、agentstate(整数) 表axpuser包含用户名:Mysql SQL查询/速度慢,mysql,sql,query-optimization,Mysql,Sql,Query Optimization,我有下面的SQL代码,这是一个MySQL数据库。现在它给出了我期望的结果,但是查询速度很慢,我认为在继续之前应该加快查询速度 表agentstatusinformation包含: PKEY(主键)、userid(整数)、agentstate(整数) 表axpuser包含用户名: PKEY(主键)不能确切地确定这是您想要的,但我认为它很接近: Select loginid, case when c.agentstate=1 Then 'Ready' wh
PKEY(主键)不能确切地确定这是您想要的,但我认为它很接近:
Select loginid, case when c.agentstate=1 Then 'Ready'
when c.agentstate=3 then 'Pause'
end state
from axpuser a
join (select userid, max(pkey) pkey
from agentstatusinformation
group by userid ) b
on a.userid=b.userid
join agentstatusinformation c
and b.pkey=c.pkey
这将消除初始SELECT子句中的subselect,并根据分组统计信息表进行联接。希望这能有所帮助。不能确切地确定这是您想要的,但我认为它很接近:
Select loginid, case when c.agentstate=1 Then 'Ready'
when c.agentstate=3 then 'Pause'
end state
from axpuser a
join (select userid, max(pkey) pkey
from agentstatusinformation
group by userid ) b
on a.userid=b.userid
join agentstatusinformation c
and b.pkey=c.pkey
这将消除初始SELECT子句中的subselect,并根据分组统计信息表进行联接。希望这有帮助。查询的问题在于嵌套选择。特别是,In子句中的子查询在MySQL中是有问题的。它被where子句过滤的每一行调用 以下内容修复了此问题:
select distinct (select loginid from axpuser where axpuser.pkey = age.userid),
case
when agentstate = 1 then 'Ready'
when agentstate = 3 then 'Pause'
end as state
from agentstatusinformation age
where exists (select userid, max(pkey)
from agentstatusinformation a2
where a2.userid = age.userid
group by userid
having age.pkey = max(pkey))
通过在AgentStatusInforomation(userid,pkey)上创建一个索引,可以加快运行速度
只要axpuser.pkey上有索引,嵌套的select就不会导致问题。但是,我认为最好在FROM子句中将其作为连接:
select distinct axpuser.loginid,
case
when agentstate = 1 then 'Ready'
when agentstate = 3 then 'Pause'
end as state
from agentstatusinformation age left outer join
axpuser
on axpuser.key = age.userid
where exists (select userid, max(pkey)
from agentstatusinformation a2
where a2.userid = age.userid
group by userid
having age.pkey = max(pkey)
)
查询的问题在于嵌套选择。特别是,In子句中的子查询在MySQL中是有问题的。它被where子句过滤的每一行调用 以下内容修复了此问题:
select distinct (select loginid from axpuser where axpuser.pkey = age.userid),
case
when agentstate = 1 then 'Ready'
when agentstate = 3 then 'Pause'
end as state
from agentstatusinformation age
where exists (select userid, max(pkey)
from agentstatusinformation a2
where a2.userid = age.userid
group by userid
having age.pkey = max(pkey))
通过在AgentStatusInforomation(userid,pkey)上创建一个索引,可以加快运行速度
只要axpuser.pkey上有索引,嵌套的select就不会导致问题。但是,我认为最好在FROM子句中将其作为连接:
select distinct axpuser.loginid,
case
when agentstate = 1 then 'Ready'
when agentstate = 3 then 'Pause'
end as state
from agentstatusinformation age left outer join
axpuser
on axpuser.key = age.userid
where exists (select userid, max(pkey)
from agentstatusinformation a2
where a2.userid = age.userid
group by userid
having age.pkey = max(pkey)
)
你到底想从这个查询中得到什么?apxuser中列出的所有用户的状态?使用
EXPLAIN
编辑您的问题一个代理状态信息可以有多个axpuser,还是一个?尝试单独运行子查询,看看它们有多快/多慢是的,基本上我相信我的查询没有优化,我想知道如何优化它。agentstatusinformation有多条链接axpuser的记录,其中只有一条记录。在MySQL中有没有计算成本的方法?你到底想从这个查询中得到什么?apxuser中列出的所有用户的状态?使用EXPLAIN
编辑您的问题一个代理状态信息可以有多个axpuser,还是一个?尝试单独运行子查询,看看它们有多快/多慢是的,基本上我相信我的查询没有优化,我想知道如何优化它。agentstatusinformation有多条链接axpuser的记录,其中只有一条记录。MySQL中有没有计算成本的方法?