各位:
我發現POSTGRE資料庫查詢慢,比SQLSERVER多兩倍,這是怎么回事?
我用的是C#,
新建資料庫:
string connectionString = "Server=localhost;Port=5432;Username=postgres;Password=12345";
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
conn.Open();
string sql = "CREATE DATABASE " + Name + " WITH OWNER = postgres ENCODING='UTF8' LC_COLLATE = 'Chinese (Simplified)_China.936' LC_CTYPE = 'Chinese (Simplified)_China.936' TABLESPACE = pg_default CONNECTION LIMIT = -1;";
NpgsqlCommand objCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
conn.Close();
conn.Dispose();
新建表:
string connectionString = "Server=localhost;Port=5432;Username=postgres;Password=12345;Database=" + DataBaseName + ";";
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
conn.Open();
string sql = "CREATE TABLE " + TableName +
"( 時間 timestamp without time zone, TempratureA integer, PowerA integer,ACVoltageA integer,CoilVoltageA integer,DCCurrentA integer,HzA integer,DCVoltageA integer,StoveWaterFlowA integer,MachingWaterFlowA integer,CapVoltageA integer,INVCurrentA integer,TPreheatSA integer,TColdSA integer,RESA1 integer,RESA2 integer) WITH(OIDS = FALSE);";
NpgsqlCommand objCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
sql = "CREATE INDEX x" + TableName + " ON " + TableName + " USING btree(時間 ASC NULLS LAST);";
objCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
sql = "ALTER TABLE " + TableName + " CLUSTER ON x" + TableName + ";";
objCommand = new NpgsqlCommand(sql, conn);
objCommand.ExecuteNonQuery();
conn.Close();
查詢:
string connectionString = "Server=localhost;Port=5432;Username=postgres;Password=12345;Database=" + DataBaseName + ";";
NpgsqlConnection conn = new NpgsqlConnection(connectionString);
conn.Open();
DateTime StTime, EdTime;
StTime = DateTime.Parse(StartTime);
EdTime = DateTime.Parse(EndTime);
string sql = "SELECT * FROM " + TableName + " where 時間> '" + StTime + "' and 時間<'" + EdTime + "';";
NpgsqlCommand objCommand = new NpgsqlCommand(sql, conn);
da = new NpgsqlDataAdapter(objCommand);
ds = new DataSet();
da.Fill(ds, "ds");
objCommand.Parameters.Clear();
objCommand.Dispose();
ds.Dispose();
conn.Close();
conn.Dispose();
每次查詢兩萬多條記錄,為什么比較慢,是SQLSERVER的三倍。
大師們能幫忙看一下嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/228577.html
標籤:PostgreSQL
