c # / NET SQLite -. REINDEX não está funcionando?
-
19-09-2019 - |
Pergunta
Eu estou tentando reindexar uma tabela em um banco de dados simples que eu criei usando SQLite.NET e VS2008. Eu preciso reindexar tabelas após cada comando DELETE e aqui está o fragmento de código que tenho escrito (que não funciona):
SQLiteCommand currentCommand;
String tempString = "REINDEX tf_questions";
//String tempString = "REINDEX [main].tf_questions";
//String tempString = "REINDEX main.tf_questions";
currentCommand = new SQLiteCommand(myConnection);
currentCommand.CommandText = tempString;
currentCommand.ExecuteNonQuery()
Quando executado dentro do meu programa, o código não produz erros, mas também não reindexar a tabela "tf_questions". No exemplo acima, você também vai ver as outras seqüências de consulta que eu tentei que também não funcionam.
Por favor, ajuda,
Graças
Solução
Eu descobri uma solução para o meu problema. Considere o seguinte código:
SQLiteCommand currentCommand;
String tempString;
String currentTableName = "tf_questions";
DataTable currentDataTable;
SQLiteDataAdapter myDataAdapter;
currentDataTable = new DataTable();
myDataAdapter = new SQLiteDataAdapter("SELECT * FROM " + currentTableName, myConnection);
myDataAdapter.Fill(currentDataTable);
//"tf_questions" is the name of the table
//"question_id" is the name of the primary key column in "tf_questions" (set to auto inc.)
//"myConnection" is and already open SQLiteConnection pointing to the db file
for (int i = currentDataTable.Rows.Count-1; i >=0 ; i--)
{
currentCommand = new SQLiteCommand(myConnection);
tempString = "UPDATE "+ currentTableName +"\nSET question_id=\'"+(i+1)+"\'\nWHERE (question_id=\'" +
currentDataTable.Rows[i][currentDataTable.Columns.IndexOf("question_id")]+"\')";
currentCommand.CommandText = tempString;
if( currentCommand.ExecuteNonQuery() < 1 )
{
throw new Exception("There was an error executing the REINDEX protion of the code...");
}
}
Esse código funciona, embora eu teria gostado de ter usado o built-in comando SQL REINDEX.
Outras dicas
Se você está fazendo isso por causa do desempenho, considere isto resposta :
REINDEX não ajuda. REINDEX é única necessário se você mudar um agrupamento sequência.
Depois de um monte de inserções e exclusões, às vezes você pode obter um pouco melhor desempenho, fazendo um vácuo. VÁCUO melhora a localização de referência ligeiramente.