postgres创建索引参考
http://www.cnblogs.com/stephen-liu74/archive/2012/05/09/2298182.html
CREATE TABLE test1 (
id integer,
content varchar
);
CREATE INDEX test1_id_index ON test1 (id);
创建存储过程
CREATE OR REPLACE FUNCTION update_sqs(lng "varchar", lat "varchar", gid "varchar")
RETURNS void
AS
$BODY$
DECLARE
r RECORD;
del bool;
num int4 := 0;
sql "varchar";
BEGIN
sql := 'UPDATE public.sqs_poi SET geocoding_lng = ' || lng || ', geocoding_lat = ' || lat || ' WHERE geoid=' || gid;
execute sql;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
下面是调用的代码
AscMethod方法通过多线程调用
for (int ii = 0; ii < 10; ii++)
{
Thread threadTmp = new Thread(new ParameterizedThreadStart(AscMethod));
threadTmp.Start(ii);
}
public void AscMethod(object i)
{
double s1 = Convert.ToInt32(i)*500000;
double s2 = Convert.ToInt32(i)*500000 + 500000;
string strQuery = "SELECT pr, city, addr, geoid FROM public.sqs_poi WHERE geocoding_lng IS NULL AND geoid >= " + s1.ToString() + " AND geoid < " + s2.ToString() + ";";// LIMIT 500000 OFFSET 0
NpgsqlConnection conn = new NpgsqlConnection("Server=" + Config.host + ";Port=5432;UserId=" + Config.user + ";Password=" + Config.password + ";Database=" + Config.database + ";Preload Reader=true;" + "CommandTimeout=0;ConnectionLifeTime=0;");
// conn.Close();
conn.Open();
NpgsqlCommand SelectCmd = new NpgsqlCommand(strQuery, conn);
NpgsqlDataReader StrReader = SelectCmd.ExecuteReader();
//int line = 1;
while (StrReader.Read())
{
string Address = StrReader["pr"].ToString() + StrReader["city"].ToString() + StrReader["addr"].ToString();
string[] geoString = geocoing(Address);
if (geoString == null)
continue;
try
{
//string updatesql = "UPDATE public.sqs_poi SET geocoding_lng = " + geoString[0] + ", geocoding_lat = " + geoString[1] + "WHERE geoid=" + StrReader["geoid"];
//NpgsqlCommand cmdi = new NpgsqlCommand(updatesql, conn);
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "update_sqs";
NpgsqlParameter p1 = new NpgsqlParameter("lng", DbType.String);
NpgsqlParameter p2 = new NpgsqlParameter("lat", DbType.String);
NpgsqlParameter p3 = new NpgsqlParameter("gid", DbType.String);
p1.Value = geoString[0];
p2.Value = geoString[1];
p3.Value = StrReader["geoid"].ToString();
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.ExecuteNonQuery();
}
catch (Exception ei)
{
if (conn.State != ConnectionState.Open)//判断数据库是否断开
{
conn.Open();
}
continue;
}
// line++;
}
conn.Close();
}