Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Optimizing MySQL query execution time with C# ASP.NET

Posted on 2008-06-24
11
Medium Priority
?
1,123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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
 
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 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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