Home Database Mysql Tutorial 用Mysql存储过程迁移数据

用Mysql存储过程迁移数据

Jun 07, 2016 pm 03:40 PM
mysql storage data migrate process

今天有一个需求是迁移tag的数据,之前写的存储过程到现在都忘记了,从新再写一个,并在这里纪录一下,防止自己下次还忘记 首先是修改一下Mysql的配置 大家可以看下 这是我们老大的测试结果 SET GLOBAL max_allowed_packet=1024*1024*1024;SET GLOBAL key_buf

今天有一个需求是迁移tag的数据,之前写的存储过程到现在都忘记了,从新再写一个,并在这里纪录一下,防止自己下次还忘记

首先是修改一下Mysql的配置

大家可以看下

这是我们老大的测试结果

<code>SET GLOBAL max_allowed_packet=1024*1024*1024;
SET GLOBAL key_buffer_size=1024*1024*1024;
SET GLOBAL tmp_table_size = 512*1024*1024;

SET SESSION myisam_sort_buffer_size = 512*1024*1024;
SET SESSION read_buffer_size = 128*1024*1024;
SET GLOBAL myisam_max_sort_file_size = 100*1024*1024*1024;</code>
Copy after login

下面选择数据库

<code>use db_database;</code>
Copy after login

如果有存储过程,先删除存储过程,(官方的中文翻译叫存储程序,感觉诡异)

<code>drop procedure 存储过程名称;</code>
Copy after login

好了,下面重头戏,把delimiter 改成 // 为开始和结束,并创建存储过程

<code>delimiter //
CREATE PROCEDURE 存储过程的名称();
BEGIN</code>
Copy after login

好了下面我们来定义变量,这个大家都能看懂吧!

<code>DECLARE rid INT;
DECLARE rtags VARCHAR(225);
DECLARE lasttime datetime;
DECLARE firsttime datetime;
DECLARE done, duplicate, expcount INT DEFAULT 0;</code>
Copy after login

下面这个比较特殊,是定义游标(或者叫指针,但是官网上面叫光标),这个需求要获取id,来操作后面得数据

<code>DECLARE cur CURSOR FOR SELECT
id
FROM 表名
ORDER BY id ASC;</code>
Copy after login

然后设置各种状态下的设置的变量的值

Mysql各种状态都在这里了

当sql错误状态是02000的时候 done这个变量为1

<code>DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;</code>
Copy after login

同上

<code>DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET duplicate = 1;</code>
Copy after login

设置变量

<code>SET lasttime = now();
SET firsttime = now();</code>
Copy after login

下面我们来遍历游标

打开游标

<code>OPEN cur;</code>
Copy after login

遍历

<code>REPEAT
FETCH cur INTO rid;

if not done then

SET duplicate = 0;
UPDATE 另一张表
SET keywords = 数据是啥
WHERE id = rid;</code>
Copy after login

好了,上面就是写你的sql,下面是100条的时候,打印出来执行时间和条数

<code>SET expcount = expcount + 1;
IF expcount % 100 = 0 then
  SELECT expcount,now() - lasttime;
  SET lasttime = now();
END IF;</code>
Copy after login

直到没有数据,遍历结束

<code>end if;

UNTIL done END REPEAT;</code>
Copy after login

关闭游标

<code>CLOSE cur;</code>
Copy after login

最后结束存储过程

<code>END;
//
delimiter ;</code>
Copy after login

以上就是做的存储过程的数据迁移的部分,这个写的比较简单,不过以后有复杂的可以直接修改修改就可以用了!

Statement of this Website
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

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to transfer WeChat chat history to another mobile phone How to transfer WeChat chat history to another mobile phone May 08, 2024 am 11:20 AM

1. On the old device, click "Me" → "Settings" → "Chat" → "Chat History Migration and Backup" → "Migrate". 2. Select the target platform device to be migrated, select the chat records to be migrated, and click "Start". 3. Log in with the same WeChat account on the new device and scan the QR code to start chat record migration.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

See all articles