?
Solved

create a faster insert statement MySQL

Posted on 2014-10-22
3
Medium Priority
?
220 Views
Last Modified: 2014-10-22
How can I make this query faster

   query = "INSERT INTO ControllerMemoryLogData (UploadTime,EraseID,Technician,Location,ControllerSerialNumber,ToolNumber,SurveyTime,PulseSyncTime,Angle,Azimuth,DipAngle,HoursLeft,Voltage,BatteryUsed,Temperature, ";
            query += " TGF,TMF,NumberOfPulses,Frame0,Frame1,Frame2,Frame3,Frame4,Frame5,Frame6,Frame7,Frame8,Frame9,Frame10,PulseEnergy0,PulseEnergy1,PulseEnergy2,PulseEnergy3, ";
            query += " PulseEnergy4,PulseEnergy5,PulseEnergy6,PulseEnergy7,PulseEnergy8,PulseEnergy9,PulseEnergy10,PulseEnergy11,PulseEnergy12,PulsePing0,PulsePing1,PulsePing2, ";
            query += " PulsePing3,PulsePing4,PulsePing5,PulsePing6,PulsePing7,PulsePing8,PulsePing9,PulsePing10,PulsePing11,PulsePing12,PulseJam0,PulseJam1,PulseJam2,PulseJam3, ";
            query += " PulseJam4,PulseJam5,PulseJam6,PulseJam7,PulseJam8,PulseJam9,PulseJam10,PulseJam11,PulseJam12,DefaultLocationIndex) ";
            query += " VALUES (@UploadTime, @EraseID,@Technician,@Location,@ControllerSerialNumber,@ToolNumber,@SurveyTime,@PulseSyncTime,@Angle,@Azimuth,@DipAngle,@HoursLeft,@Voltage,@BattUsed,@Temperature,";
            query += " @TGF,@TMF,@NumberOfPulses,@Frame0,@Frame1,@Frame2,@Frame3,@Frame4,@Frame5,@Frame6,@Frame7,@Frame8,@Frame9,@Frame10,@PulseEnergy0,@PulseEnergy1,@PulseEnergy2,@PulseEnergy3, ";
            query += " @PulseEnergy4,@PulseEnergy5,@PulseEnergy6,@PulseEnergy7,@PulseEnergy8,@PulseEnergy9,@PulseEnergy10,@PulseEnergy11,@PulseEnergy12,@PulsePing0,@PulsePing1,@PulsePing2, ";
            query += " @PulsePing3,@PulsePing4,@PulsePing5,@PulsePing6,@PulsePing7,@PulsePing8,@PulsePing9,@PulsePing10,@PulsePing11,@PulsePing12,@PulseJam0,@PulseJam1,@PulseJam2,@PulseJam3, ";
            query += " @PulseJam4,@PulseJam5,@PulseJam6,@PulseJam7,@PulseJam8,@PulseJam9,@PulseJam10,@PulseJam11,@PulseJam12,@DefaultLocationIndex); ";
            using (MySqlConnection cn = new MySqlConnection(globalconnStr))
            using (MySqlCommand cmd = new MySqlCommand(query, cn))
            {
                try
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@UploadTime", DateTime.Now);
                    cmd.Parameters.AddWithValue("@EraseID", controllermemorylog.EraseID);
                    cmd.Parameters.AddWithValue("@Technician", controllermemorylog.technician.Replace("'", "`"));
                    cmd.Parameters.AddWithValue("@Location", controllermemorylog.location.Replace("'", "`"));
                    cmd.Parameters.AddWithValue("@ControllerSerialNumber", controllermemorylog.controllerserialnumber);
                    cmd.Parameters.AddWithValue("@ToolNumber", controllermemorylog.toolnumber);
                    cmd.Parameters.AddWithValue("@SurveyTime", controllermemorylog.SurveyTime.ToString("yyyyMMddHHmmss"));
                    cmd.Parameters.AddWithValue("@PulseSyncTime", controllermemorylog.PulseSyncTime.ToString("yyyyMMddHHmmss"));
                    cmd.Parameters.AddWithValue("@Angle", controllermemorylog.angle.ToString("G"));
                    if (controllermemorylog.azimuth.ToString("G") != "NaN")
                    {
                        cmd.Parameters.AddWithValue("@Azimuth", controllermemorylog.azimuth.ToString("G"));
                    }
                    else
                    {
                        string nullstring = string.Empty;
                        cmd.Parameters.AddWithValue("@Azimuth", null);
                    }
                    if (controllermemorylog.dip.ToString("G") != "NaN")
                    {
                        cmd.Parameters.AddWithValue("@DipAngle", controllermemorylog.dip.ToString("G"));
                    }
                    else
                    {
                        string nullstring = string.Empty;
                        cmd.Parameters.AddWithValue("@DipAngle", null);
                    }
                    cmd.Parameters.AddWithValue("@HoursLeft", controllermemorylog.hoursleft.ToString("G"));
                    cmd.Parameters.AddWithValue("@Voltage", controllermemorylog.voltage.ToString("G"));
                    cmd.Parameters.AddWithValue("@BattUsed", controllermemorylog.battused.ToString("G"));
                    cmd.Parameters.AddWithValue("@Temperature", controllermemorylog.temperature.ToString("G"));
                    cmd.Parameters.AddWithValue("@TGF", controllermemorylog.tgf.ToString("G"));
                    if (controllermemorylog.voltage.ToString("G") != "NaN")
                    {
                        cmd.Parameters.AddWithValue("@TMF", controllermemorylog.voltage.ToString("G"));
                    }
                    else
                    {
                        string nullstring = string.Empty;
                        cmd.Parameters.AddWithValue("@TMF", null);
                    }
                    cmd.Parameters.AddWithValue("@NumberOfPulses", controllermemorylog.numberofpulses.ToString());
                    cmd.Parameters.AddWithValue("@Frame0", controllermemorylog.frame[0].ToString());
                    cmd.Parameters.AddWithValue("@Frame1", controllermemorylog.frame[1].ToString());
                    cmd.Parameters.AddWithValue("@Frame2", controllermemorylog.frame[2].ToString());
                    cmd.Parameters.AddWithValue("@Frame3", controllermemorylog.frame[3].ToString());
                    cmd.Parameters.AddWithValue("@Frame4", controllermemorylog.frame[4].ToString());
                    cmd.Parameters.AddWithValue("@Frame5", controllermemorylog.frame[5].ToString());
                    cmd.Parameters.AddWithValue("@Frame6", controllermemorylog.frame[6].ToString());
                    cmd.Parameters.AddWithValue("@Frame7", controllermemorylog.frame[7].ToString());
                    cmd.Parameters.AddWithValue("@Frame8", controllermemorylog.frame[8].ToString());
                    cmd.Parameters.AddWithValue("@Frame9", controllermemorylog.frame[9].ToString());
                    cmd.Parameters.AddWithValue("@Frame10", controllermemorylog.frame[10].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy0", controllermemorylog.PulseEnergy[0].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy1", controllermemorylog.PulseEnergy[1].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy2", controllermemorylog.PulseEnergy[2].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy3", controllermemorylog.PulseEnergy[3].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy4", controllermemorylog.PulseEnergy[4].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy5", controllermemorylog.PulseEnergy[5].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy6", controllermemorylog.PulseEnergy[6].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy7", controllermemorylog.PulseEnergy[7].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy8", controllermemorylog.PulseEnergy[8].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy9", controllermemorylog.PulseEnergy[9].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy10", controllermemorylog.PulseEnergy[10].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy11", controllermemorylog.PulseEnergy[11].ToString());
                    cmd.Parameters.AddWithValue("@PulseEnergy12", controllermemorylog.PulseEnergy[12].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing0", controllermemorylog.PulsePing[0].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing1", controllermemorylog.PulsePing[1].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing2", controllermemorylog.PulsePing[2].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing3", controllermemorylog.PulsePing[3].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing4", controllermemorylog.PulsePing[4].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing5", controllermemorylog.PulsePing[5].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing6", controllermemorylog.PulsePing[6].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing7", controllermemorylog.PulsePing[7].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing8", controllermemorylog.PulsePing[8].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing9", controllermemorylog.PulsePing[9].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing10", controllermemorylog.PulsePing[10].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing11", controllermemorylog.PulsePing[11].ToString());
                    cmd.Parameters.AddWithValue("@PulsePing12", controllermemorylog.PulsePing[12].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam0", controllermemorylog.PulseJam[0].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam1", controllermemorylog.PulseJam[1].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam2", controllermemorylog.PulseJam[2].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam3", controllermemorylog.PulseJam[3].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam4", controllermemorylog.PulseJam[4].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam5", controllermemorylog.PulseJam[5].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam6", controllermemorylog.PulseJam[6].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam7", controllermemorylog.PulseJam[7].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam8", controllermemorylog.PulseJam[8].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam9", controllermemorylog.PulseJam[9].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam10", controllermemorylog.PulseJam[10].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam11", controllermemorylog.PulseJam[11].ToString());
                    cmd.Parameters.AddWithValue("@PulseJam12", controllermemorylog.PulseJam[12].ToString());
                    cmd.Parameters.AddWithValue("@DefaultLocationIndex", controllermemorylog.defaultLocationIndex);

                    try
                    {
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                    catch (MySqlException ex)
                    {
                        SaveSQLCommands(cmd.CommandText);
                    }

Open in new window

0
Comment
Question by:r3nder
[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
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 40397902
Instead of keep using query += to create a dynamic piece of SQL you could have all that as a stored procedure and use that instead.

It might run a millisecond or two faster (which is what you specifically ask for) - but an INSERT command is going to take time.

(check if your end table you insert to has indexes, if it has indexes then any that are superfluous will slow things down a lot.)
0
 
LVL 6

Author Comment

by:r3nder
ID: 40398028
I just have an ID field that is a primary key and auto increment - but no indexes
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40398104
Thanks
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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 Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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