Solved

create a faster insert statement MySQL

Posted on 2014-10-22
3
215 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
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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