簡體   English   中英

如何在 mysql 中顯示復合索引?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM