• MySQL 事件调度


    1、打开数据库连接、找到事件,右键点击新建事件

    2、在定义中编写要执行的事件sql代码,在计划中设计事件调度执行的规律。

    编写sql时要注意格式要求:

    (1)开头要写BEGIN,结尾处写END

    (2)每一个sql结束处需要打上分好,“;”,区分每一条sql语句,漏写时,事件无法保存

    (3)事件示例

    1. BEGIN
    2. -- 推送当天门票预测数据
    3. DELETE FROM ylxs_dp.ylxs_jqpwfx_mpydqktj WHERE SUBSTR(sj,1,10) = (SUBSTR(NOW(),1,10));
    4. -- 添加今日数据
    5. INSERT INTO ylxs_jqpwfx_mpydqktj (jr,jqmc,sj,jdmc,jrwj,jrjp,mr)
    6. SELECT
    7. count(0) as jr,
    8. '玉龙雪山' as jqmc,
    9. orderTime as sj,
    10. scenicAreaName as jdmc,
    11. 0 as jrwj,
    12. 0 as jrjp,
    13. 0 as mr
    14. FROM
    15. ylxs_hldc.t_dm_ticket_data
    16. WHERE
    17. scenicAreaName in ('冰川公园索道','云杉坪索道','印象丽江','牦牛坪索道','玉龙雪山景区门票')
    18. and SUBSTR(orderTime,1,10) = (SUBSTR(NOW(),1,10))
    19. GROUP BY scenicAreaName;
    20. INSERT INTO ylxs_jqpwfx_mpydqktj (jr,jqmc,sj,jdmc,jrwj,jrjp)
    21. SELECT
    22. count(0) as jr,
    23. '玉龙雪山' as jqmc,
    24. orderTime as sj,
    25. '玉龙雪山景区门票' as jdmc,
    26. 0 as jrwj,
    27. 0 as jrjp
    28. FROM
    29. ylxs_hldc.t_dm_ticket_data
    30. WHERE
    31. scenicAreaName in ('冰川公园索道','云杉坪索道','印象丽江','牦牛坪索道','玉龙雪山景区门票')
    32. and SUBSTR(orderTime,1,10) = (SUBSTR(NOW(),1,10));
    33. -- 添加昨日数据
    34. UPDATE
    35. ylxs_jqpwfx_mpydqktj
    36. SET
    37. zr =
    38. (SELECT
    39. IFNULL(count(0),0) as sl
    40. FROM
    41. ylxs_hldc.t_dm_ticket_data
    42. WHERE
    43. scenicAreaName = '冰川公园索道'
    44. and SUBSTR(orderTime,1,10) = date_sub(SUBSTR(NOW(),1,10),interval 1 day)
    45. )
    46. WHERE SUBSTR(sj,1,10) = (SUBSTR(NOW(),1,10)) and jdmc = '冰川公园索道';
    47. UPDATE
    48. ylxs_jqpwfx_mpydqktj
    49. SET
    50. zr =
    51. (SELECT
    52. IFNULL(count(0),0) as sl
    53. FROM
    54. ylxs_hldc.t_dm_ticket_data
    55. WHERE
    56. scenicAreaName = '云杉坪索道'
    57. and SUBSTR(orderTime,1,10) = date_sub(SUBSTR(NOW(),1,10),interval 1 day)
    58. )
    59. WHERE SUBSTR(sj,1,10) = (SUBSTR(NOW(),1,10)) and jdmc = '云杉坪索道';
    60. UPDATE
    61. ylxs_jqpwfx_mpydqktj
    62. SET
    63. zr =
    64. (SELECT
    65. IFNULL(count(0),0) as sl
    66. FROM
    67. ylxs_hldc.t_dm_ticket_data
    68. WHERE
    69. scenicAreaName = '印象丽江'
    70. and SUBSTR(orderTime,1,10) = date_sub(SUBSTR(NOW(),1,10),interval 1 day)
    71. )
    72. WHERE SUBSTR(sj,1,10) = (SUBSTR(NOW(),1,10)) and jdmc = '印象丽江';
    73. UPDATE
    74. ylxs_jqpwfx_mpydqktj
    75. SET
    76. zr =
    77. (SELECT
    78. IFNULL(count(0),0) as sl
    79. FROM
    80. ylxs_hldc.t_dm_ticket_data
    81. WHERE
    82. scenicAreaName = '牦牛坪索道'
    83. and SUBSTR(orderTime,1,10) = date_sub(SUBSTR(NOW(),1,10),interval 1 day)
    84. )
    85. WHERE SUBSTR(sj,1,10) = (SUBSTR(NOW(),1,10)) and jdmc = '牦牛坪索道';
    86. UPDATE
    87. ylxs_jqpwfx_mpydqktj
    88. SET
    89. zr =
    90. ( SELECT
    91. count(0) as sl
    92. FROM
    93. ylxs_hldc.t_dm_ticket_data
    94. WHERE
    95. scenicAreaName in ('冰川公园索道','云杉坪索道','印象丽江','牦牛坪索道','玉龙雪山景区门票')
    96. and SUBSTR(orderTime,1,10) = date_sub(SUBSTR(NOW(),1,10),interval 1 day)
    97. )
    98. WHERE SUBSTR(sj,1,10) = (SUBSTR(NOW(),1,10)) and jdmc = '玉龙雪山景区门票';
    99. -- 添加玉龙雪山明日预测数据
    100. UPDATE
    101. ylxs_jqpwfx_mpydqktj
    102. SET
    103. mr =
    104. ( SELECT
    105. tomorrow_max_value as sl
    106. FROM
    107. hl_data_predict.kl_predict_data
    108. WHERE
    109. SUBSTR(create_date,1,10) = SUBSTR(NOW(),1,10) ORDER BY SUBSTR(create_date,1,19) desc LIMIT 0,1
    110. )
    111. WHERE SUBSTR(sj,1,10) = SUBSTR(NOW(),1,10) and jdmc = '玉龙雪山景区门票';
    112. -- 添加今日检票数
    113. UPDATE ylxs_jqpwfx_mpydqktj
    114. SET jrjp = (
    115. SELECT
    116. SUM(jps) as jrjp
    117. FROM
    118. ylxs_dp.ylxs_jqpwfx_jdjpsdqs
    119. WHERE
    120. SUBSTR(sj,1,10) = SUBSTR(NOW(),1,10)
    121. and jdmc = '玉龙雪山'
    122. and jrzr = '1'
    123. )
    124. WHERE
    125. SUBSTR( sj, 1, 10 ) = SUBSTR( NOW(), 1, 10 )
    126. AND jdmc = '玉龙雪山景区门票';
    127. -- 添加今日未检票数
    128. UPDATE ylxs_jqpwfx_mpydqktj
    129. SET jrwj = ABS((
    130. SELECT
    131. COUNT(distinct touristCardNum) as sl
    132. FROM
    133. ylxs_hldc.t_dm_ticket_data
    134. WHERE
    135. SUBSTR(travelTime,1,10) = SUBSTR(NOW(),1,10)
    136. and scenicAreaName in ('冰川公园索道','云杉坪索道','印象丽江','牦牛坪索道','玉龙雪山景区门票')
    137. )-( SELECT
    138. SUM(jps) as jrjp
    139. FROM
    140. ylxs_dp.ylxs_jqpwfx_jdjpsdqs
    141. WHERE
    142. SUBSTR(sj,1,10) = SUBSTR(NOW(),1,10)
    143. and jdmc = '玉龙雪山'
    144. and jrzr = '1'
    145. ))
    146. WHERE
    147. SUBSTR( sj, 1, 10 ) = SUBSTR( NOW(), 1, 10 )
    148. AND jdmc = '玉龙雪山景区门票';
    149. END

     (4)在计划中设置事件执行规律

     

     注意:一定要写事件开始时间,且要精确到秒钟,否则无法保存该事件

    4、适用场景

    1、对于某些需要实时更新的数据,我们可以针对业务需求,单独建立一张结果表,编写事件定时更新,而不需要在接口请求中,每次去获取原表的最新数据。

    2、对于数据量特别大的表,哪怕加了索引,优化了sql,执行查询时间也严重偏长,我们可以以不同的时间维度分组,或者根据业务需求分租,定时推送到结果表中,同时,还可以设置数据库的读、取时间,来防止事件执行时间过长,数据库连接宕机

    1. oracle.net.READ_TIMEOUT=30000
    2. oracle.jdbc.ReadTimeout=30000

    5、通过列表查看时间最近一次执行时间

    6、查看事件是否开启

    SHOW VARIABLES LIKE 'event_scheduler'

    7、设置当前事件开启

    SET GLOBAL event_scheduler = 1

    8、好处

    当我们把一些业务逻辑通过编写事件、搭建结果表、sql函数等方式放在sql中去实现后,业务代码中执行,可以一定程度上减少接口的响应时间,提升项目性能以及用户体验感。

     9、防止数据库连接池耗尽的超时时间设置

    中文社区 (MOSC)

    中间件 (MOSC)

    0 Replies

    Last updated on March 27, 2015

    1 WebLogic Server自动回收使用超时的连接

    WebLogic Server 数据源的配置项: Admin Console -> JDBC Data Source: Configuration: Connection Pool -> "Inactive Connection Timeout"

    设置为大于0的秒数。WebLogic Server 将自动检测出使用时间过长的连接并强制收回, 输出警告信息到日志文件:BEA-001153: Forcibly releasing inactive connection. 占用时间过长的连接分为两种情形: 一是应用程序有bug, 没有调用javax.sql.Connection.close()归还连接, 另一种是长时间执行SQL一直没有完成。

    2 设置 JDBC SQL Statement Timeout

    WebLogic Server 数据源的配置项: Admin Console -> JDBC Data Source: Configuration: Connection Pool -> "Statement Timeout"

    设置为大于0的秒数。当SQL语句执行时间过长, 超过这个时间时Oracle JDBC驱动程序会抛出java.sql.SQLException: ORA-01013: user requested cancel of current operation. 这个特性依赖于JDBC驱动程序如何实现java.sql.Statement.setQueryTimeout()方法。

    3 JDBC driver Socket Timeout

    WebLogic Server 数据源的配置项: Admin Console -> JDBC Data Source: Configuration: Connection Pool, 在"Properties"输入框输入属性名字和值,每行一个。例如 Oracle JDBC driver 支持的:

    oracle.net.READ_TIMEOUT=30000

    oracle.jdbc.ReadTimeout=30000

    当 Socket 操作因为网络问题或者数据库服务器端没有回应导致超过这个时间, Oracle JDBC driver 会抛出 java.sql.SQLRecoverableException:To view full details, sign in with your My Oracle Support Community account.

    Don't have a My Oracle Support

    Community account? Click to get started!
     

  • 相关阅读:
    Chapter 5 决策树和随机森林实践
    【django问题集】django.db.utils.OperationalError: (1040, ‘Too many connections‘)
    [Google DeepMind] LARGE LANGUAGE MODELS AS OPTIMIZERS
    学习CentOS7系统安装nginx环境,以及相关配置命令
    VScode为什么选择了Electron,而不是QT?
    GBase8s数据库select有ORDER BY 子句6
    Rust逆向学习 (1)
    2022-05-05 mybatis-plus 批量插入修改操作
    HazelEngine 学习记录 - Profiling
    Ps:选框工具
  • 原文地址:https://blog.csdn.net/Zyw907155124/article/details/126037464