温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL8的数据库优化讲义

发布时间:2020-04-23 11:35:37 来源:亿速云 阅读:268 作者:三月 栏目:MySQL数据库

下文内容主要给大家带来MySQL8的数据库优化讲义,这里所讲到的知识,与书籍略有不同,都是亿速云专业技术人员在与用户接触过程中,总结出来的,具有一定的经验分享价值,希望给广大读者带来帮助。

EXPLAIN

EXPLAIN
select dept_name from dept_emp join employees on dept_emp.emp_no=employees.emp_no join departments on departments.dept_no=dept_emp.dept_no where employees.first_name='Aamer';

EXPLAIN format=JSON select dept_name from dept_emp join employees on dept_emp.emp_no=employees.emp_no join departments on departments.dept_no=dept_emp.dept_no where employees.first_name='Aamer';

mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|              30 |
+-----------------+
1 row in set (0.01 sec)

mysql> explain format=json for connection 30\G
ERROR 3012 (HY000): EXPLAIN FOR CONNECTION command is supported only for SELECT/UPDATE/INSERT/DELETE/REPLACE

mysql> pager grep rows

mysql> SELECT e.emp_no,salary from  salaries s join employees e on s.emp_no=e.emp_no where first_name='Adam';
2384 rows in set (0.24 sec)

mysql> pager
Default pager wasn't set, using stdout.

MySQL8的数据库优化讲义

mysqlslap

mysqlslap 对于模拟多个用户同时对 MySQL 发起“进攻”提供了方便。同时详细的提供了“高负荷*** MySQL”的详细数据报告。

#mysqlslap -uroot -p --create-schema=employees --query="SELECT e.emp_no,salary from  salaries s join employees e on s.emp_no=e.emp_no where first_name='Adam';" -c 100 -i 100
Enter password:
Benchmark
Average number of seconds to run all queries: 0.995 seconds
Minimum number of seconds to run all queries: 0.876 seconds
Maximum number of seconds to run all queries: 1.155 seconds
Number of clients running queries: 100
Average number of queries per client: 1

#mysqlslap -uroot -p  --auto-generate-sql --concurrency=50,100 --number-of-queries=1000  -i 5  --debug-info
Enter password:
Benchmark
Average number of seconds to run all queries: 0.148 seconds
Minimum number of seconds to run all queries: 0.148 seconds
Maximum number of seconds to run all queries: 0.148 seconds
Number of clients running queries: 50
Average number of queries per client: 20

Benchmark
Average number of seconds to run all queries: 0.143 seconds
Minimum number of seconds to run all queries: 0.143 seconds
Maximum number of seconds to run all queries: 0.143 seconds
Number of clients running queries: 100
Average number of queries per client: 10

mysqlslap -uroot -p  --auto-generate-sql --only-print

#mysqlslap -uroot -p --concurrency=50,100,200 --iterations=1 --number-int-cols=4 --number-char-cols=35 \
--auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed --engine=innodb --number-of-queries=200
Enter password:
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.042 seconds
Minimum number of seconds to run all queries: 0.042 seconds
Maximum number of seconds to run all queries: 0.042 seconds
Number of clients running queries: 50
Average number of queries per client: 4

Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.022 seconds
Minimum number of seconds to run all queries: 0.022 seconds
Maximum number of seconds to run all queries: 0.022 seconds
Number of clients running queries: 100
Average number of queries per client: 2

Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.515 seconds
Minimum number of seconds to run all queries: 0.515 seconds
Maximum number of seconds to run all queries: 0.515 seconds
Number of clients running queries: 200
Average number of queries per client: 1

#mysqlslap -uroot -p --delimiter=";" --create="CREATE TABLE a (b int);INSERT INTO a VALUES (23)" --query="SELECT * FROM a" --concurrency=50 --iterations=200
Enter password:
Benchmark
Average number of seconds to run all queries: 0.002 seconds
Minimum number of seconds to run all queries: 0.002 seconds
Maximum number of seconds to run all queries: 0.007 seconds
Number of clients running queries: 50
Average number of queries per client: 1

mysqlslap --concurrency=5 --iterations=5 --query=query.sql --create=create.sql --delimiter=";"

参考:https://my.oschina.net/moooofly/blog/152547

where、order by、group by、表连接都会使用到索引  
主键的选择:
1、唯一非空
2、尽量的小,因为所有的二级索引都会存储主键
3、选择一个单调自增的键,物理行是按照主键排序的

alter table employees add [unqiue] index idx_name(first_name(10));
alter table employees drop index idx_name;

对于以上关于MySQL8的数据库优化讲义,如果大家还有更多需要了解的可以持续关注我们亿速云的行业推新,如需获取专业解答,可在官网联系售前售后的,希望该文章可给大家带来一定的知识更新。

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI