search
HomeDatabaseMysql Tutorialmysql学习记录(十一)--事件调度器_MySQL

一、理论:

1.优势:
1)减少系统管理员的操作风险,有利于数据库整体迁移。因数据库迁移时即可同时迁移调度事件
2.场景:
1)定期收集统计信息
2)清理历史数据
3)数据库检查
3.要点:
1)在性能要求高且重要的数据库中要慎重部署和启用调度器
2)过于复杂的更适合用程序
3)开启和关闭事件调度器需要具有超级用户权限

二、实践:

 

mysql> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table test(id1 varchar(10),create_time datetime);
Query OK, 0 rows affected (0.02 sec)

mysql> create event test_event_1
    -> on schedule
    -> every 5 second 
    -> do 
    -> insert into test1.test(id1,create_time)
    -> values ('test',now());
Query OK, 0 rows affected (0.01 sec)

mysql> show events \G;
*************************** 1. row ***************************
                  Db: test1
                Name: test_event_1
             Definer: root@localhost
           Time zone: SYSTEM
                Type: RECURRING
          Execute at: NULL
      Interval value: 5
      Interval field: SECOND
              Starts: 2015-10-14 06:33:17
                Ends: NULL
              Status: ENABLED
          Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
1 row in set (0.01 sec)

ERROR: 
No query specified

mysql> select * from test;
Empty set (0.00 sec)

mysql> show variables like '%scheduler%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| event_scheduler | OFF   |
+-----------------+-------+
1 row in set (0.00 sec)

mysql> set global event_scheduler = 1 ;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%scheduler%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| event_scheduler | ON    |
+-----------------+-------+
1 row in set (0.00 sec)

mysql> show processlist \G;
*************************** 1. row ***************************
     Id: 17
   User: root
   Host: localhost
     db: test1
Command: Sleep
   Time: 247901
  State: 
   Info: NULL
*************************** 2. row ***************************
     Id: 21
   User: root
   Host: localhost
     db: test1
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 3. row ***************************
     Id: 22
   User: event_scheduler
   Host: localhost
     db: NULL
Command: Daemon
   Time: 1
  State: Waiting for next activation
   Info: NULL
3 rows in set (0.00 sec)

ERROR: 
No query specified

mysql> select * from test;
+------+---------------------+
| id1  | create_time         |
+------+---------------------+
| test | 2015-10-14 06:33:57 |
| test | 2015-10-14 06:34:02 |
| test | 2015-10-14 06:34:07 |
| test | 2015-10-14 06:34:12 |
| test | 2015-10-14 06:34:17 |
| test | 2015-10-14 06:34:22 |
+------+---------------------+
6 rows in set (0.00 sec)

mysql> create event trunc_test
    -> on schedule every 1 minute
    -> do truncate table test;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+------+---------------------+
| id1  | create_time         |
+------+---------------------+
| test | 2015-10-14 06:34:52 |
| test | 2015-10-14 06:34:57 |
| test | 2015-10-14 06:35:02 |
| test | 2015-10-14 06:35:07 |
| test | 2015-10-14 06:35:12 |
| test | 2015-10-14 06:35:17 |
| test | 2015-10-14 06:35:22 |
| test | 2015-10-14 06:35:27 |
| test | 2015-10-14 06:35:32 |
| test | 2015-10-14 06:35:37 |
+------+---------------------+
10 rows in set (0.00 sec)

mysql> select * from test;
+------+---------------------+
| id1  | create_time         |
+------+---------------------+
| test | 2015-10-14 06:37:52 |
| test | 2015-10-14 06:37:57 |
+------+---------------------+
2 rows in set (0.00 sec)

mysql> show processlist \G;
*************************** 1. row ***************************
     Id: 17
   User: root
   Host: localhost
     db: test1
Command: Sleep
   Time: 248177
  State: 
   Info: NULL
*************************** 2. row ***************************
     Id: 21
   User: root
   Host: localhost
     db: test1
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 3. row ***************************
     Id: 22
   User: event_scheduler
   Host: localhost
     db: NULL
Command: Daemon
   Time: 2
  State: Waiting for next activation
   Info: NULL
3 rows in set (0.00 sec)

ERROR: 
No query specified

mysql> alter event trunc_test disable;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from test;
+------+---------------------+
| id1  | create_time         |
+------+---------------------+
| test | 2015-10-14 06:38:52 |
| test | 2015-10-14 06:38:57 |
| test | 2015-10-14 06:39:02 |
| test | 2015-10-14 06:39:07 |
| test | 2015-10-14 06:39:12 |
| test | 2015-10-14 06:39:17 |
| test | 2015-10-14 06:39:22 |
| test | 2015-10-14 06:39:27 |
| test | 2015-10-14 06:39:32 |
| test | 2015-10-14 06:39:37 |
+------+---------------------+
10 rows in set (0.00 sec)

mysql> select * from test;
+------+---------------------+
| id1  | create_time         |
+------+---------------------+
| test | 2015-10-14 06:38:52 |
| test | 2015-10-14 06:38:57 |
| test | 2015-10-14 06:39:02 |
| test | 2015-10-14 06:39:07 |
| test | 2015-10-14 06:39:12 |
| test | 2015-10-14 06:39:17 |
| test | 2015-10-14 06:39:22 |
| test | 2015-10-14 06:39:27 |
| test | 2015-10-14 06:39:32 |
| test | 2015-10-14 06:39:37 |
| test | 2015-10-14 06:39:42 |
| test | 2015-10-14 06:39:47 |
| test | 2015-10-14 06:39:52 |
| test | 2015-10-14 06:39:57 |
| test | 2015-10-14 06:40:02 |
| test | 2015-10-14 06:40:07 |
+------+---------------------+
16 rows in set (0.00 sec)

mysql> alter event test_event_1 disable;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+------+---------------------+
| id1  | create_time         |
+------+---------------------+
| test | 2015-10-14 06:38:52 |
| test | 2015-10-14 06:38:57 |
| test | 2015-10-14 06:39:02 |
| test | 2015-10-14 06:39:07 |
| test | 2015-10-14 06:39:12 |
| test | 2015-10-14 06:39:17 |
| test | 2015-10-14 06:39:22 |
| test | 2015-10-14 06:39:27 |
| test | 2015-10-14 06:39:32 |
| test | 2015-10-14 06:39:37 |
| test | 2015-10-14 06:39:42 |
| test | 2015-10-14 06:39:47 |
| test | 2015-10-14 06:39:52 |
| test | 2015-10-14 06:39:57 |
| test | 2015-10-14 06:40:02 |
| test | 2015-10-14 06:40:07 |
| test | 2015-10-14 06:40:12 |
| test | 2015-10-14 06:40:17 |
| test | 2015-10-14 06:40:22 |
| test | 2015-10-14 06:40:27 |
+------+---------------------+
20 rows in set (0.00 sec)

mysql>  drop event test_event_1;
Query OK, 0 rows affected (0.01 sec)

mysql> show events \G;
*************************** 1. row ***************************
                  Db: test1
                Name: trunc_test
             Definer: root@localhost
           Time zone: SYSTEM
                Type: RECURRING
          Execute at: NULL
      Interval value: 1
      Interval field: MINUTE
              Starts: 2015-10-14 06:34:48
                Ends: NULL
              Status: DISABLED
          Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)

ERROR: 
No query specified
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
事件 ID 4660:已删除对象 [修复]事件 ID 4660:已删除对象 [修复]Jul 03, 2023 am 08:13 AM

我们的一些读者遇到了事件ID4660。他们通常不确定该怎么做,所以我们在本指南中解释。删除对象时通常会记录事件ID4660,因此我们还将探索一些实用的方法在您的计算机上修复它。什么是事件ID4660?事件ID4660与活动目录中的对象相关,将由以下任一因素触发:对象删除–每当从ActiveDirectory中删除对象时,都会记录事件ID为4660的安全事件。手动更改–当用户或管理员手动更改对象的权限时,可能会生成事件ID4660。更改权限设置、修改访问级别或添加或删除人员或组时,可能会发生这种情

在iPhone锁屏上获取即将到来的日历事件在iPhone锁屏上获取即将到来的日历事件Dec 01, 2023 pm 02:21 PM

在运行iOS16或更高版本的iPhone上,您可以直接在锁定屏幕上显示即将到来的日历事件。继续阅读以了解它是如何完成的。由于表盘复杂功能,许多AppleWatch用户习惯于能够看一眼手腕来查看下一个即将到来的日历事件。随着iOS16和锁定屏幕小部件的出现,您可以直接在iPhone上查看相同的日历事件信息,甚至无需解锁设备。日历锁定屏幕小组件有两种风格,允许您跟踪下一个即将发生的事件的时间,或使用更大的小组件来显示事件名称及其时间。若要开始添加小组件,请使用面容ID或触控ID解锁iPhone,长按

在JavaScript中,"oninput"事件的用途是什么?在JavaScript中,"oninput"事件的用途是什么?Aug 26, 2023 pm 03:17 PM

当在输入框中添加值时,就会发生oninput事件。您可以尝试运行以下代码来了解如何在JavaScript中实现oninput事件-示例<!DOCTYPEhtml><html>&nbsp;&nbsp;<body>&nbsp;&nbsp;&nbsp;<p>Writebelow:</p>&nbsp;&nbsp;&nbsp;<inputtype="text"

如何在PHP项目中实现日历功能和事件提醒?如何在PHP项目中实现日历功能和事件提醒?Nov 02, 2023 pm 12:48 PM

如何在PHP项目中实现日历功能和事件提醒?在开发Web应用程序时,日历功能和事件提醒是常见的需求之一。无论是个人日程管理、团队协作,还是在线活动安排,日历功能都可以提供便捷的时间管理和事务安排。在PHP项目中实现日历功能和事件提醒可以通过以下步骤来完成。数据库设计首先,需要设计数据库表来存储日历事件的相关信息。一个简单的设计可以包含以下字段:id:事件的唯一

jQuery中如何实现select元素的改变事件绑定jQuery中如何实现select元素的改变事件绑定Feb 23, 2024 pm 01:12 PM

jQuery是一个流行的JavaScript库,可以用来简化DOM操作、事件处理、动画效果等。在web开发中,经常会遇到需要对select元素进行改变事件绑定的情况。本文将介绍如何使用jQuery实现对select元素改变事件的绑定,并提供具体的代码示例。首先,我们需要使用标签来创建一个包含选项的下拉菜单:

jquery中常用的事件有哪些jquery中常用的事件有哪些Jan 03, 2023 pm 06:13 PM

jquery中常用的事件有:1、window事件;2、鼠标事件,是当用户在文档上面移动或单击鼠标时而产生的事件,包括鼠标单击、移入事件、移出事件等;3、键盘事件,是用户每次按下或者释放键盘上的按键时都会产生事件,包括按下按键事件、释放按键按键等;4、表单事件,例如当元素获得焦点时会触发focus()事件,失去焦点时会触发blur()事件,表单提交时会触发submit()事件。

Vue文档中的input框绑定事件详解Vue文档中的input框绑定事件详解Jun 21, 2023 am 08:12 AM

Vue.js是一种轻量级的JavaScript框架,具有易用、高效和灵活的特点,是目前广受欢迎的前端框架之一。在Vue.js中,input框绑定事件是一个十分常见的需求,本文将详细介绍Vue文档中的input框绑定事件。一、基础概念在Vue.js中,input框绑定事件指的是将输入框的值绑定到Vue实例的数据对象中,从而实现输入和响应的双向绑定。在Vue.j

如何使用 PHP 构建基于事件的应用程序如何使用 PHP 构建基于事件的应用程序May 04, 2024 pm 02:24 PM

在PHP中构建基于事件的应用程序的方法包括:使用EventSourceAPI创建事件源,并在客户端使用EventSource对象监听事件。使用服务器发送的事件(SSE)发送事件,并在客户端使用XMLHttpRequest对象监听事件。一个实用的例子是在电子商务网站中使用EventSource实时更新库存计数,在服务器端通过随机更改库存并发送更新来实现,客户端则通过EventSource监听库存更新并实时显示。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Safe Exam Browser

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools