天天看點

SqlCE與SQLite 性能測試

最近做快遞項目,用到了PDA WinCe的系統 采用先本地儲存 再進行上傳的模式

但是客戶提出了一個問題:操作慢

一直沒有管 就這樣一直運作着  就是最近 客戶拿了一把新槍讓我們開發

說我們的PDA設定不行 反應到上司那 決定找下原因

後來通過增加幾個時間點 發現一個問題 掃描用時是60ms 插入一條資料600ms  然後再從資料庫中查詢出來顯示到界面上用時900ms

比較可怕 用的SQLCe資料庫

後來通過網上查閱 SQLite性能要高點  親測 确實如果 測試代碼如下

支行結果

單條插入SQLCE    170545ms

單條插入SQLite      22596ms

事務插入SQLCE        9272ms

事務插入SQLite          151ms

讀取SQLCE              7544ms

讀取SQLite              3384ms

無論從讀取速度還是從插入速度上,SQLite都比SQLCE快很多 尤其是在事務方面

DatabaseCE ce = new DatabaseCE();
            DatabaseSQLite lite = new DatabaseSQLite();
            int start = 0;
            int end = 0;
            int countCE = 0;
            int countLite = 0;
            string[] sqlTransCE = new string[1000];
            for (int i = 0; i < 1000; i++)//插入1000條資料到SQLCE中
            {
                string sqlInsert = "insert into TMsg(FromUserNo,ToUserNo,SendTime,MsgType,MsgContent,MsgStatus,SendTag,MsgUserName,AddUserNo,AddTime)"
                    + " values(" + i + "," + i + ",getdate(),0,'第" + i + "條',0,0,'User" + i + "',0,getdate())";
                sqlTransCE[i] = sqlInsert;
                start = Environment.TickCount;
                ce.ExecuteSQL(sqlInsert);
                end = Environment.TickCount;
                countCE += (end - start);
            }
            lbCE.Text = countCE + "ms";
            Application.DoEvents();

            string[] sqlTransLite = new string[1000];
            for (int i = 0; i < 1000; i++)//插入1000條資料到SQLite中
            {
                string sqlInsert = "insert into TMsg(FromUserNo,ToUserNo,SendTime,MsgType,MsgContent,MsgStatus,SendTag,MsgUserName,AddUserNo,AddTime)"
                    + " values(" + i + "," + i + ",'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',0,'第" + i + "條',0,0,'User" + i + "',0,'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')";
                sqlTransLite[i] = sqlInsert;
                start = Environment.TickCount;
                lite.ExcuteSql(sqlInsert);
                end = Environment.TickCount;
                countLite += (end - start);
            }

            lbLite.Text = countLite + "ms";
            Application.DoEvents();

            start = Environment.TickCount;
            ce.ExecuteSQL(sqlTransCE);//事務插入1000條資料到SQLCE中
            end = Environment.TickCount;

            lbCET.Text=(end-start).ToString();
            Application.DoEvents();

            start = Environment.TickCount;
            lite.ExecuteSQL(sqlTransCE);//事務插入1000條資料到SQLite中
            end = Environment.TickCount;


            lbLiteT.Text = (end - start).ToString();
            Application.DoEvents();

            string sql = "Select * from TMsg where MsgStatus=0";
            start = Environment.TickCount;
            DataTable dt = ce.GetDataTable(sql);//從SQLCE中讀取1000條資料
            end = Environment.TickCount;
            lbCEQ.Text = (end - start).ToString();
            Application.DoEvents();

            start = Environment.TickCount;
            DataTable dtLite = lite.GetDataTable(sql);//從SQLite中讀取1000條資料
            end = Environment.TickCount;

            lbLiteQ.Text = (end - start).ToString();