我正在使用一個接口在WinForm應用程序中顯示來自DBF文件的數據。 我開始使用OdbcConnection。儘管有效,但由於Visual FoxPro驅動程序的某些限制(子查詢不受支持),因此我使用OLEDB(VFPOLEDB)打開。現在我可以運行復雜的查詢,但是出現了新的問題需要解決。問題是這些查詢太慢了。比預期的要慢100倍。使用OLEDB(VFPOLEDB)在DBF文件上運行查詢太慢
下面是演示代碼。 有一個DBF表「PROD」。索引字段PRICE_N用於查詢的Where子句中。該表位於運行應用程序的同一臺PC上。正如您所看到的,通過ODBC(Microsoft Visual FoxPro Driver)和OLEDB(VFPOLEDB)運行查詢所花費的時間差別很大。
TimeSpan timeSpanODBC;
DateTime timeODBC = DateTime.Now;
OdbcConnection odbcConnection = new OdbcConnection(@"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=C:\Users\Vakshul\Documents\dbfs;Exclusive=No;Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;");
odbcConnection.Open();
OdbcCommand odbcCommand = new OdbcCommand("SELECT utk_ved FROM prod WHERE (price_n='641857')", odbcConnection);
odbcCommand.ExecuteScalar();
timeSpanODBC = DateTime.Now - timeODBC;
double timeOdbcEqual = timeSpanODBC.TotalMilliseconds;
System.Console.WriteLine("Time spent via ODBC(milliseconds) using '=' to compare - {0}", timeOdbcEqual.ToString());
timeODBC = DateTime.Now;
odbcCommand = new OdbcCommand("SELECT utk_ved FROM prod WHERE (price_n like'641857')", odbcConnection);
odbcCommand.ExecuteScalar();
timeSpanODBC = DateTime.Now - timeODBC;
double timeOdbcLike = timeSpanODBC.TotalMilliseconds;
System.Console.WriteLine("Time spent via ODBC(milliseconds) using 'Like' to compare - {0}", timeOdbcLike.ToString());
TimeSpan timeSpanOLEDB;
DateTime timeOLEDB = DateTime.Now;
OleDbConnection oleDbCon = new OleDbConnection(@"Provider=VFPOLEDB.1;Data Source=C:\Users\Vakshul\Documents\dbfs;Collating Sequence=MACHINE;Mode=Read");
oleDbCon.Open();
OleDbCommand oleDbcommand = new OleDbCommand("SELECT utk_ved FROM prod WHERE (price_n = '641857')", oleDbCon);
oleDbcommand.ExecuteScalar();
timeSpanOLEDB = DateTime.Now - timeOLEDB;
double timeOLEDBEqual = timeSpanOLEDB.TotalMilliseconds;
System.Console.WriteLine("Time spent via OLEDB(milliseconds) using '=' to compare - {0}", timeOLEDBEqual.ToString());
timeOLEDB = DateTime.Now;
oleDbcommand = new OleDbCommand("SELECT utk_ved FROM prod WHERE (price_n like '641857')", oleDbCon);
oleDbcommand.ExecuteScalar();
timeSpanOLEDB = DateTime.Now - timeOLEDB;
double timeOLEDLike = timeSpanOLEDB.TotalMilliseconds;
System.Console.WriteLine("Time spent via OLEDB(milliseconds) using 'Like' to compare - {0}", timeOLEDLike.ToString());
System.Console.WriteLine("ODBC is faster than OLEDB {0} times using '=' to compare", Math.Round(timeOLEDBEqual/timeOdbcEqual, 0));
System.Console.WriteLine("ODBC is faster than OLEDB {0} times using 'Like' to compare", Math.Round(timeOLEDBEqual/timeOdbcEqual, 0));
控制檯,在第一次運行後:
Time spent via ODBC(milliseconds) using '=' to compare - 5,0006
Time spent via ODBC(milliseconds) using 'Like' to compare - 3,5005
Time spent via OLEDB(milliseconds) using '=' to compare - 1630,207
Time spent via OLEDB(milliseconds) using 'Like' to compare - 1755,2228
ODBC is faster than OLEDB 326 times using '=' to compare
ODBC is faster than OLEDB 326 times using 'Like' to compare
Console, after the second run:
Time spent via ODBC(milliseconds) using '=' to compare - 4,5006
Time spent via ODBC(milliseconds) using 'Like' to compare - 4,5005
Time spent via OLEDB(milliseconds) using '=' to compare - 1526,1938
Time spent via OLEDB(milliseconds) using 'Like' to compare - 1595,2026
ODBC is faster than OLEDB 339 times using '=' to compare
ODBC is faster than OLEDB 339 times using 'Like' to compare
Console, after the third run:
Time spent via ODBC(milliseconds) using '=' to compare - 4,0005
Time spent via ODBC(milliseconds) using 'Like' to compare - 3,0004
Time spent via OLEDB(milliseconds) using '=' to compare - 1449,184
Time spent via OLEDB(milliseconds) using 'Like' to compare - 1451,1843
ODBC is faster than OLEDB 362 times using '=' to compare
ODBC is faster than OLEDB 362 times using 'Like' to compare
Console, after the fourth run:
Time spent via ODBC(milliseconds) using '=' to compare - 3,5004
Time spent via ODBC(milliseconds) using 'Like' to compare - 4,5006
Time spent via OLEDB(milliseconds) using '=' to compare - 1475,6874
Time spent via OLEDB(milliseconds) using 'Like' to compare - 1621,2059
ODBC is faster than OLEDB 422 times using '=' to compare
ODBC is faster than OLEDB 422 times using 'Like' to compare
在此示例中的索引字段PRICE_N被包括在其中查詢的子句。 我還測試了相同的查詢,包括Where子句中的非索引字段而不是索引字段。結果是相同的〜1400 - 1600毫秒。 我的印象是,在OLEDB(VFPOLEDB)的情況下不使用索引。 我對結果不滿意,我需要使用索引。
如果有人有任何建議,我將非常感激。
你見過[本條](HTTP://www.vfug。組織/通訊/ oledbproviderwaystochangesettings.htm)? – stuartd
謝謝,@stuartd。我以前沒有見過,所以我已經仔細閱讀。 –
謝謝@stuartd。我以前沒有見過,所以我已經仔細閱讀。我試圖使用TABLEVALIDATE = 0但無濟於事。什麼也沒有變。速度如此之低以至於很難得到讚賞。我猜測索引不被使用。但爲什麼呢?我甚至部署了一個虛擬機,在那裏安裝了所有需要的SW,但結果如下:( –