付费解决mysql sum 多字段统计慢的问题
最近遇见一个sum 统计特别慢的问题。
大概有13个字段需要sum 和一个count
数据五六万就要去到5秒
微ID amu-ajin
欢迎大佬联系我
佣金:500起
CREATE TABLE "order_orders" (
"id" bigint unsigned NOT NULL AUTO_INCREMENT,
"oid" char(20) COLLATE utf8mb4_unicode_ci NOT NULL,
"user_id" bigint unsigned NOT NULL,
"username" varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
"nickname" varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
"quantity" bigint unsigned NOT NULL DEFAULT '1',
"amount" decimal(20,4) NOT NULL,
"consumption" decimal(20,4) NOT NULL DEFAULT '0.0000',
"pay_amount" decimal(20,4) NOT NULL DEFAULT '0.0000',
"discount" decimal(20,4) NOT NULL DEFAULT '0.0000',
"order_at_day" date NOT NULL,
"created_at" timestamp NULL DEFAULT NULL,
"updated_at" timestamp NULL DEFAULT NULL,
PRIMARY KEY ("id"),
KEY "order_orders_oid_index" ("oid"),
KEY "order_orders_user_id_index" ("user_id"),
KEY "order_orders_order_at_day_index" ("order_at_day")
);
DB::table('order_orders')
->select(
[
'username',
'nickname',
DB::raw('count(quantity) as quantity'),
DB::raw('sum(amount) as amount'),
DB::raw('sum(consumption) as consumption'),
DB::raw('sum(pay_amount) as pay_amount'),
DB::raw('sum(discount) as discount'),
]
)
->whereIn('user_id', [1, 2, 3, 5, 8, 11])
->whereBetween('order_at_day', $this->request->dateRange)
->groupBy(
'username',
'nickname',
)
->cursor();
我是讨论
MySQL
的数据五六万,是指
order_orders
表的数量嘛?还是user_id IN (1, 2, 3, 5, 8, 11)
的数量。。order_orders_user_id_index
索引改成(user_id, order_at_day)
应该能提速?(这个order_at_day
在哪儿。。)GROUP BY user_id
呢?explain 分析,贴出下索引的命中情况。
把cursor改成get试试多久🌚
用云服务么,可以用云服务来解这种题,另外OLTP、OLAP混用了么,分析类建议走OLAP就很好解了,就是稍微花点钱,不用那么折腾。
group by治标不治本,修改代码吧!新增一张表按维度聚合。
为啥我的expain结果跟你的不一样
做个缓存,定时每分钟更新一下就行了,统计数据没必要实时,或者同步个列存储库,统计数据从列存储走,也算是近实时
建多一个表来存统计数据。定时同步过来
先in 查出来在自己foreach 处理数据试试呢?