MySql,如何将索引从开发数据库导出到生产数据库?

发布于 2024-10-12 18:53:45 字数 150 浏览 4 评论 0原文

我一直在研究我的开发数据库并调整了它的性能。

然而,令我惊讶的是,我找不到将索引导出到生产数据库的方法。

我认为有一个简单的方法可以做到这一点。我不想替换生产数据库中的数据。

主要问题是我看不到索引中的排序,因此甚至手动执行排序也很困难。

I've been working on my development database and have tweaked its performance.

However, to my surprise I can't find a way to export the indexes to my production database.

I thought there would be an easy way to do this. I don't want to replace the data in my production database.

The main problem is that I can't see sorting in the indexes so its going to be difficult to even do it manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

初雪 2024-10-19 18:53:45

也许您的意思是“如何在我的(现有)实时数据库上重新创建我的开发索引”?

如果是这样,我认为您正在寻找的 SQL 命令是;

显示创建表 {表名};

ALTER TABLE ADD INDEX {index_name} (col1, col2)

ALTER TABLE DROP INDEX {index_name}

您可以从“SHOW CREATE TABLE”输出中复制“KEY”和“CONSTRAINT”行,并将其放回“ALTER TABLE ADD INDEX” 。

dev mysql> SHOW CREATE TABLE city;
CREATE TABLE `city` (
  `id` smallint(4) unsigned NOT NULL auto_increment,
  `city` varchar(50) character set utf8 collate utf8_bin NOT NULL default '',
  `region_id` smallint(4) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `region_idx` (region_id),
  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`region_id`) REFERENCES `region` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB;

live mysql> SHOW CREATE TABLE city;
CREATE TABLE `city` (
  `id` smallint(4) unsigned NOT NULL auto_increment,
  `city` varchar(50) character set utf8 collate utf8_bin NOT NULL default '',
  `region_id` smallint(4) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

live mysql> ALTER TABLE `city` ADD KEY `region_idx` (region_id);
live mysql> ALTER TABLE `city` ADD CONSTRAINT `city_ibfk_1` FOREIGN KEY (`region_id`) REFERENCES `region` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT;

希望这有帮助!

Perhaps you mean "How do I re-create my development indexes on my (existing) live database"?

If so, I think the SQL commands you're looking for are;

SHOW CREATE TABLE {tablename};

ALTER TABLE ADD INDEX {index_name} (col1, col2)

ALTER TABLE DROP INDEX {index_name}

You can copy the "KEY" and "CONSTRAINT" rows from "SHOW CREATE TABLE" output and put it back in the "ALTER TABLE ADD INDEX".

dev mysql> SHOW CREATE TABLE city;
CREATE TABLE `city` (
  `id` smallint(4) unsigned NOT NULL auto_increment,
  `city` varchar(50) character set utf8 collate utf8_bin NOT NULL default '',
  `region_id` smallint(4) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `region_idx` (region_id),
  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`region_id`) REFERENCES `region` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB;

live mysql> SHOW CREATE TABLE city;
CREATE TABLE `city` (
  `id` smallint(4) unsigned NOT NULL auto_increment,
  `city` varchar(50) character set utf8 collate utf8_bin NOT NULL default '',
  `region_id` smallint(4) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

live mysql> ALTER TABLE `city` ADD KEY `region_idx` (region_id);
live mysql> ALTER TABLE `city` ADD CONSTRAINT `city_ibfk_1` FOREIGN KEY (`region_id`) REFERENCES `region` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT;

Hope this helps!

ぇ气 2024-10-19 18:53:45

扩展@origo 的答案。有一种情况我需要提取一堆索引的 DDL。这个脚本完成了这项工作。

来源:https://rogerpadilla.wordpress.com/2008/12/ 02/mysql-export-indexes/

SELECT
CONCAT(
'ALTER TABLE ' ,
TABLE_NAME,
' ',
'ADD ',
IF(NON_UNIQUE = 1,
CASE UPPER(INDEX_TYPE)
WHEN 'FULLTEXT' THEN 'FULLTEXT INDEX'
WHEN 'SPATIAL' THEN 'SPATIAL INDEX'
ELSE CONCAT('INDEX ',
INDEX_NAME,
' USING ',
INDEX_TYPE
)
END,
IF(UPPER(INDEX_NAME) = 'PRIMARY',
CONCAT('PRIMARY KEY USING ',
INDEX_TYPE
),
CONCAT('UNIQUE INDEX ',
INDEX_NAME,
' USING ',
INDEX_TYPE
)
)
),
'(',
GROUP_CONCAT(
DISTINCT
CONCAT('', COLUMN_NAME, '')
ORDER BY SEQ_IN_INDEX ASC
SEPARATOR ', '
),
');'
) AS 'Show_Add_Indexes'
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = 'PLEASE CHANGE HERE'
GROUP BY TABLE_NAME, INDEX_NAME
ORDER BY TABLE_NAME ASC, INDEX_NAME ASC;

Extending on @origo's answer. There is a case where i needed to extract the DDL for a bunch of indexes. This script does the job.

source : https://rogerpadilla.wordpress.com/2008/12/02/mysql-export-indexes/

SELECT
CONCAT(
'ALTER TABLE ' ,
TABLE_NAME,
' ',
'ADD ',
IF(NON_UNIQUE = 1,
CASE UPPER(INDEX_TYPE)
WHEN 'FULLTEXT' THEN 'FULLTEXT INDEX'
WHEN 'SPATIAL' THEN 'SPATIAL INDEX'
ELSE CONCAT('INDEX ',
INDEX_NAME,
' USING ',
INDEX_TYPE
)
END,
IF(UPPER(INDEX_NAME) = 'PRIMARY',
CONCAT('PRIMARY KEY USING ',
INDEX_TYPE
),
CONCAT('UNIQUE INDEX ',
INDEX_NAME,
' USING ',
INDEX_TYPE
)
)
),
'(',
GROUP_CONCAT(
DISTINCT
CONCAT('', COLUMN_NAME, '')
ORDER BY SEQ_IN_INDEX ASC
SEPARATOR ', '
),
');'
) AS 'Show_Add_Indexes'
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = 'PLEASE CHANGE HERE'
GROUP BY TABLE_NAME, INDEX_NAME
ORDER BY TABLE_NAME ASC, INDEX_NAME ASC;
離人涙 2024-10-19 18:53:45

首先,阅读此处有关如何使用导出 MySQL 索引的教程SQL 查询。此外:

  1. (使用 PHPMyAdmin 等),索引将重新生成。
  2. 如果可能,您可以将整个 MySQL 数据库文件夹的内容复制到生产数据库。这也能很快达到目的。 在 MySQL 文档中了解更多信息。

First, read the tutorial here about how-to Export MySQL Indexes using a SQL query. Further:

  1. If you do complete DUMP of your database and IMPORT it to another (using PHPMyAdmin, etc), the indexes will get regenerated.

  2. If possible, you can copy contents of your entire MySQL database folder to the production database. This will do the trick too, quickly. Read more here at MySQL docs.

剧终人散尽 2024-10-19 18:53:45

您可以使用以下命令进行转储

mysqldump -u [USERNAME] -p [DBNAME] | gzip > [/path_to_file/DBNAME].sql.gz

,索引将自动复制。

you can use the following command to take a dump

mysqldump -u [USERNAME] -p [DBNAME] | gzip > [/path_to_file/DBNAME].sql.gz

and indexes will be copied automatically.

爱的十字路口 2024-10-19 18:53:45

我相信您正在尝试导出索引本身,而不仅仅是在生产中重新生成它们的代码,对吧? (我之所以这样假设,是因为在大多数生产环境中生成这些索引的负载并不理想。)

如果性能不是您主要关心的问题,则 mysqldump 实用程序非常有用,而且我一直使用它。不过,如果您正在寻找一种非常快速的方法,我建议将实际的 InnoDB 文件从一个冷数据库复制到另一个数据库(假设它们是完全相同的 MySQL 版本,具有完全相同的配置和完全相同的预期)行为等)。如果系统之间存在任何差异,则此方法是危险的。

听起来,在您的情况下,您可能希望首先将良好的数据复制到测试环境中。我的开发周期通常遵循这种方法:DDL 通过编程从测试流向生产,DML 通过系统的实际使用从生产流向测试。

I believe you're trying to export the indexes themselves and not just the code to regenerate them in production, right? (I'm assuming this because the load of generating these indexes is not favorable in most production environments.)

The mysqldump utility is useful if performance isn't your main concern, and I use it all the time. If you're looking for a very fast method, though, I would suggest copying the actual InnoDB files from one cold database to the other (assuming they're exactly the same MySQL version with the exactly the same configuration and the exactly the same expected behavior, etc). This method is dangerous if there any differences between the systems.

It sounds like, in your situation, you might want to copy your good data to your testing environment first. My development cycle typically follows this approach: DDL flows from testing to production via programming, and DML flows from production to testing via actual use of the system.

浅语花开 2024-10-19 18:53:45

我还有具有相同数据库结构的开发和生产服务器。

我修改了它们的索引,所以我想将它们合并在一起。所以我需要用Notepad+来比较数据。

以下是如何导出所有表、所有数据库的索引以及如何过滤和比较它们:

--- Single table:
    SHOW INDEX FROM mydb.mytable;
    SHOW INDEX FROM mytable;

--- Multi tables, databases:
    USE information_schema;
    SELECT * FROM `statistics` ORDER BY TABLE_SCHEMA ASC, TABLE_NAME ASC, INDEX_NAME ASC;

现在,phpMyAdmin ->导出到 CSV-Excel:

添加标题行 ->删除所有列,但保留“database_name table_name 索引列值”

然后,按数据库过滤。

将数据库1中的所有内容复制到记事本+屏幕1,过滤excel数据库2,将所有内容复制到记事本+屏幕2 - >比较!

I also have development and production servers with the same database structure.

I modified indexes on both of them so I wanted to merge all together. So I needed to compare data with Notepad+.

Here is how you export indexes for all tables, all databases and how to filter and compare them:

--- Single table:
    SHOW INDEX FROM mydb.mytable;
    SHOW INDEX FROM mytable;

--- Multi tables, databases:
    USE information_schema;
    SELECT * FROM `statistics` ORDER BY TABLE_SCHEMA ASC, TABLE_NAME ASC, INDEX_NAME ASC;

Now, phpMyAdmin -> Export to CSV-Excel:

add a header row -> delete all columns but leave "database_name table_name index column value"

Then, filter by database.

Copy all from database1 to a notepad+ screen 1, filter excel database2, copy all to notepad+ screen 2 -> COMPARE!

嗼ふ静 2024-10-19 18:53:45

对于多个索引,我更喜欢每个表一个查询。
但不适用于具有多个列的组合索引。
不管怎样,试一试吧。

SELECT 
CONCAT
(concat("ALTER TABLE ",trim(TABLE_NAME)),
group_concat(
IF(INDEX_NAME="PRIMARY", 
concat(" ADD PRIMARY KEY (`", column_name,"`)"),
concat(" ADD INDEX `",INDEX_NAME,"` (`",column_name,"` ASC) ")
)),";") 
AS CMD
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA='your_schema'  
GROUP BY TABLE_NAME;

I prefer one query per table, for multiples indexes.
But not work for composed indexes with more than one column.
Anyway, give it a shot.

SELECT 
CONCAT
(concat("ALTER TABLE ",trim(TABLE_NAME)),
group_concat(
IF(INDEX_NAME="PRIMARY", 
concat(" ADD PRIMARY KEY (`", column_name,"`)"),
concat(" ADD INDEX `",INDEX_NAME,"` (`",column_name,"` ASC) ")
)),";") 
AS CMD
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA='your_schema'  
GROUP BY TABLE_NAME;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文