July 11, 2016
Hi,
I have a VBA loop that inserts around 50 rows with 6 columns into a MySQL table.
Everything runs well, however currently it take approx 20 seconds to insert those 50 rows (loop 50 times) ,this is far too much. I just want to know if there is a faster way (just one INSERT query in one hit), inserting all 50 rows, all at once but I don't know if it is possible.
I have search all the solution around and mostly suggested to use bulk insert, but i do not know how to use it.
What I want is simplicity & super-fast uploading.
Please note that I am a beginner on this so if you can stir me into some samples on how it should be done it will be much appreciated.
Thanks guys!
Regards,
Hooi Thin
July 11, 2016
Hi Philip,
The reason i can't add the SQL here is because I get access denied - Sucuri Website Firewall (Block reason: SQL injection was detected and blocked.)
Yes, is there an alternative way to change the query execute once rather than 50 times, can you guide me please? I've been struggling this for several days 🙁
Thanks.
Regards,
Hooi Thin
October 5, 2010
So attaching a workbook with VBA (SQL) in it to your forum comment gives you an error? Have you clicked on the Attachments button below and tried adding/uploading a workbook?
With regards to your SQL, why are you doing a SELECT every loop? It looks like what you are trying to do is look for a specific record and if it isn't found you do the INSERT.
However, what it is actually doing is looking through every row in your worksheet, checking (SELECT) the database to see if those values are already in there, BUT because your INSERT statement is outside the FOR loop, the code is only doing 1 INSERT and this is for the last record not found to match your data in the workbook.
So it's not doing 50 INSERTS the code is only doing 1 at most every time you run the VBA Sub. As I'm not clear on what it is you are trying to do exactly, and I haven't seen your data or database, I can't get a lot more specific in my advice.
Of course removing the SELECT will speed things up but I don't know if you need it there or not.
If you can explain what you are trying to do, and what you expect the code to do, and supply the workbook with data, I can probably help more 🙂
Cheers
Phil
July 11, 2016
Hi Philip,
I did't try to upload workbook with SQL but pasted macro with SQL coding at here having the error.
Yes you are right, I am doing the SELECT statement inside for loop just to checking if each row in excel exist in MySQL db, if not found then will insert so i need the SELECT.
INSERT statement is outside the FOR loop -- previously i put in for loop, but i move out after that to populate string inside the loop just to prepare a complete full query, after full query done then make one time insert query to execute to database. I thought this way will be faster but its not.
Finally I have found the root cause, I am currently running this vba on my local country Malaysia, but the server itself is remote on US. i tested to import to my local db, 1 second can insert but to remote server need longer time, i think the best way is not to use VBA as it have limitation on time constraint issue? If want to stay at VBA what can i do?
Thanks!
Regards,
Hooi Thin
October 5, 2010
I think the issue is more to do with the DB being in the USA than you using VBA. If you are doing database operations remotely like that you must expect delays like you are experiencing. Working on a DB on your local machine will always be faster. So, if you have to work with the database being remote like this you may have to put up with it.
I don't know what type of server your DB is stored on. If it's a low powered machine you may get better response by using something with more power. You could look at Azure. Do you have to use MySQL? Are you using this because it's hosted on a web server?
If you want to try getting away from VBA you could look at MySQL Workbench but I don't know what performance improvements that will give, if any, over your current setup.
Regards
Phil
July 11, 2016
Hi Phil,
I think i will continue with VBA and put up with it. I need to use MySQL as all of my application are hosted there.
Our server is on virtual platform with microsoft windows server 2012 R2 Standard.
After all, I think still limitation i can do as all things control by our organisation.
Thanks a lot for your advise and guidance! Appreciate that 🙂
Regards,
Hooi Thin
1 Guest(s)