准备数据 sysbench --test=oltp --oltp-nontrx-mode=update_key --mysql-table-engine=innodb --oltp-table-size=1000000 --mysql-socket=/tmp/mysql3392.sock --mysql-user=dba --mysql-host=localhost --mysql-password=localdba --db-driver=mysql --mysq
准备数据
sysbench --test=oltp --oltp-nontrx-mode=update_key --mysql-table-engine=innodb --oltp-table-size=1000000 --mysql-socket=/tmp/mysql3392.sock --mysql-user=dba --mysql-host=localhost --mysql-password=localdba --db-driver=mysql --mysql-db=test prepare
mysql> desc sbtest;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| k | int(10) unsigned | NO | MUL | 0 | |
| c | char(120) | NO | | | |
| pad | char(60) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
mysql> select count(c) from sbtest;
+----------+
| count(c) |
+----------+
| 1000000 |
+----------+
char的情况下:::
mysql> desc select * from sbtest order by pad;
+----+-------------+--------+------+---------------+------+---------+------+---------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+---------+----------------+
| 1 | SIMPLE | sbtest | ALL | NULL | NULL | NULL | NULL | 1000126 | Using filesort |
+----+-------------+--------+------+---------------+------+---------+------+---------+----------------+
mysql> set profiling = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from sbtest order by pad;
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
1 row in set (0.53 sec)
mysql> show profiles;
+----------+------------+------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------------------+
| 1 | 0.53102850 | select count(*) from sbtest order by pad |
+----------+------------+------------------------------------------+
1 row in set (0.02 sec)
mysql> show profile cpu,block io for query 1;
+----------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000127 | 0.000000 | 0.000000 | 0 | 0 |
| checking permissions | 0.000017 | 0.000000 | 0.000000 | 0 | 0 |
| Opening tables | 0.000042 | 0.000000 | 0.000000 | 0 | 0 |
| System lock | 0.000022 | 0.000000 | 0.000000 | 0 | 0 |
| init | 0.000038 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000011 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.000022 | 0.000000 | 0.000000 | 0 | 0 |
| preparing | 0.000017 | 0.000000 | 0.000000 | 0 | 0 |
| executing | 0.000014 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.530471 | 0.502923 | 0.018997 | 32 | 0 |
| end | 0.000034 | 0.000000 | 0.000000 | 0 | 0 |
| query end | 0.000010 | 0.000000 | 0.000000 | 0 | 0 |
| closing tables | 0.000025 | 0.000000 | 0.000000 | 0 | 0 |
| freeing items | 0.000037 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000128 | 0.000000 | 0.000000 | 0 | 8 |
| cleaning up | 0.000012 | 0.000000 | 0.000000 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+
/////char change to varchar
mysql> alter table sbtest change pad pad varchar(60);
Query OK, 1000000 rows affected (10.72 sec)
Records: 1000000 Duplicates: 0 Warnings: 0
mysql> select pad from sbtest limit 30;
+----------------------------------------------------+
| pad |
+----------------------------------------------------+
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
| qqqqqqqqqqwwwwwwwwwweeeeeeeeeerrrrrrrrrrtttttttttt |
mysql> desc sbtest;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| k | int(10) unsigned | NO | MUL | 0 | |
| c | char(120) | NO | | | |
| pad | varchar(60) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
================无索引的情况下:=====================
varchar(60)的情况下:
mysql> desc select * from sbtest order by pad;
+----+-------------+--------+------+---------------+------+---------+------+---------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+---------+----------------+
| 1 | SIMPLE | sbtest | ALL | NULL | NULL | NULL | NULL | 1000117 | Using filesort |
+----+-------------+--------+------+---------------+------+---------+------+---------+----------------+
mysql> select count(*) from sbtest order by pad;
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
1 row in set (0.51 sec)
mysql> show profile cpu,block io for query 11;
+----------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000081 | 0.000000 | 0.000000 | 0 | 0 |
| checking permissions | 0.000014 | 0.000000 | 0.000000 | 0 | 0 |
| Opening tables | 0.000033 | 0.000000 | 0.000000 | 0 | 0 |
| System lock | 0.000025 | 0.000000 | 0.000000 | 0 | 0 |
| init | 0.000028 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000009 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.000019 | 0.000000 | 0.000000 | 0 | 0 |
| preparing | 0.000013 | 0.000000 | 0.000000 | 0 | 0 |
| executing | 0.000011 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.506327 | 0.489925 | 0.018997 | 0 | 0 |
| end | 0.000026 | 0.001000 | 0.000000 | 0 | 0 |
| query end | 0.000010 | 0.000000 | 0.000000 | 0 | 0 |
| closing tables | 0.000022 | 0.000000 | 0.000000 | 0 | 0 |
| freeing items | 0.000034 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000003 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000114 | 0.000000 | 0.000000 | 0 | 8 |
| cleaning up | 0.000009 | 0.000000 | 0.000000 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+
varchar(120)的情况下:
mysql> alter table sbtest change pad pad varchar(120);
Query OK, 1000000 rows affected (11.77 sec)
Records: 1000000 Duplicates: 0 Warnings: 0
mysql> desc sbtest;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| k | int(10) unsigned | NO | MUL | 0 | |
| c | char(120) | NO | | | |
| pad | varchar(120) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
mysql> desc select * from sbtest;
+----+-------------+--------+------+---------------+------+---------+------+---------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+---------+-------+
| 1 | SIMPLE | sbtest | ALL | NULL | NULL | NULL | NULL | 1000117 | |
+----+-------------+--------+------+---------------+------+---------+------+---------+-------+
1 row in set (0.00 sec)
mysql> desc select count(*) from sbtest;
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | sbtest | index | NULL | k | 4 | NULL | 1000117 | Using index |
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
mysql> select count(*) from sbtest order by pad;
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
1 row in set (0.51 sec)
mysql> show profile cpu,block io for query 17;
+----------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000073 | 0.000000 | 0.000000 | 0 | 0 |
| checking permissions | 0.000013 | 0.000000 | 0.000000 | 0 | 0 |
| Opening tables | 0.000031 | 0.000000 | 0.000000 | 0 | 0 |
| System lock | 0.000024 | 0.000000 | 0.000000 | 0 | 0 |
| init | 0.000026 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000008 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.000019 | 0.000000 | 0.000000 | 0 | 0 |
| preparing | 0.000013 | 0.000000 | 0.000000 | 0 | 0 |
| executing | 0.000011 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.510981 | 0.491925 | 0.018997 | 0 | 0 |
| end | 0.000025 | 0.000000 | 0.000000 | 0 | 0 |
| query end | 0.000013 | 0.000000 | 0.000000 | 0 | 0 |
| closing tables | 0.000026 | 0.000000 | 0.000000 | 0 | 0 |
| freeing items | 0.000039 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000004 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000110 | 0.000000 | 0.000000 | 0 | 8 |
| cleaning up | 0.000007 | 0.000000 | 0.000000 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+
================有索引的情况下:=====================
alter table sbtest change pad pad varchar(60);
mysql> alter table sbtest add key(pad);
Query OK, 0 rows affected (7.60 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from sbtest;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sbtest | 0 | PRIMARY | 1 | id | A | 1000117 | NULL | NULL | | BTREE | | |
| sbtest | 1 | k | 1 | k | A | 6 | NULL | NULL | | BTREE | | |
| sbtest | 1 | pad | 1 | pad | A | 200 | NULL | NULL | YES | BTREE | | |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
varchar(60)
mysql> desc select count(*) from sbtest order by pad;
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | sbtest | index | NULL | pad | 183 | NULL | 1000117 | Using index |
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
mysql> select count(*) from sbtest order by pad;
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
1 row in set (0.44 sec)
mysql> show profile cpu,block io for query 1
-> ;
+----------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000115 | 0.000000 | 0.000000 | 0 | 0 |
| checking permissions | 0.000017 | 0.000000 | 0.000000 | 0 | 0 |
| Opening tables | 0.000043 | 0.000000 | 0.000000 | 0 | 0 |
| System lock | 0.000023 | 0.000000 | 0.000000 | 0 | 0 |
| init | 0.000038 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000010 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.000024 | 0.000000 | 0.000000 | 0 | 0 |
| preparing | 0.000018 | 0.000000 | 0.000000 | 0 | 0 |
| executing | 0.000015 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.445040 | 0.428935 | 0.015998 | 0 | 0 |
| end | 0.000023 | 0.000000 | 0.000000 | 0 | 0 |
| query end | 0.000013 | 0.000000 | 0.000000 | 0 | 0 |
| closing tables | 0.000028 | 0.000000 | 0.000000 | 0 | 0 |
| freeing items | 0.000041 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000003 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000133 | 0.000000 | 0.001000 | 0 | 8 |
| cleaning up | 0.000008 | 0.000000 | 0.000000 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+
varchar(120)的:
mysql> alter table sbtest change pad pad varchar(120);
Query OK, 1000000 rows affected (14.56 sec)
Records: 1000000 Duplicates: 0 Warnings: 0
mysql> show index from sbtest;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sbtest | 0 | PRIMARY | 1 | id | A | 1000117 | NULL | NULL | | BTREE | | |
| sbtest | 1 | k | 1 | k | A | 6 | NULL | NULL | | BTREE | | |
| sbtest | 1 | pad | 1 | pad | A | 24 | NULL | NULL | YES | BTREE | | |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
这里开始有区别了.Cardinality 从200降到24
varchar(120) Cardinality =200
varchar(60) Cardinality =40 ... 字段是全部60char长度的
mysql> desc select count(*) from sbtest order by pad;
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | sbtest | index | NULL | pad | 363 | NULL | 1000117 | Using index |
+----+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
mysql> select count(*) from sbtest order by pad;
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
1 row in set (0.45 sec)
mysql> show profile cpu,block io for query 8;
+----------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000120 | 0.000000 | 0.000000 | 0 | 0 |
| checking permissions | 0.000017 | 0.000000 | 0.000000 | 0 | 0 |
| Opening tables | 0.000046 | 0.000000 | 0.000000 | 0 | 0 |
| System lock | 0.000028 | 0.000000 | 0.000000 | 0 | 0 |
| init | 0.000038 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000011 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.000023 | 0.000000 | 0.000000 | 0 | 0 |
| preparing | 0.000018 | 0.000000 | 0.000000 | 0 | 0 |
| executing | 0.000013 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.452434 | 0.437933 | 0.014998 | 0 | 0 |
| end | 0.000025 | 0.000000 | 0.000000 | 0 | 0 |
| query end | 0.000010 | 0.000000 | 0.000000 | 0 | 0 |
| closing tables | 0.000023 | 0.000000 | 0.000000 | 0 | 0 |
| freeing items | 0.000033 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000003 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000098 | 0.000000 | 0.000000 | 0 | 8 |
| cleaning up | 0.000007 | 0.000000 | 0.000000 | 0 | 0 |
+----------------------+----------+----------+------------+--------------+---------------+
这里也有区别了.有索引的情况下,
是0.44 pk 0.45s
=====================================================================================================
加到varchar(600)试下
mysql> alter table sbtest change pad pad varchar(600);
Query OK, 1000000 rows affected, 2 warnings (14.60 sec)
Records: 1000000 Duplicates: 0 Warnings: 2
mysql> desc select count(*) from sbtest order by pad;
+----+-------------+--------+------+---------------+------+---------+------+---------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+---------+-------+
| 1 | SIMPLE | sbtest | ALL | NULL | NULL | NULL | NULL | 1000117 | |
+----+-------------+--------+------+---------------+------+---------+------+---------+-------+
1 row in set (0.01 sec)
mysql> select count(*) from sbtest order by pad;
+----------+
| count(*) |
+----------+
| 1000000 |
+----------+
1 row in set (0.50 sec)
600是0.5s....

InnoDBBufferPool通过缓存数据和索引页来减少磁盘I/O,提升数据库性能。其工作原理包括:1.数据读取:从BufferPool中读取数据;2.数据写入:修改数据后写入BufferPool并定期刷新到磁盘;3.缓存管理:使用LRU算法管理缓存页;4.预读机制:提前加载相邻数据页。通过调整BufferPool大小和使用多个实例,可以优化数据库性能。

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。 MySQL以其高性能、可扩展性和跨平台支持着称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

MySQL值得学习,因为它是强大的开源数据库管理系统,适用于数据存储、管理和分析。1)MySQL是关系型数据库,使用SQL操作数据,适合结构化数据管理。2)SQL语言是与MySQL交互的关键,支持CRUD操作。3)MySQL的工作原理包括客户端/服务器架构、存储引擎和查询优化器。4)基本用法包括创建数据库和表,高级用法涉及使用JOIN连接表。5)常见错误包括语法错误和权限问题,调试技巧包括检查语法和使用EXPLAIN命令。6)性能优化涉及使用索引、优化SQL语句和定期维护数据库。

MySQL适合初学者学习数据库技能。1.安装MySQL服务器和客户端工具。2.理解基本SQL查询,如SELECT。3.掌握数据操作:创建表、插入、更新、删除数据。4.学习高级技巧:子查询和窗口函数。5.调试和优化:检查语法、使用索引、避免SELECT*,并使用LIMIT。

MySQL通过表结构和SQL查询高效管理结构化数据,并通过外键实现表间关系。1.创建表时定义数据格式和类型。2.使用外键建立表间关系。3.通过索引和查询优化提高性能。4.定期备份和监控数据库确保数据安全和性能优化。

MySQL是一个开源的关系型数据库管理系统,广泛应用于Web开发。它的关键特性包括:1.支持多种存储引擎,如InnoDB和MyISAM,适用于不同场景;2.提供主从复制功能,利于负载均衡和数据备份;3.通过查询优化和索引使用提高查询效率。

SQL用于与MySQL数据库交互,实现数据的增、删、改、查及数据库设计。1)SQL通过SELECT、INSERT、UPDATE、DELETE语句进行数据操作;2)使用CREATE、ALTER、DROP语句进行数据库设计和管理;3)复杂查询和数据分析通过SQL实现,提升业务决策效率。

MySQL的基本操作包括创建数据库、表格,及使用SQL进行数据的CRUD操作。1.创建数据库:CREATEDATABASEmy_first_db;2.创建表格:CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY,titleVARCHAR(100)NOTNULL,authorVARCHAR(100)NOTNULL,published_yearINT);3.插入数据:INSERTINTObooks(title,author,published_year)VA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版
中文版,非常好用

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载
最流行的的开源编辑器

禅工作室 13.0.1
功能强大的PHP集成开发环境