位置:海鸟网 > IT > ASP.NET >

基于性能的编程技巧点滴

例1

//bIsBegin表示是否是开始时间,否则代表结束时间;

//具体指定了每个查询条件;

public DataSet UserQuery()

{

DateTime dtBegin = new DateTime(1900,1,1,0,0,0);

DateTime dtEnd = new DateTime(9999,12,31,23,59,59);

return UserQuery(dtBegin,dtEnd);

}

public DataSet UserQuery(DateTime dtInput, bool bIsBegin)

{

DateTime dtBegin;

DateTime dtEnd;

if(bIsBegin)

{

dtBegin = dtInput;

dtEnd = new DateTime(9999,12,31,23,59,59);

}

else

{

dtBegin = new DateTime(1900,1,1,0,0,0);

dtEnd = dtInput;

}

return UserQuery(dtBegin,dtEnd);

}

public DataSet UserQuery()

{

DateTime dtTemp = new DateTime(1900,1,1,0,0,0);

return UserQuery(dtTemp,dtTemp,false,false);

}

public DataSet UserQuery(DateTime dtInput, bool bIsBegin)

{

if(bIsBegin)

{

return UserQuery(dtInput,dtInput,true,false);

}

else

{

return UserQuery(dtInput,dtInput,false,true);

}

}

public DataSet UserQuery(DateTime dtBegin,DateTime dtEnd)

{

return UserQuery(dtBegin,dtEnd,true,true);

}

private DataSet UserQuery(DateTime dtBegin,DateTime dtEnd, bool bNeedBegin,bool bNeedEnd)

{

string strSQL = "select A.somefields from A where 1=1";

if(bNeedBegin)

{

strSQL += " and A.fdBeginDate>=@dtBegin";

}

if(bNeedEnd)

{

strSQL += " and A.fdEndDate<=@dtEnd";

}

//do some query

....

return a DataSet;

}

例2

SELECT TB_NAMELIB_3.NAME OBJ3_NAME FROM TB_NAMELIB_3 WHERE TB_NAMELIB_3.NAME_ID= OBJ1_ID;

SELECT TB_NAMELIB_4.NAME OBJ4_NAME FROM TB_NAMELIB_4 WHERE TB_NAMELIB_4.NAME_ID= OBJ2_ID;

下面用Union合成取数据过程

DataRowCollection objRows = DataSet1.Tables[0].Rows;

int iCount = objRows.Count;

string[] strSQLArr = new string[iCount];

string strSQL;

string strFields;

string strObjID1;

string strObjID2;

DataSet objDataSet;

for(int i=0;i<iCount;i++)

{

strObjID1 = objRows[i][“OBJ1_ID”].ToString();

strObjID2 = objRows[i][“OBJ2_ID”].ToString();

strFields = “’”+strObjID1 +"’ OBJ1_ID, ";

strFields+= “’”+objRows[i][“OBJ1_NAME”].ToString()+"’ OBJ1_NAME, ";

strFields+= “’”+strObjID2 +"’ OBJ2_ID, ";

strFields+= “’”+objRows[i][“OBJ2_NAME”].ToString()+"’ OBJ2_NAME, ";

strSQLArr[i] = " UNION SELECT "+ strFields +" TEMPU. OBJ3_NAME,TEMPJ.OBJ4_NAME, FROM (SELECT 1 PID, TB_NAMELIB_3.NAME OBJ3_NAME FROM TB_NAMELIB_3 WHERE TB_NAMELIB_3.NAME_ID="'"+ strObjID1 +""'") TEMPU LEFT JOIN (SELECT 1 PID, TB_NAMELIB_4.NAME OBJ4_NAME FROM TB_NAMELIB_4 WHERE TB_NAMELIB_4.NAME_ID= "'"+ strObjID2 +"'") TEMPJ ON TEMPU.PID = TEMPJ.PID UNION SELECT "+ strFields +" TEMPU.OBJ3_NAME,TEMPJ.OBJ4_NAME, FROM (SELECT 1 PID, TB_NAMELIB_3.NAME OBJ3_NAME FROM TB_NAMELIB_3 WHERE TB_NAMELIB_3.NAME_ID= "'"+ strObjID1 +"'") TEMPU RIGHTER JOIN (SELECT 1 PID, TB_NAMELIB_4.NAME OBJ4_NAME FROM TB_NAMELIB_4 WHERE TB_NAMELIB_4.NAME_ID= "'"+ strObjID2 +"'") TEMPJ ON TEMPU.PID = TEMPJ.PID ";

}

if(iCount>0)

{

if(null==strSQLArr[0])

{

return null;

}

strSQLArr[0] = strSQLArr[0].Substring(6);

strSQL = String.Concat(strSQLArr);

//创建连接 PersistConn m_objPersistConn = new PersistConn();

//执行查询 objDataSet = m_objPersistConn.ExecuteDataSet(strSQL);

//释放连接 m_objPersistConn.Dispose();

return objDataSet;

}

另外一个比较重要的问题是:如何在“有限”的情况下检验程序的性能。这里的“有限”是指不能够快速地使系统具有超大数据量或者复杂度。这时,我们可以考虑进行“逆境测试”,即通过降低Server的配置或程序的简单修改从而能够在某种程度上模拟出一些性能问题。