Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改数据库表引擎提高全表行数统计的性能 #90

Open
wtune opened this issue Mar 20, 2020 · 0 comments
Open

修改数据库表引擎提高全表行数统计的性能 #90

wtune opened this issue Mar 20, 2020 · 0 comments

Comments

@wtune
Copy link

wtune commented Mar 20, 2020

问题

从数据库查询日志中我们可以看到有很多统计全表行数的代码(多为执行分页查询前做的准备查询),如下所示

SELECT COUNT(1) FROM sys_notice
SELECT COUNT(1) FROM sys_operation_log WHERE 1 = 1
SELECT COUNT(1) FROM sys_dept WHERE 1 = 1
SELECT COUNT(1) FROM sys_login_log WHERE 1 = 1
SELECT COUNT(1) FROM sys_dict_type
SELECT COUNT(1) FROM sys_role

在InnoDB中不会缓存表的元数据,因此执行这样的语句会造成比较大的时间开销,而MyISAM则缓存有表的元数据,统计行数可以在很短的时间内完成。如果没有其它必须使用InnoDB的原因,建议修改表的存储引擎从而优化这类语句。
参考:https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_count

解决方法

修改表的存储引擎为MyISAM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant