MySQL 大数据操作注意事项
http://netkiller.github.io/journal/mysql.parallel.html
Mr. Neo Chen (netkiller), 陈景峰(BG7NYT)
中国广东省深圳市龙华新区民治街道溪山美地
518131
+86 13113668890
+86 755 29812080
版权 © 2011, 2012, 2013, 2014 http://netkiller.github.io
版权声明
转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。
|
|
2014-05-16
摘要 我的系列文档Netkiller Architect 手札 | Netkiller Developer 手札 | Netkiller PHP 手札 | Netkiller Python 手札 | Netkiller Testing 手札 | Netkiller Cryptography 手札 |
Netkiller Linux 手札 | Netkiller Debian 手札 | Netkiller CentOS 手札 | Netkiller FreeBSD 手札 | Netkiller Shell 手札 | Netkiller Security 手札 |
Netkiller Web 手札 | Netkiller Monitoring 手札 | Netkiller Storage 手札 | Netkiller Mail 手札 | Netkiller Docbook 手札 | Netkiller Version 手札 |
Netkiller Database 手札 | Netkiller PostgreSQL 手札 | Netkiller MySQL 手札 | Netkiller NoSQL 手札 | Netkiller LDAP 手札 | Netkiller Network 手札 |
Netkiller Cisco IOS 手札 | Netkiller H3C 手札 | Netkiller Multimedia 手札 | Netkiller Perl 手札 | Netkiller Amateur Radio 手札 |
目录
- 1. 关于 delete
- 2. 关于 update
- 3. 关于创建索引
- 4. 关于 OPTIMIZE
- 5. 确保SELECT不被受阻
1. 关于 delete
delete from mytable 必死无疑,你需要分批删除,尽量缩小每个批次删除的记录数,delete 是可以并行执行的,你可以同时运行多个删除操作
mysql> show processlist;+--------+-----------------+---------------------+-----------+---------+-------+-----------------------------+--------------------------------------------------------+| Id | User | Host | db | Command | Time | State | Info |+--------+-----------------+---------------------+-----------+---------+-------+-----------------------------+--------------------------------------------------------+| 1 | event_scheduler | localhost | NULL | Daemon | 52 | Waiting for next activation | NULL || 115986 | dba | localhost | example | Query | 0 | NULL | show processlist || 117446 | dba | localhost | example | Query | 20 | updating | delete from mytable where OPEN_TIME like '2011.11.28%' || 117525 | dba | localhost | example | Query | 2 | updating | delete from mytable where OPEN_TIME like '2011.12.02%' || 117526 | dba | localhost | example | Query | 49 | updating | delete from mytable where OPEN_TIME like '2011.12.12%' || 117527 | dba | localhost | example | Query | 6 | updating | delete from mytable where OPEN_TIME like '2011.12.21%' || 117528 | dba | localhost | example | Query | 64 | updating | delete from mytable where OPEN_TIME like '2011.12.30%' || 117546 | dba | localhost | example | Query | 33 | updating | delete from mytable where OPEN_TIME like '2011.11.10%' |+--------+-----------------+---------------------+-----------+---------+-------+-----------------------------+--------------------------------------------------------+23 rows in set (0.00 sec)
2. 关于 update
在电商领域常常遇到一个问题“调价”,经常需要调整一批商品的价格, 程序猿一条预警搞定有没有?
update goods set price=price+10 where category_id = xxx
在开发,测试环境是可以通过测试的,一旦部署到生产环境,必死无疑
3. 关于创建索引
大表创建索引需要很久的时间,通常要经历 manage keys 与 copy to tmp table 的过程
mysql> show processlist;+--------+-----------------+---------------------+----------+---------+-------+-----------------------------+------------------------------------------------------------------+| Id | User | Host | db | Command | Time | State | Info |+--------+-----------------+---------------------+----------+---------+-------+-----------------------------+------------------------------------------------------------------+| 1 | event_scheduler | localhost | NULL | Daemon | 47 | Waiting for next activation | NULL || 115986 | dba | localhost | example | Query | 0 | NULL | show processlist || 118814 | dba | 192.168.6.20:50459 | example | Query | 8 | copy to tmp table | ALTER TABLE `mytable` ADD INDEX `modifiy_time` (`MODIFY_TIME`) |+--------+-----------------+---------------------+----------+---------+-------+-----------------------------+------------------------------------------------------------------+17 rows in set (0.00 sec)
删除索引,也需要经理 copy to tmp table 过程,漫长的等待
mysql> show processlist;+--------+-----------------+---------------------+--------------+---------+-------+-----------------------------+-------------------------------------------------+| Id | User | Host | db | Command | Time | State | Info |+--------+-----------------+---------------------+--------------+---------+-------+-----------------------------+-------------------------------------------------+| 1 | event_scheduler | localhost | NULL | Daemon | 11 | Waiting for next activation | NULL || 115986 | dba | localhost | example | Query | 0 | NULL | show processlist || 118814 | dba | 192.168.6.20:50459 | example | Query | 4 | copy to tmp table | ALTER TABLE `mytable` DROP INDEX `modifiy_time` |+--------+-----------------+---------------------+--------------+---------+-------+-----------------------------+-------------------------------------------------+17 rows in set (0.00 sec)
所以数据设计要深思熟虑,做到提前未雨绸缪,不要亡羊补牢
4. 关于 OPTIMIZE
OPTIMIZE 的操作是将当前表复制到临时表操作后再删除当前表,最后将临时表改名
mysql> show processlist;+--------+-----------------+---------------------+---------------------------+---------+-------+-----------------------------+--------------------------+| Id | User | Host | db | Command | Time | State | Info |+--------+-----------------+---------------------+---------------------------+---------+-------+-----------------------------+--------------------------+| 1 | event_scheduler | localhost | NULL | Daemon | 14 | Waiting for next activation | NULL || 115835 | dba | 192.168.6.20:49664 | example | Query | 9 | copy to tmp table | OPTIMIZE TABLE `mytable` || 115986 | dba | localhost | example | Query | 0 | NULL | show processlist |+--------+-----------------+---------------------+---------------------------+---------+-------+-----------------------------+--------------------------+17 rows in set (0.00 sec)
5. 确保SELECT不被受阻
使用各种手段保证select操作不被受阻,只要select一直可以查询网站前端就能提供80%的功能,一旦select受阻一切都是浮云。
保证 select 操作优先于其他操作
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition] [ORDER BY ...] [LIMIT row_count]
update的时候增加 LOW_PRIORITY 参数,可以降低更新语句的优先级。
my.cnf
[mysqld] low_priority_updates=1
或者启动是添加--low-priority-updates参数
全局开启
SET @@global.low_priority_updates = 1;
适用于本次会话连接
SET @@session.low_priority_updates = 1;

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
