WIP: Partial support for prefix indexing

[Work In Progress] MySQL performs prefix indexing on a few columns.
PostgreSQL lacks support for prefix indexing (although it does support
expression indexing, which can be used to similar effect), which results
in the following error message:

Error running :
ALTER TABLE wp_comments ADD KEY comment_author_email (comment_author_email(10))
---- converted to ----
CREATE INDEX wp_comments_comment_author_email ON wp_comments (comment_author_email(10)
----> ERROR:  syntax error at end of input
LINE 1: ...omment_author_email ON wp_comments (comment_author_email(10)

Since the prefix indexing does not currently appear to be advantageous
(other than working around MySQL row index size limitations), convert
these to full-column indexes.

FIXME:  SHOW INDEX needs to be updated so that it returns the requested
prefix length in order to avoid attempted index recreation during schema
upgrades.  This could be accomplished by storing the length as a comment
or some other ancillary metadata.  This is not implemented yet.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
This commit is contained in:
Kevin Locke
2015-06-03 01:21:16 -06:00
parent b4b346f694
commit debe87c368

View File

@ -175,13 +175,18 @@ WHERE '.$where : '').';';
$newq .= " NOT NULL";
$sql = $newq;
}
$pattern = '/ALTER TABLE\s+(\w+)\s+ADD (UNIQUE |)KEY\s+([^\s]+)\s+\(([^\)]+)\)/';
$pattern = '/ALTER TABLE\s+(\w+)\s+ADD (UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[^\(\)]+|\([^\(\)]+\))+)\)/';
if( 1 === preg_match( $pattern, $sql, $matches))
{
$table = $matches[1];
$unique = $matches[2];
$index = $matches[3];
$columns = $matches[4];
// Remove prefix indexing
// Rarely used and apparently unnecessary for current uses
$columns = preg_replace( '/\([^\)]*\)/', '', $columns);
// Workaround for index name duplicate
$index = $table.'_'.$index;
$sql = "CREATE {$unique}INDEX $index ON $table ($columns)";