我要每隔1s查询数据库(数据库的量不大2000条记录。我每次取400条)),现在是查询的过程不要消耗多少时间,但是cpu消耗比较大,达到了40%,一直维持在这个比例,问有什么办法将cpu给降下来,大家给个方法,谢谢!

解决方案 »

  1.   

    在这每隔1s查询,执行下面的代码,我记错了,是20%,400/1万时为40%左右。
    CTime tm = CTime::GetCurrentTime();
    int nYear = tm.GetYear();
    int nMonth = tm.GetMonth();
    int   nDay =  tm.GetDay();
    int  nHour = tm.GetHour();
    int nMinute = tm.GetMinute()+1;
    int nSecond = tm.GetSecond();
    CTime tmBeg(nYear, nMonth, nDay, nHour,nMinute,nSecond);
    CTime tmEnd(nYear, nMonth, nDay, nHour, nMinute, nSecond);
    tmEnd -= CTimeSpan(0,0,2,0);
    // 查询数据库中2分钟的数据
    BOOL bRet = TRUE;
    CString strDbField, strTable, strTmBeg, strTmEnd;
    GetRtDefName(strTable);
    strTable.Replace("#", "#"); strTmBeg = tmBeg.Format("%#Y-%#m-%#d %H:%M");
    strTmEnd = tmEnd.Format("%#Y-%#m-%#d %H:%M"); CString strSql;
    CTimeSpan tmSpan = tmEnd-tmBeg;
    strSql.Format("SELECT * FROM %s WHERE [查询时间] >= \'%s\' AND [查询时间] <= \'%s\' ORDER BY [查询时间]",
    strTable, strTmEnd,strTmBeg);
    clock_t begin = clock();
    try
    {
    if ( IsOpen() )
    Close();

         if ( !Open(AFX_DB_USE_DEFAULT_TYPE, strSql))
                  return ;
    }
    catch( CDBException *e)
    {
    CString strMsg(e->m_strError);
    strMsg.Replace('\x0a', '!');
    AfxMessageBox(strMsg);
    e->Delete();
    bRet = FALSE;
    return ;

    while(!IsEOF())
    {
         EVENT event;
    GetRtValue(&event);
    events.InsertAt(0,event);
          MoveNext(); 
     
    }
      

  2.   

    每次处理的时候不多于100条,每500毫秒以内调用一次,看看CPU
      

  3.   

    200/6000 1s刷新读取,cpu达到15%,不知道为什么cpu是摇摆逐渐升高的啊?
    而且是同一段代码的执行,cpu一直在摇摆,3%-%14-%6这样的样子,
    按道理应是逐步升高啊?为什么?
      

  4.   

    数据库是否在本机上,还有不知道你存取数据库的具体封装是什么方式?ADO?还有你如何确定你每次select到200个,再者由于是根据时间来select不能保证每个周期都select 到同样数量的数据
    观察代码,通常select消耗一些CPU资源,循环会消耗一些资源,如果结果集为空,那么循环不会消耗资源,每次循环的次数相同,你才能知道循环对CPU的消耗是怎样的一个程度