I'm doing a website that hosts surveys. For some reason, the execution time of the insert/update to the mySQL database for a single survey response is nearly a half a second. This seems like an aweful long time for a small query to execute.
Below is the entire query of entering a single survey page response. I thought keeping a running total of the number of responses in the row that stores the answer would save a lot of time when it was time to generate reports. However this seems to double the execution time.
Even without the totals=totals+1 lines, the execution time is still around .265 seconds. That would be fine if only one person was taking a survey at a time, but my goal is to host lots of surveys.
Elapsed Time: 0 seconds, or 453.125 milliseconds per Survey save.
Last SQL Statement:
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,2,1,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=1;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,3,6,'This is sample text for testing purposes.',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=6;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,4,7,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=7;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,6,17,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=17;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,7,18,'This is sample text for testing purposes.',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=18;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,9,23,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=23;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,10,28,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=28;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,11,30,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=30;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,13,34,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=34;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,14,41,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=41;
INSERT INTO results(sid,qid,aid,abody,pid,date_taken) VALUES (1,15,47,'',1,'2008-06-24 18:04:12');
UPDATE answers SET totals=totals+1 WHERE aid=47;
UPDATE surveys SET lastactivity_date='2008-06-24 18:04:12',totals=totals+1 WHERE sid=1;
This is the method that generates and executes the statement:
public void StoreResults(int iPid)
{
sSQL="";
string dateFormat = "yyyy-MM-dd HH:mm:ss";
string surveyDateTime = DateTime.Now.ToString(dateFormat);
string sSurveyID = iSurveyID.ToString();
string sPid = iPid.ToString();
for (int iQuestion = 0; iQuestion < myQuestions.Length; iQuestion++)
{
if (myQuestions[iQuestion].Answers != null)
{
for (int iAnswer = 0; iAnswer < myQuestions[iQuestion].Answers.Length; iAnswer++)
{
if (myQuestions[iQuestion].Answers[iAnswer].Selected)
{
//insert results table row into database
sSQL += "INSERT INTO results(sid,qid,aid,abody,pid,date_taken) ";
sSQL += "VALUES (" + sSurveyID + "," + myQuestions[iQuestion].QuestionID.ToString();
sSQL += "," + myQuestions[iQuestion].Answers[iAnswer].AnswerID.ToString() + ",'";
sSQL += SQLFix(myQuestions[iQuestion].Answers[iAnswer].Response.ToString()) + "',";
sSQL += sPid + ",'" + surveyDateTime + "');";
sSQL += "UPDATE answers SET totals=totals+1 WHERE aid=";
sSQL += myQuestions[iQuestion].Answers[iAnswer].AnswerID.ToString() + ";";
}
}
}
}
//update survey lastactivity_date and totals in database
sSQL += "UPDATE surveys SET lastactivity_date='" + surveyDateTime;
sSQL += "',totals=totals+1 WHERE sid=" + iSurveyID.ToString() + ";";
dsPollsDatabase.UpdateCommand = sSQL;
dsPollsDatabase.Update();
}
With a multiple insert, the INDEX is only rebuilt once for the INSERT rather than one time for each row with your current code.
Sure, your current server is running a small data set quickly -- but just wait until the data set gets bigger. The time to rebuild the INDEXes can become a meaningful component.