mysql定时器既是mysql的事件,在实际开发中,我们有时候需要定时去执行一些操作,大部分人通过ScheduledExecutorService类去创建定时,这种如果遇到大数据的更新的时候,运行速度比较忙,这时候我们可以考虑使用mysql定时器去执行SQL脚本文件。
要使用mysql定时器。
首先必须启动调度器“event_scheduler”。
查看当前是否已开启事件计划(调度器)有3种方法:
1) SHOW VARIABLES LIKE 'event_scheduler';
2) SELECT @@event_scheduler;
3) SHOW PROCESSLIST;
开启事件计划(调度器)开关有4种方法:
1) SET GLOBAL event_scheduler = 1;
2) SET @@global.event_scheduler = 1;
3) SET GLOBAL event_scheduler = ON;
4) SET @@global.event_scheduler = ON;
其次是创建定时器事件,可以通过sql创建,也可以安装Navicat Premium(mysql客户端)创建。
1)通过sql创建定时器事件:
create event if not exists eventJob
on schedule every 2 second STARTS '2015-07-14 00:00:00'
on completion PRESERVE
ENABLE
do call mypro();
上述sql脚本中“eventJob ”为定时器事件名称,“mypro”为mysql函数或者存储过程。sql脚本表示从 '2015-07-14 00:00:00'开始,每2秒钟执行mypro()函数一次。当然也可以设置结束时间。
2) 通过客户端创建定时器事件:
如图中所示,call关键字后是需要执行的函数或者存储过程;状态表示定时器事件的状态,是否启用,enable表示启用,disable表示不启用,可以手动设置也可以通过sql来进行设置; on completion设置为“PRESERVE”。
“计划”中标签中,主要是设置定时器的频率和开始结束时间。AT表示,从当前mysql数据库时间开始(这个时间是从启动器启用开始,即当状态为“ENABLE”时),根据一定频率的执行;EVERY是自定义开始时间和结束时间;相对来书EVERY比较灵活,使用方便。
最后,当然是设置定时器事件的状态。
在上一步中,我们无论是sql创建还是客户端创建都可以设置状态,但是当我们没有设置状态或者说关闭定时器事件的时候,就需要执行sql
ALTER EVENT eventJob ON COMPLETION PRESERVE ENABLE; ---- 开启事件
ALTER EVENT eventJob ON COMPLETION PRESERVE DISABLE; ---- 关闭事件

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools
