Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#vs Python对SQLite数据库的查询_C#_Python_Sqlite_System.data.sqlite - Fatal编程技术网

C#vs Python对SQLite数据库的查询

C#vs Python对SQLite数据库的查询,c#,python,sqlite,system.data.sqlite,C#,Python,Sqlite,System.data.sqlite,我有一个SQLite数据库,它有一个包含1800万行和24列的表。模式大致如下: Date (VARCHAR) CompanyName (VARCHAR) Amount (REAL) AggCode (VARCHAR) Level1 ... Level20 (VARCHAR) 我以两种方式查询表——首先使用Python脚本,然后使用C#函数,该函数通过ExcelDNA向Excel公开(最终我更喜欢使用Excel运行查询,因为某些查询将返回需要进一步操作的数据行) 我发现Python的性能通常比

我有一个SQLite数据库,它有一个包含1800万行和24列的表。模式大致如下:

Date (VARCHAR)
CompanyName (VARCHAR)
Amount (REAL)
AggCode (VARCHAR)
Level1 ... Level20 (VARCHAR)
我以两种方式查询表——首先使用Python脚本,然后使用C#函数,该函数通过ExcelDNA向Excel公开(最终我更喜欢使用Excel运行查询,因为某些查询将返回需要进一步操作的数据行)

我发现Python的性能通常比Excel插件高出3-5倍,我想知道我的代码是否有问题。下面的示例查询

query = "SELECT Sum(Amount) FROM my WHERE Level9='STIN_J' AND (AggCode='R_REAL' AND Date='05DEC2016')"
查询通常结合
WHERE
子句中的
Level9
Level5
AggCode
Date
CompanyName
字段运行。因此,除了原始表之外,我还配置了以下四个索引

CREATE INDEX idx1 on my(Level09, AggCode);
CREATE INDEX idx2 on my(Level05, AggCode);
CREATE INDEX idx3 on my(CompanyName, AggCode);
CREATE INDEX idx4 on my(Date, AggCode);
这是运行查询的示例Python代码

import sqlite3 as lite
...
con = lite.connect("C:\temp\my.db")
cur = con.cursor()
cur.execute(query)
data = cur.fetchall
for row in data:
    for i in range(len(row)):
        print row[i],
    print "\t",
using System.Data.SQLite;
...
string constr = "Data Source=C:\temp\my.db;Version=3;";
SQLiteConnection conn = new SQLiteConnection(constr);
SQLiteCommand command = new SQLiteCommand(query, conn);                
conn.Open();
SQLiteDataAdapter sda = new SQLiteDataAdapter(command);                
DataTable dt = new DataTable();
sda.Fill(dt);
sda.Dispose();
command.Dispose();
conn.Dispose();

object[,] ret = new object[dt.Rows.Count, dt.Columns.Count];
int rowCount = 0;
int colCount = 0;
foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn col in dt.Columns)
    {
        ret[rowCount, colCount] = col.ColumnName;
        colCount++;
    }
    rowCount++;
}
...
return ret;
总的来说,这段代码运行得相当好

这是运行查询的示例C#代码

import sqlite3 as lite
...
con = lite.connect("C:\temp\my.db")
cur = con.cursor()
cur.execute(query)
data = cur.fetchall
for row in data:
    for i in range(len(row)):
        print row[i],
    print "\t",
using System.Data.SQLite;
...
string constr = "Data Source=C:\temp\my.db;Version=3;";
SQLiteConnection conn = new SQLiteConnection(constr);
SQLiteCommand command = new SQLiteCommand(query, conn);                
conn.Open();
SQLiteDataAdapter sda = new SQLiteDataAdapter(command);                
DataTable dt = new DataTable();
sda.Fill(dt);
sda.Dispose();
command.Dispose();
conn.Dispose();

object[,] ret = new object[dt.Rows.Count, dt.Columns.Count];
int rowCount = 0;
int colCount = 0;
foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn col in dt.Columns)
    {
        ret[rowCount, colCount] = col.ColumnName;
        colCount++;
    }
    rowCount++;
}
...
return ret;
Python或C代码是次优的吗?例如,我应该使用
SQLiteDataReader
而不是
SQLiteDataAdapter
?如果您有任何想法,我将不胜感激

结果集本身非常小,在某些情况下只是一个数字,所以我不会认为ExcelDNA为过程增加了空间。示例Python查询大约需要15秒,而C#最多需要1分钟

最后,修改
PRAGMA
设置将如何影响性能?对一些通用设置有什么建议吗?我的首要任务是查询的速度


此外,对于如何在Python、C#或持久化中实际实现这些设置的任何建议都将不胜感激。

我没有正确地清除datatable、SQL命令或连接。一旦我将
Dispose
方法插入到
finally
块中,C#code的响应急剧增加,在大多数情况下都优于Python