MySQL一次大量内存消耗的跟踪

Posted GreatSQL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL一次大量内存消耗的跟踪相关的知识,希望对你有一定的参考价值。

  • GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。
  • GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。
  • 文章来源:GreatSQL社区原创

线上使用MySQL8.0.25的数据库,通过监控发现数据库在查询一个视图(80张表的union all)时内存和cpu均明显上升。

在8.0.25 MySQL Community Server官方版本测试发现:只能在视图上进行数据过滤,不能将视图上的过滤条件下推到视图内的表上进行数据过滤。8.0.29以后的版本已解决该问题。

MySQL视图访问原理

下面是在8.0.25 MySQL Community Server上做的测试

使用sysbench 构造4张1000000的表

 mysql> select count(*) from sbtest1;

+----------+
| count(*) |
+----------+
|  1000000 |
+----------+

1 row in set (1.44 sec)
mysql> show create table sbtest1;

| Table   | Create Table  | sbtest1 | 
CREATE TABLE `sbtest1` (

  `id` int NOT NULL AUTO_INCREMENT,

  `k` int NOT NULL DEFAULT \'0\',

  `c` char(120) COLLATE utf8mb4_0900_bin NOT NULL DEFAULT \'\',

  `pad` char(60) COLLATE utf8mb4_0900_bin NOT NULL DEFAULT \'\',

  PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2000000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin |

+---------+-----------------------------------------------------------------------------------
1 row in set (0.00 sec)

手工收集表统计信息

mysql> analyze table sbtest1,sbtest2 ,sbtest3,sbtest4;

+----------------+---------+----------+----------+
| Table          | Op      | Msg_type | Msg_text |
+----------------+---------+----------+----------+
| sbtest.sbtest1 | analyze | status   | OK       |
| sbtest.sbtest2 | analyze | status   | OK       |
| sbtest.sbtest3 | analyze | status   | OK       |
| sbtest.sbtest4 | analyze | status   | OK       |
+----------------+---------+----------+----------+

4 rows in set (0.17 sec)

创建视图

drop view view_sbtest1 ;

Create view view_sbtest1  as 

select * from sbtest1 
union all 
select * from sbtest2 
union all 
select * from sbtest3 
union all 
select * from sbtest4;

查询视图

Select * from view_sbtest1 where id=1;

 mysql> Select id ,k,left(c,20) from view_sbtest1 where id=1;
+----+--------+----------------------+
| id | k      | left(c,20)           |
+----+--------+----------------------+
|  1 | 434041 | 61753673565-14739672 |
|  1 | 501130 | 64733237507-56788752 |
|  1 | 501462 | 68487932199-96439406 |
|  1 | 503019 | 18034632456-32298647 |
+----+--------+----------------------+
4 rows in set (1 min 8.96 sec)

通过主键查询数据, 查询返回4条数据,耗时1分8.96秒

查看执行计划

从执行计划上看,先对视图内的表进行全表扫描,最后在视图上过滤数据。

mysql> explain Select id ,k,left(c,20) from view_sbtest1 where id=1;
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
| id | select_type | table      | partitions | type | possible_keys | key         | key_len | ref   | rows   | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
|  1 | PRIMARY     | <derived2> | NULL       | ref  | <auto_key0>   | <auto_key0> | 4       | const |     10 |   100.00 | NULL  |
|  2 | DERIVED     | sbtest1    | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 986400 |   100.00 | NULL  |
|  3 | UNION       | sbtest2    | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 986400 |   100.00 | NULL  |
|  4 | UNION       | sbtest3    | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 986400 |   100.00 | NULL  |
|  5 | UNION       | sbtest4    | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 986400 |   100.00 | NULL  |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
5 rows in set, 1 warning (0.07 sec)  

添加hint后的执行计划

添加官方的 merge hint 进行视图合并(期望视图不作为一个整体,让where上的过滤条件能下推到视图中的表),不能改变sql执行计划,优化器需要先进行全表扫描在对结果集进行过滤。sql语句的执行时间基本不变

mysql> explain Select /*+  merge(t1) */ id ,k,left(c,20) from view_sbtest1 t1 where id=1;
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
| id | select_type | table      | partitions | type | possible_keys | key         | key_len | ref   | rows   | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
|  1 | PRIMARY     | <derived2> | NULL       | ref  | <auto_key0>   | <auto_key0> | 4       | const |     10 |   100.00 | NULL  |
|  2 | DERIVED     | sbtest1    | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 986400 |   100.00 | NULL  |
|  3 | UNION       | sbtest2    | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 986400 |   100.00 | NULL  |
|  4 | UNION       | sbtest3    | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 986400 |   100.00 | NULL  |
|  5 | UNION       | sbtest4    | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 986400 |   100.00 | NULL  |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
5 rows in set, 1 warning (0.00 sec)

创建视图(过滤条件在视图内)

mysql> drop view view_sbtest3;
ERROR 1051 (42S02): Unknown table \'sbtest.view_sbtest3\'
mysql> Create view view_sbtest3 as 
     select * from sbtest1 where id=1
      union all 
      select * from sbtest2 where id=1
     union all 
    select * from sbtest3  where id=1
     union all 
    select * from sbtest4 where id=1;
Query OK, 0 rows affected (0.02 sec)

查询视图(过滤条件在视图上)

Select id ,k,left(c,20) from view_sbtest3 where id=1;

mysql>  Select id ,k,left(c,20) from view_sbtest3 where id=1;
+----+--------+----------------------+
| id | k      | left(c,20)           |
+----+--------+----------------------+
|  1 | 501462 | 68487932199-96439406 |
|  1 | 434041 | 61753673565-14739672 |
|  1 | 501130 | 64733237507-56788752 |
|  1 | 503019 | 18034632456-32298647 |
+----+--------+----------------------+
4 rows in set (0.01 sec)

直接运行sql语句

 mysql> select id ,k,left(c,20) from sbtest1 where id=1  
    ->  union all 
    ->  select id ,k,left(c,20) from sbtest2 where id=1  
    ->  union all 
    ->  select id ,k,left(c,20) from sbtest3 where id=1 
    ->  union all 
    ->  select id ,k,left(c,20) from sbtest4 where id=1;
+----+--------+----------------------+
| id | k      | left(c,20)           |
+----+--------+----------------------+
|  1 | 501462 | 68487932199-96439406 |
|  1 | 434041 | 61753673565-14739672 |
|  1 | 501130 | 64733237507-56788752 |
|  1 | 503019 | 18034632456-32298647 |
+----+--------+----------------------+
4 rows in set (0.01 sec)

直接运行sql语句或者把过滤条件放到视图内均能很快得到数据。

8.0.32

新的MySQL8.0.32版本 已解决掉该问题,视图上的过滤条件能下推到表上。

 Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement.

mysql> use sbtest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> Select id ,k,left(c,20) from view_sbtest1 where id=1;
+----+--------+----------------------+
| id | k      | left(c,20)           |
+----+--------+----------------------+
|  1 | 501462 | 68487932199-96439406 |
|  1 | 434041 | 61753673565-14739672 |
|  1 | 501130 | 64733237507-56788752 |
|  1 | 503019 | 18034632456-32298647 |
+----+--------+----------------------+
4 rows in set (0.01 sec)

mysql> Select id ,k,left(c,20) from view_sbtest3 where id=1;
+----+--------+----------------------+
| id | k      | left(c,20)           |
+----+--------+----------------------+
|  1 | 501462 | 68487932199-96439406 |
|  1 | 434041 | 61753673565-14739672 |
|  1 | 501130 | 64733237507-56788752 |
|  1 | 503019 | 18034632456-32298647 |
+----+--------+----------------------+
4 rows in set (0.00 sec)

Enjoy GreatSQL

如何跟踪android应用程序消耗的峰值内存

【中文标题】如何跟踪android应用程序消耗的峰值内存【英文标题】:How to track peak memory consumed by android application 【发布时间】:2014-03-07 09:19:28 【问题描述】:

有没有办法找到应用程序在运行时消耗的峰值内存。通常应用程序会占用一些内存,而当应用程序运行时,它会消耗更多内存,并在操作完成后再次释放。我要注意我的应用程序在运行时消耗的峰值内存(图形表示)。

在 DDMS 中,堆选项卡显示应用程序完全使用的堆大小。但我想要应用程序消耗多少,然后每次运行时释放内存。是否有任何工具或 eclipse 插件来跟踪这个?请建议一个解决方案。谢谢。

【问题讨论】:

【参考方案1】:

https://developer.android.com/tools/debugging/debugging-memory.html - 你应该看看这个并使用 hprof 工具来发现你的内存问题。

【讨论】:

以上是关于MySQL一次大量内存消耗的跟踪的主要内容,如果未能解决你的问题,请参考以下文章

休眠,保存 1Millon 实体,消耗大量内存

flask如何使模板返回大文件,又不消耗大量内存

大量内存泄漏 - CocoaLibSpotify

如何跟踪android应用程序消耗的峰值内存

MySQL 8.0不再担心被垃圾SQL搞爆内存

[EntityFramework6,由于有InitializedDatabases列表,所以有大量表使用的内存