MySQL 导出索引
我确信以前肯定有人问过这个问题,但我找不到它:
我有一个正在不断开发的 CMS。我已经使用 CMS 建立了许多网站,因此它们存在许多数据库。
我想从开发数据库中提取索引并将其应用到生产数据库。有没有一种简单的方法可以将索引提取为 SQL?
大致如下:
create index idx1 on one (lft, rght);
create index idx1 on two (alias);
create index acos_idx3 on three (model, related_id);
I'm sure it must have been asked before, but I can't find it:
I have a CMS that is under constant development. I have built a number of websites using the CMS, so their are a number of databases in existence.
I want to extract the indices from the development db and apply them to the production dbs. Is there an easy way to extract indices as SQL?
Something along the lines of:
create index idx1 on one (lft, rght);
create index idx1 on two (alias);
create index acos_idx3 on three (model, related_id);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从 INFORMATION_SCHEMA 数据库中提取索引,然后将它们添加到另一个数据库中,但这并不容易。
举个例子(来自用于部署的存储过程的代码),这会添加一个唯一键(如果尚不存在):
您基本上可以在 INFORMATION_SCHEMA 中找到所需的任何内容。我相信您可以编写代码来动态检查所有这些索引,但我不确定这对您来说是否容易。
更新:
您还可以使用
show index from database.table
,正如您在MaasSql的答案提供的链接中看到的那样。然后循环遍历结果并添加每个索引(如果数据库中没有)。或者你可以尝试这个:
You can extract indices from INFORMATION_SCHEMA database, then add them on another database, but it's not quite easy.
To give you an example (code from a stored procedure used for deployment), this adds an unique key if it's not already there:
You can basically find whatever you need in INFORMATION_SCHEMA. I believe you can write code to dynamically check for all these indices, but I'm not sure if it's easy for you.
UPDATE:
You can also use
show index from database.table
, as you can see at the link provided by MaasSql's answer. Then loop throuth the results and add each index if it's not in the database.Or you can try this:
我不经常使用 My SQL,但据我记得,备份是开放文本 DDL 语句。似乎这意味着每个备份的开始将包含所有表,然后是所有索引。
也许这个带有一点表循环的命令会让你想要你想要的: My SQL 5.0 参考手册。
祝你好运!
I don't work with My SQL a whole lot, but as I remember, the backups were open text DDL statements. Seems like that would mean that the beginning of each backup would contain all of the tables, then all of the indexes.?
Maybe this command with a bit of table looping will get you want you want: My SQL 5.0 Reference Manual.
Good LUCK!
好吧,我不得不转向其他事情,这就是我所得到的(就在 Ceteras 发布他的更新之前!)。目前的问题是 CREATE INDEX IF NOT EXISTS 无效,所以我需要找到另一种方法来做到这一点。可能会在本周末重新讨论这个问题。
}
Well, I had to move onto something else and this is as far as I got (just before Ceteras published his update!). The problem at the moment is that CREATE INDEX IF NOT EXISTS is invalid, so I need to find another way to do it. Might get back on this at the end of the week.
}