add_clean_index( string $table, string $index )

Adds an index to a specified table.


Parameters Parameters

$table

(string) (Required) Database table name.

$index

(string) (Required) Database table index column.


Top ↑

Return Return

(true) True, when done with execution.



Top ↑

Source Source

File: wp-admin/includes/upgrade.php

function add_clean_index( $table, $index ) {
	global $wpdb;
	drop_index( $table, $index );
	$wpdb->query( "ALTER TABLE `$table` ADD INDEX ( `$index` )" );
	return true;
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.1 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by MakeWebBetter

    Sometimes after a table has been created in a database, we find that it is advantageous to add an index to that table to speed up queries involving this table.

    function wpdocs_mwb_make_fetch_fast()
    {
        global $wpdb;
        // Add some Clean up indices
        add_clean_index( $wpdb->posts, 'post_name' );
        add_clean_index( $wpdb->categories, 'category_nicename' );
        add_clean_index( $wpdb->comments, 'comment_approved' );
        add_clean_index( $wpdb->posts, 'post_status' );
    }
    

You must log in before being able to contribute a note or feedback.