Solved

Optimizing MySQL query execution time with C# ASP.NET

Posted on 2008-06-24
11
1,088 Views
Last Modified: 2013-12-16
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();
    }
0
Comment
Question by:meerkat2040
  • 6
  • 4
11 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21861983
How often are reports generated? If insert is more frequent than report generation, actually you don't need to keep on adding the total as you can just compute the total when you are about to generate report. You can do that by using "select count(*)" based on certain "where" condition.
0
 

Author Comment

by:meerkat2040
ID: 21861992
Yeah.  I may have to do that.

I have a logging application that keeps track of my daily web hits.  Every day I get around 30,000 hits, but most are from search engines going through my site (each word in a dictionary has it's own web page, and there are 200,000 words).  It takes a long time to generate the totals for a whole month, which charts how many unique ip addresses visit per day.  Since that's my experience with reporting, I figured it would save a lot of time to keep a running tally in advance.

Even if I exclude those statements that set totals=totals+1 (which cuts the time in half), the rest of the inserts still take a quarter of a second.  I certainly want to be able to support more than 4 users/second on my web app.
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21862077
Any index in results table? What are the data type of those columns that you index? Index causes insert to be smaller, especially with bigger data type
0
 

Author Comment

by:meerkat2040
ID: 21862091
CREATE TABLE `answers` (
  `aid` int(11) NOT NULL auto_increment,
  `sid` int(11) NOT NULL default '0',
  `qid` int(11) NOT NULL default '0',
  `anumber` int(11) NOT NULL default '0',
  `abody` text,
  `atype` int(11) NOT NULL,
  `width` smallint(6) NOT NULL default '0',
  `height` smallint(6) NOT NULL default '0',
  `totals` int(11) NOT NULL default '0',
  PRIMARY KEY  (`aid`),
  KEY `questionid` (`qid`),
  KEY `surveyid` (`sid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2749 ;

CREATE TABLE `surveys` (
  `sid` int(11) NOT NULL auto_increment,
  `guid` varchar(16) NOT NULL COMMENT 'This is the survey unique identifier',
  `title` varchar(128) NOT NULL,
  `ownerid` int(11) NOT NULL,
  `creation_date` datetime NOT NULL,
  `modified_date` datetime NOT NULL,
  `open_date` datetime default NULL,
  `close_date` datetime default NULL,
  `lastactivity_date` datetime default NULL,
  `active` tinyint(1) NOT NULL default '1',
  `shared` tinyint(1) NOT NULL default '0',
  `questions` smallint(6) NOT NULL default '0',
  `description` text,
  `totals` int(11) NOT NULL default '0',
  PRIMARY KEY  (`sid`),
  KEY `GUID` (`guid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ;
0
 

Author Comment

by:meerkat2040
ID: 21862097
CREATE TABLE `results` (
  `rid` int(11) NOT NULL auto_increment,
  `sid` int(11) NOT NULL default '0',
  `qid` int(11) default NULL,
  `aid` int(11) NOT NULL default '0',
  `abody` text,
  `pid` int(11) NOT NULL default '0',
  `date_taken` datetime NOT NULL,
  PRIMARY KEY  (`rid`),
  KEY `pid` (`pid`),
  KEY `qid` (`qid`),
  KEY `aid` (`aid`),
  KEY `sid` (`sid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=18947 ;
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 18

Expert Comment

by:philipjonathan
ID: 21862106
typo...
Index causes insert to be slower, especially with bigger data type
0
 

Author Comment

by:meerkat2040
ID: 21862124
pid is participant id.  we want to be able to lookup responses by participant.
qid is question id.  We need to look up answers based on questionid.
aid is answer id.   We used to need that to look up total number of a given answer, but I'm not using it now since I've got the totals column.
sid is surveyid.  We need to look up based on surveyid.
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21862155
Well, the only thing i can think of is surveys.guid, as your survey table gets bigger, it sure will slow down writes to that table.
0
 

Author Comment

by:meerkat2040
ID: 21862175
We only write the guid to the surveys table once.  That's just so when someone's taking a survey we are not puting the actual surveyid in the request URL.
0
 

Author Comment

by:meerkat2040
ID: 21862263
Well, after some further testing, it appears that it's a problem with my test machine.

On the godaddy server that's hosting the future website, it only takes around 17ms for each survey save.  So I've got some work to do on my development machine.
0
 
LVL 20

Accepted Solution

by:
virmaior earned 125 total points
ID: 21865609
I would recommend switching to the multiple insert format and then using the SELECT COUNT(*) methods.

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.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now