解决方案 »
- mysql 如何修复INNODB的表
- 怎么把year(curdate())转换成字符串
- Parameter 'table_name' not found in the collection. C#中调用MySQL存储过程出错
- 关于MYsql proxy read_query 注入sql的问题
- C# 读取一条mysql里的信息
- sum之后结果是所需要的二倍?
- PostgreSQL中执行查询的问题
- red hat 8.0下安装mysql出问题了。。。
- 如果数据库名称中含有小数点,运行存储过程会提示找不到
- mysql并发问题
- MySql 远程连接失败,求解答
- MYSQL 存储过程 分割字符串,游标遍历每个元素
explain select
user_id,
count(*)as num,
sum(case when status=2 then 1 else 0 end),
sum(dup)
from upload
where upload_type = 'ct' and begin_date >= '2013-09-12'
and begin_date <'2013-09-13'
group by user_id
order by num desc
limit 50;
show index from upload;以供分析。
begin_date必须有索引。
begin_date也有索引的,忘了写。
+----+-------------+---------------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+-------+---------------+---------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | mps_uploading | index | begin_date | user_id | 4 | NULL | 9670 | Using where; Using temporary; Using filesort |
+----+-------------+---------------+-------+---------------+---------+---------+------+------+----------------------------------------------+
谢谢大神~
+---------------+------------+------------+--------------+-------------+-----------+-------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality |
+---------------+------------+------------+--------------+-------------+-----------+-------------+
| mps_uploading | 0 | PRIMARY | 1 | id | A | 3176792 |
| mps_uploading | 1 | user_id | 1 | user_id | A | 529465 |
| mps_uploading | 1 | begin_date | 1 | begin_date | A | 3176792 |
| mps_uploading | 1 | status | 1 | status | A | 17 |
user_id,
count(*)as num,
sum(case when status=2 then 1 else 0 end),
sum(dup)
from upload
where
user_id in(
select user_id
from upload
where
upload_type = 'ct'
and begin_date >= '2013-09-12'
and begin_date <'2013-09-13'
)
group by user_id
order by num desc
limit 50;
如果这个提高速度的话,再建立一个联合索引(begin_date ,upload_type, user_id),这样里边的查询可以全部走索引,而不用再去根据索引查了。
explainselect
user_id,
count(*)as num,
sum(case when status=2 then 1 else 0 end),
sum(dup)
from upload
where
user_id in(
select user_id
from upload
where
upload_type = 'ct'
and begin_date >= '2013-09-12'
and begin_date <'2013-09-13'
)
group by user_id
order by num desc
limit 50;
– Monitor Sort_merge_passes < 1-2 an hour optimal
– Usually a problem in a reporting or data warehouse database大意是:
sort_buffer_size这个参数很重要,用于order by,group by,distinct,union。
监控Sort_merge_passes参数(show status like 'Sort_merge_passes';),比较好的状态:每个小时有2个。今天在官方文档上看到这个,可能你用得着,你可能要把sort_buffer_size调大一些!!!
+----+--------------------+---------------+----------------+--------------------+---------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------------+----------------+--------------------+---------+---------+------+------+----------------------------------------------+
| 1 | PRIMARY | mps_uploading | index | NULL | user_id | 4 | NULL | 250 | Using where; Using temporary; Using filesort |
| 2 | DEPENDENT SUBQUERY | mps_uploading | index_subquery | user_id,begin_date | user_id | 4 | func | 5 | Using where |
+----+--------------------+---------------+----------------+--------------------+---------+---------+------+------+----------------------------------------------+