MySQL 导出索引

发布于 2024-09-07 15:07:50 字数 307 浏览 2 评论 0原文

我确信以前肯定有人问过这个问题,但我找不到它:

我有一个正在不断开发的 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 技术交流群。

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

发布评论

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

评论(3

可可 2024-09-14 15:07:50

您可以从 INFORMATION_SCHEMA 数据库中提取索引,然后将它们添加到另一个数据库中,但这并不容易。

举个例子(来自用于部署的存储过程的代码),这会添加一个唯一键(如果尚不存在):

if not exists (select * from information_schema.KEY_COLUMN_USAGE 
                where table_schema = 'database_name' 
                and table_name='your_table'
                and constraint_name ='key_name')
then
    alter table your_table add unique key `key_name` ('column_name');
end if;

您基本上可以在 INFORMATION_SCHEMA 中找到所需的任何内容。我相信您可以编写代码来动态检查所有这些索引,但我不确定这对您来说是否容易。

更新:

您还可以使用show index from database.table,正如您在MaasSql的答案提供的链接中看到的那样。然后循环遍历结果并添加每个索引(如果数据库中没有)。

或者你可以尝试这个:

if not exists (select * from information_schema.STATISTICS
where table_schema = 'database_name' 
and table_name='table_name'
and index_name ='key_name')
then 
...
end if;

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:

if not exists (select * from information_schema.KEY_COLUMN_USAGE 
                where table_schema = 'database_name' 
                and table_name='your_table'
                and constraint_name ='key_name')
then
    alter table your_table add unique key `key_name` ('column_name');
end if;

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:

if not exists (select * from information_schema.STATISTICS
where table_schema = 'database_name' 
and table_name='table_name'
and index_name ='key_name')
then 
...
end if;
柳絮泡泡 2024-09-14 15:07:50

我不经常使用 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!

靖瑶 2024-09-14 15:07:50

好吧,我不得不转向其他事情,这就是我所得到的(就在 Ceteras 发布他的更新之前!)。目前的问题是 CREATE INDEX IF NOT EXISTS 无效,所以我需要找到另一种方法来做到这一点。可能会在本周末重新讨论这个问题。

$query="SHOW TABLES";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
  $idxQ = "SHOW INDEXES FROM {$row[0]} IN $database;";
  $idxR = mysql_query($idxQ);

  while($idxRow = mysql_fetch_object($idxR))
  {
      //basic query: CREATE INDEX indexName ON tableName (columnName);
      $indexName = $idxRow->Key_name;
      if($indexName=='PRIMARY') continue;
      $tableName = $idxRow->Table;
      $columnName = $idxRow->Column_name;
      $idxCreateQ = "CREATE INDEX IF NOT EXISTS $indexName ON $tableName ($columnName);";
      echo $idxCreateQ;
  }

}

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.

$query="SHOW TABLES";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
  $idxQ = "SHOW INDEXES FROM {$row[0]} IN $database;";
  $idxR = mysql_query($idxQ);

  while($idxRow = mysql_fetch_object($idxR))
  {
      //basic query: CREATE INDEX indexName ON tableName (columnName);
      $indexName = $idxRow->Key_name;
      if($indexName=='PRIMARY') continue;
      $tableName = $idxRow->Table;
      $columnName = $idxRow->Column_name;
      $idxCreateQ = "CREATE INDEX IF NOT EXISTS $indexName ON $tableName ($columnName);";
      echo $idxCreateQ;
  }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文