次のように実行するのに48秒かかるクエリがあります。
SELECT count(DISTINCT tmd_logins.userID) as totalLoginsUniqueLast30Days
FROM tmd_logins
join tmd_users on tmd_logins.userID = tmd_users.userID
where tmd_users.isPatient = 1 AND loggedIn > '2011-03-25'
and tmd_logins.userID in
(SELECT userID as accounts30Days FROM tmd_users
where isPatient = 1 AND created > '2012-04-29' AND computerID is null)
キーワードを削除するとDISTINCT
1秒もかからないので、ボトルネックはその中にあるようです。
tmd_logins
データベースは、ユーザーがシステムにログインするたびにテーブルにエントリを追加します。過去30日間など、特定の期間内に作成およびログインされた患者であるすべてのユーザーの総数を取得しようとしています。
DISTINCTキーワードを削除group by tmd_logins.userID
してステートメントに追加しようとしましたが、パフォーマンスの問題が残っています。
テーブルtmd_logins
には約300,000のレコードがtmd_users
あり、約40,000があります
これを行うためのより良い方法はありますか?