0

以下の SQL コードがあります。これは MySQL データベースからのものです。これで期待どおりの結果が得られましたが、クエリが遅いため、先に進む前にこのクエリを高速化する必要があると思います。

表 agentstatusinformation には次の情報があります。

PKEY (主キー)、userid (整数)、agentstate (整数)

テーブル axpuser にはユーザー名が含まれています。

PKEY (主キー) <-- これはユーザー ID、ログイン ID (ユーザー名) のキーです。

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  (userid, pkey) in 
 (select userid, max(pkey) from agentstatusinformation group by userid)

これは改善できると確信していますが、木を見て木を見ることはできません。

どうもありがとう。

4

3 に答える 3

2

これがあなたが望むものであるかどうかは正確にはわかりませんが、近いと思います:

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 句の副選択が削除され、グループ化された統計情報テーブルに対して結合されます。お役に立てれば。

于 2012-09-13T15:08:29.017 に答える
1

クエリの問題は、ネストされた選択です。特に、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))

agenstatusinfromation(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)
             )
于 2012-09-13T15:46:23.010 に答える
0
 select ax.loginid,
        case
        when age.agentstate = 1 then 'Ready'
        when age.agentstate = 3 then 'Pause'
        end as state 
from 
 agentstatusinformation age
 join 
 axpuser ax
 on age.userid = ax.userid and age.pkey=(select max(pkey) from agentstatusinformation group by userid)
于 2012-09-13T15:05:08.580 に答える