Solved

create a faster insert statement MySQL

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Loop not working 29 47
Pivot Table for a join with partition by and over clause 7 27
System.Speech 2 15
Make query more efficient 1 17
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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