MySQL状态变量是当前服务器自启动后累计的一些系统状态信息,主要用于评估当前系统资源的使用情况以进一步分析系统性能而做出相应的调整决策。这些状态变量我们可以理解为等同于Oracle数据库的动态性能视图。MySQL的状态变量有很多,比如SQL执行频率,索引的使用情况,锁资源的使用情况等等。状态变量可以分区全局以及会话级别的状态变量。状态变量不可修改,为只读属性,由系统更新。本文演示了状态变量的一些示例,仅为抛砖引玉之用。
1、状态变量
2、show方式查看状态变量
--当前演示环境 root@localhost[(none)]> show variables like 'version'; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.5.39-log | +---------------+------------+ a、查看所有状态变量 root@localhost[(none)]> show status; +------------------------------------------+-------------+ | Variable_name | Value | +------------------------------------------+-------------+ | Aborted_clients | 0 | | Binlog_stmt_cache_use | 1 | | Bytes_received | 135 | | Bytes_sent | 266 | | ................ | | | Threads_running | 1 | | Uptime | 76242 | | Uptime_since_flush_status | 76242 | +------------------------------------------+-------------+ 312 rows in set (0.00 sec) --可以看出当前版本5.5.39有312个状态变量 b、查看仅有global的状态变量(connections) --查看指定的状态变量,以下两个与connection相关的都为全局状态变量 root@localhost[(none)]> show global status like 'connection%'; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | Connections | 11 | --连接到MySQL服务器的数量(包含成功或失败的)。 +----------------------+-------+ suse11b:~ # mysql -ufred fred@localhost[(none)]> show global status like '%connection%'; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | Connections | 12 | --连接之后,我们看到Connections的值变为12了。 +----------------------+-------+ c、查看既有global又有session状态的变量 --查看session状态变量opened_tables root@localhost[tempdb]> show session status like 'opened_tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 0 | +---------------+-------+ root@localhost[tempdb]> select count(*) from tb_slow; +----------+ | count(*) | +----------+ | 424448 | +----------+ root@localhost[tempdb]> show session status like 'opened_tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 1 | --值变为1 +---------------+-------+ --从information_schema.session_status表查询状态变量OPENED_TABLES root@localhost[tempdb]> select * from information_schema.session_status -> where variable_name like 'opened_tables'; +---------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +---------------+----------------+ | OPENED_TABLES | 1 | +---------------+----------------+ --查看全局状态变量opened_tables root@localhost[tempdb]> show global status like 'opened_tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 54 | +---------------+-------+ root@localhost[tempdb]> select count(*) from mysql.db; +----------+ | count(*) | +----------+ | 2 | +----------+ root@localhost[tempdb]> show global status like 'opened_tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 55 | --值变为55 +---------------+-------+ --从information_schema.global_status表查询状态变量OPENED_TABLES root@localhost[tempdb]> select * from information_schema.global_status -> where variable_name like 'opened_tables'; +---------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +---------------+----------------+ | OPENED_TABLES | 55 | +---------------+----------------+ --清洗状态变量统计信息 root@localhost[tempdb]> flush status; Query OK, 0 rows affected (0.00 sec) --下面的查询结果可以看出,session级别的opened_tables被重置为0 root@localhost[tempdb]> show session status like 'opened_tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 0 | +---------------+-------+ --Author: Leshami --Blog : http://blog.csdn.net/leshami --而全局级别的opened_tables未受到任何影响 root@localhost[tempdb]> show global status like 'opened_tables'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Opened_tables | 55 | +---------------+-------+
3、使用mysqladmin extended-status获取状态变量
suse11b:~ # mysqladmin extended-status|grep Connections | Connections | 18 | suse11b:~ # mysql root@localhost[(none)]> system mysqladmin extended-status|grep Connections | Connections | 20 | root@localhost[(none)]> exit Bye suse11b:~ # mysqladmin --help |more #mysqladmin与状态变量有关的使用 extended-status Gives an extended status message from the server flush-status Clear status variables

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)長度,始終使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

mysqlStringTypesIncludeVarChar,文本,char,Enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptoPuptOuptoPepePecifiedLimit.2)textisidealforlargetStortStorStoverStoverStorageWithoutAutAdefinedLength.3)charlisfixed-lenftenge,for forConsistentDatalikeCodes.4)

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)

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)創建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。 2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。 3)GrantWriteAccessto'

MySQL中的字符串數據類型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序規則(Collations)決定了字符串的比較和排序方式。 1.CHAR適合固定長度字符串,VARCHAR適合可變長度字符串。 2.BINARY和VARBINARY用於二進制數據,BLOB和TEXT用於大對像數據。 3.排序規則如utf8mb4_unicode_ci忽略大小寫,適合用戶名;utf8mb4_bin區分大小寫,適合需要精確比較的字段。

最佳的MySQLVARCHAR列長度選擇應基於數據分析、考慮未來增長、評估性能影響及字符集需求。 1)分析數據以確定典型長度;2)預留未來擴展空間;3)注意大長度對性能的影響;4)考慮字符集對存儲的影響。通過這些步驟,可以優化數據庫的效率和擴展性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)