Hi all. I wanted to see if someone can take a look at this and advise if I am on the right path or if this can be improved. This is importing the data just fine, so that’s not what I’m looking for help on. It’s the speed of the whole process. This is a desktop project and testing on Mac 10.14 but will be run on Windows too and Xojo 2019 r3.2
The code is down below, but here are some of my comments/questions to see if this is optimized the best:
- This is pulling data from a MySQL table and saving into a SQLite table
- ActiveStudents is a local SQLite table and contains 45 records
- teachMonitor is another local SQLite table that is populated from the data in the MySQL table. There are only 150 records based on the criteria from the SELECT statement for ActiveStudents
- StudyModTimeMySQL is the 3rd local SQLite table also populated with data from the MySQL table. There aren’t any records that match the original criteria, so no data to insert
- Am I using BeginTransaction and CommitTransaction correctly? Since there are two sets of inserts within one main While/Wend block, do I put just one BeginTransaction before the first While and the CommitTransaction after the last Wend? (no BT/CT inside the main While/Wend block) Or does this not even matter?
- This code takes about 11 seconds to complete. That is after going through 45 loops and pulling in a total of 150 records
- I understand that having to loop 45 times initially and then again for each of the W/W loops inside is quite excessive, but based on everything I laid out and the code below, is this the best practice? Optimal? Expected? Completely wrong?
Again, the INSERT commands are just fine. I am getting the data as expected. I just copied over everything from this part of the project so I didn’t forget anything, so this looks a bit muddy. It’s the time I want to improve upon. Thanks!
dim sql as String = "SELECT * FROM ActiveStudents"
dim rs as RecordSet = dbUser.SQLSelect(sql)
while not rs.eof
'tests
dim sqlTests as string = "SELECT * FROM emailresults WHERE RegCode = '" + rs.Field("RegCode").StringValue + "' AND Email = '" + rs.Field("Email").StringValue + "'"
dim rsTests as RecordSet = mdb.SQLSelect(sqlTests)
dbUser.BeginTransaction
if rsTests <> Nil then
while not rsTests.EOF
dbUser.SQLExecute "INSERT INTO teachMonitor ('RegCode', 'Email', 'FirstName', 'LastName', 'RemoveStudent', 'WhichTest', 'OutOf', 'TotalCorrect'," +_
" 'D1Corr', 'D2Corr', 'D3Corr', 'D4Corr', 'D5Corr', 'D1Taken', 'D2Taken', 'D3Taken', 'D4Taken', 'D5Taken', 'LastImport', 'YearMonitor', 'Score'," +_
" 'Domain', 'TestDate', 'TimeRemain', 'Product', 'TestID', 'MonTopicCorr', 'MonTopicIncorr') VALUES ('" + rsTests.Field("RegCode").StringValue + "', '" + rsTests.Field("Email").StringValue + "'," +_
" '" + rsTests.Field("FirstName").StringValue + "', '" + rsTests.Field("LastName").StringValue + "', ''," +_
" '" + rsTests.Field("WhichTest").StringValue + "', '" + rsTests.Field("OutOf").StringValue + "', '" + rsTests.Field("TotalCorrect").StringValue + "'," +_
" '" + rsTests.Field("D1Corr").StringValue + "', '" + rsTests.Field("D2Corr").StringValue + "', '" + rsTests.Field("D3Corr").StringValue + "'," +_
" '" + rsTests.Field("D4Corr").StringValue + "', '" + rsTests.Field("D5Corr").StringValue + "', '" + rsTests.Field("D1Taken").StringValue + "'," +_
" '" + rsTests.Field("D2Taken").StringValue + "', '" + rsTests.Field("D3Taken").StringValue + "', '" + rsTests.Field("D4Taken").StringValue + "'," +_
" '" + rsTests.Field("D5Taken").StringValue + "', '" + strDate + "', '" + rs.Field("YearMonitor").StringValue + "'," +_
" '" + rsTests.Field("Score").StringValue + "', '" + rsTests.Field("Domain").StringValue + "', '" + rsTests.Field("TestDate").StringValue + "'," +_
" '" + rsTests.Field("TimeRemain").StringValue + "', '" + rsTests.Field("Product").StringValue + "', '" + rsTests.Field("id").StringValue + "', '" + rsTests.Field("MonTopicCorr").StringValue + "', '" + rsTests.Field("MonTopicIncorr").StringValue + "');"
rsTests.MoveNext
wend
dbUser.CommitTransaction
end if
'time
sqlTime = "SELECT * FROM StudyModTime WHERE RegCode = '" + rs.Field("RegCode").StringValue + "' AND Email = '" + rs.Field("Email").StringValue + "'"
dim rsTime as RecordSet = mdb.SQLSelect(sqlTime)
dbUser.BeginTransaction
if rsTime <> Nil then
while not rsTime.EOF
dbUser.SQLExecute "INSERT INTO StudyModTimeMySQL ('totalTime', 'Email', 'studyMod', 'RegCode') VALUES ('" + rsTime.Field("TimeSpent").StringValue + "', '" + rsTime.Field("Email").StringValue + "', '" + rsTime.Field("StudyMod").StringValue + "', '" + rsTime.Field("RegCode").StringValue + "');"
rsTime.MoveNext
wend
dbUser.CommitTransaction
end if
rs.MoveNext
wend