購入したアイテムの数に基づいてすべての顧客を「ビンに入れ」、各ビンの数を表示しようとしています。何人 (account_id) が 1 つのアイテムを購入したか、2 つのアイテムを購入した人の数、9 つのアイテム、そして 10 以上のアイテムを購入したかを確認しようとしています。
これが私が使用しているクエリです - その価値のために、結果を生成するためにクエリが売上に対してフルテーブルスキャンを実行することを期待していますが、プロセス全体が永遠にかかります!
私は Oracle のバックグラウンドを持っており、Oracle の場合と同じようにクエリを作成しました。
select thecnt
, count(*)
from (select count(*)
, case when count(*) >= 10 then 'tenormore' else cast(count(*) as char) end thecnt
from sales
where created >= SUBDATE( CURRENT_DATE(), INTERVAL 60 DAY )
group by account_id) sub
group by thecnt
order by thecnt;
サブクエリを処理するときに mysql に落とし穴はありますか?
計画を説明する
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2143248 | 100.00 | Using temporary; Using filesort |
| 2 | DERIVED | sales | range | created | created | 8 | NULL | 2012492 | 100.00 | Using where; Using index; Using temporary; Using filesort |
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
2 rows in set, 1 warning (1 hour 4 min 6.14 sec)
mysql> describe sales;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| account_id | char(36) | NO | PRI | NULL | |
| created | datetime | NO | MUL | NULL | |
| histogram_value | bigint(20) unsigned | NO | PRI | NULL | |
+-----------------+---------------------+------+-----+---------+-------+