Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use event to divide tables automatically in MySQL

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how MySQL uses event to automatically divide tables. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The new unit needs a sub-table to deal with a large log table.

First, turn on the event function.

SET GLOBAL event_scheduler = 1

Then automatically create a new table based on time and change the view to the query of the new table.

-- simulated log table

Create table T1 (id int,log varchar (30))

-- simulated view

Create view v_log as select * from T1

Delimiter $$

CREATE EVENT test_event

ON SCHEDULE

EVERY 1 second

STARTS str_to_date ('2014-04-14 20, 14, 20, 12, 10, 12, 14, 14, 20, 10, 12, 14, 14, 20, 10, 14, 14, 20, 14, 14, 04, 14, 20, 14, 04, 14, 20, 14, 04, 14, 20, 14, 14, 04, 14, 20, 14, 14, 04, 14, 20, 14, 20, 14, 04, 14, 20, 14, 20, 14, 04, 14, 20, 14, 20, 14, 04, 14, 20, 14, 20, 14, 20, 14, 04, 14, 20, 14, 04, 14, 20, 14, 04, 14, 20, 14, 04, 14, 20, 14, 04, 14, 20, 14, 04, 14, 20, 14, 04, 14, 20, 14, 04, 14, 20, 14,

COMMENT 'create a new log table based on the date and change the view to a query for the new log table'

DO

BEGIN

Set @ cur_date=replace (curtime (),':','_')

Set @ sqltext=concat ("create table log_", @ cur_date, "(id int);")

PREPARE c_tab_stat from @ sqltext

Execute c_tab_stat

Set @ sqltext=concat ("create or replace view v_log as select * from log_", @ cur_date, ";")

Select @ sqltext

PREPARE a_view_stat from @ sqltext

Execute a_view_stat

END $$

Delimiter

Thank you for reading! This is the end of this article on "how to use event for automatic sub-table in MySQL". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report