Solved

create a faster insert statement MySQL

Posted on 2014-10-22
3
199 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Creating and Managing Databases with phpMyAdmin in cPanel.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

16 Experts available now in Live!

Get 1:1 Help Now