22m行のテーブルからユーザーのサブスクライバーの数を取得しようとしています。
私のSQLは次のとおりです。
SELECT
COUNT(id)
FROM
subscribers
WHERE
suid=541839243781
ロードするのに12.6020秒必要です
ただし、次の同じクエリ(ユーザーのサブスクリプションを取得)は、ロードに0.0036秒しか必要ありません(問題ないようです)
SELECT
COUNT(uid)
FROM
subscribers
WHERE
uid=541839243781
私の説明:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE dvx_subscribers index 4 4 16 NULL 22041275 Using where; Using index
作成テーブルを表示:
CREATE TABLE `subscribers` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`sid` bigint(20) unsigned NOT NULL,
`uid` bigint(20) unsigned NOT NULL,
`suid` bigint(20) unsigned NOT NULL,
`date` datetime NOT NULL,
KEY `id` (`id`),
KEY `2` (`uid`,`suid`),
KEY `4` (`suid`,`id`)
) ENGINE=MyISAM AUTO_INCREMENT=23226599 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
インデックス:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment
subscribers 1 id 1 id A NULL NULL NULL BTREE
subscribers 1 2 1 uid A 449821 NULL NULL BTREE
subscribers 1 2 2 suid A 459193 NULL NULL BTREE
subscribers 1 4 1 suid A 6115 NULL NULL BTREE
subscribers 1 4 2 id A 22041275 NULL NULL BTREE
インデックスを作成したり、できるだけ速くロードするように最適化するにはどうすればよいですか?原因12秒はこれには地獄です...