[英]How to show composite index in mysql?
我正在编写一个 WordPress 插件,我想检查以确保在激活插件时将复合索引放在 postmeta.post_id 和 postmeta.meta_key 列上,并警告用户创建此索引,如果它有还没有。 这是我在 postmeta.post_id 列上的单个索引:
$meta_post_id_indices = $db->query("
SHOW INDEX
FROM {$wpdb->postmeta}
WHERE column_name = 'post_id'
");
if (!$meta_post_id_indices) {
echo "You might consider creating an index on the {$wpdb->postmeta}.post_id column. ALTER TABLE {$wpdb->postmeta} ADD index(post_id);";
}
如何为组合(post_id,meta_key)索引实现类似的代码?
这是我的做法:
select index_name, group_concat(column_name order by seq_in_index) as columns
from information_schema.statistics
where (table_schema, table_name) = ('test', 'postmeta')
group by index_name having columns='post_id,meta_key';
(模式名称“test”只是一个示例;您应该将“test”替换为 wordpress 模式的名称。)
MySQL 中的大多数 SHOW 语句实际上是作为对 information_schema 中的视图的查询来实现的,因此这只是直接访问源数据,这使您可以更灵活地查询它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.