mysql 通过批处理与 CLI 或 GUI 创建索引
mysql create index from batch vs. CLI or GUI
我正在尝试将 mysql 中的现有(并已填充)table 索引为 PHP 中 ETL 脚本的一部分。我有两个索引语句(称之为 create_index.sql
):
create index my_index_1 on my_table (my_col_1);
create fulltext index my_index_2 on my_table (my_col_2 asc, my_col_3 asc);
这是通过 PHP 自动完成的,运行 填充后通过加载 SQL(文本)文件并将其作为批处理执行:
$conn = new mysqli('my_host', 'my_user', 'my_pass', 'my_db');
$result = $conn->query(file_get_contents('create_index.sql');
if (false === $result)
error_log($conn->error);
按上面列出的方式执行时(通过 PHP),我收到一条标准 'You have an error in your SQL syntax' 消息。
在MySQLWorkbench中执行时,两个create index
语句,完全一样,运行没有错误。
有人可以指出 GUI 在我假设的语句之间所做的不同的方向,这会导致相同语句的不同结果吗?我是不是找错树了?
您需要分别执行文件中的每个SQL 语句。语句的拆分可能更优雅,但根据您在问题中显示的内容,这应该可行:
$conn = new mysqli('my_host', 'my_user', 'my_pass', 'my_db');
$sql_file_text = file_get_contents('create_index.sql');
$sql = explode(';',$sql_file_text);
foreach($sql as $key => $sql_statement) {
if($sql_statement != '') {
$result = $conn->query($sql_statement);
if (false === $result) {
error_log($conn->error);
}
}
}
我正在尝试将 mysql 中的现有(并已填充)table 索引为 PHP 中 ETL 脚本的一部分。我有两个索引语句(称之为 create_index.sql
):
create index my_index_1 on my_table (my_col_1);
create fulltext index my_index_2 on my_table (my_col_2 asc, my_col_3 asc);
这是通过 PHP 自动完成的,运行 填充后通过加载 SQL(文本)文件并将其作为批处理执行:
$conn = new mysqli('my_host', 'my_user', 'my_pass', 'my_db');
$result = $conn->query(file_get_contents('create_index.sql');
if (false === $result)
error_log($conn->error);
按上面列出的方式执行时(通过 PHP),我收到一条标准 'You have an error in your SQL syntax' 消息。
在MySQLWorkbench中执行时,两个create index
语句,完全一样,运行没有错误。
有人可以指出 GUI 在我假设的语句之间所做的不同的方向,这会导致相同语句的不同结果吗?我是不是找错树了?
您需要分别执行文件中的每个SQL 语句。语句的拆分可能更优雅,但根据您在问题中显示的内容,这应该可行:
$conn = new mysqli('my_host', 'my_user', 'my_pass', 'my_db');
$sql_file_text = file_get_contents('create_index.sql');
$sql = explode(';',$sql_file_text);
foreach($sql as $key => $sql_statement) {
if($sql_statement != '') {
$result = $conn->query($sql_statement);
if (false === $result) {
error_log($conn->error);
}
}
}