Performance Drop / Query Slow after Compact Repair - Access 2007 - sql

I am performing a large number of insert operations using VBA - Access. the VBA code is in a file "control.accdb" performing operations on "data.accdb"
Few tables in the process are created from the VBA , few remain as such.
After finishing Phase 1 of the process , I do a Compact And Repair either manually or from DBEngine.Compact VBA code. The second phase (same code - next iteration) becomes very very slow.
Here is the Insert Code :
SQL = "INSERT INTO ExpectedResult " _
& "( DLID, NumRows, Total, SubjectBlock, NextSubjectBlockSeq ) " _
& "SELECT INVDL.DLID, 1 AS Expr1, INVDL.Amount, " _
& "INVDL.SubjectBlock,INVDL.SubjectBlockSeq+1 AS Expr2 " _
& "FROM INVDL where SubjectBlock > 0;"
cdb.Execute (SQL)
TargetNumRows = 2
Do While TargetNumRows < MaxSubjectBlockSeq + 1
Set qdf = cdb.QueryDefs("pq_appendToExpectedResult")
qdf!TargetNumRows = TargetNumRows '' parameter value
qdf.Execute
TargetNumRows = TargetNumRows + 1
Set qdf = Nothing
Loop
where pq_appendToExpectedResult :
PARAMETERS TargetNumRows Long;
INSERT INTO ExpectedResult _
( DLID, NumRows, Total, SubjectBlock, NextSubjectBlockSeq ) _
SELECT INVDL.DLID+1-[TargetNumRows], [TargetNumRows] AS Expr1, _
[ExpectedResult].[Total]+[INVDL].[Amount] AS NewTotal, _
INVDL.SubjectBlock, [INVDL].[SubjectBlockSeq]+1 AS Expr2 _
FROM INVDL INNER JOIN ExpectedResult _
ON (INVDL.SubjectBlock = ExpectedResult.SubjectBlock) _
AND (INVDL.SubjectBlockSeq = ExpectedResult.NextSubjectBlockSeq) _
WHERE (((INVDL.SubjectBlockSeq)>=[TargetNumRows]) _
AND (ExpectedResult.NumRows=[TargetNumRows]-1));
To the best of my knowledge , all indexes in INVDL are untouched.
I drop the table ExpectedResult and recreate it with indexes after each Phase (iteration).
Other tables - dont think will make any difference.

Related

Update query, nested iif SET and joined tables

Trying to make an Update Query with inner join and have the SET reflect the current month number.
Need help with syntax on updating a joined table that has columns for months, need to update the months that are Month(DATE()) +1,+2 only. Trying to use the below nested IIF as "IIF(Month(Date()) ="1",( SET table.c = table2.c2......) complete code below.
Syntax error is the result
Sample data
to update sql table
linked excel with values to use for updates
Update dbo_MasterSalesForecast_Test
INNER JOIN MasterSalesForecastUpdate
ON (dbo_MasterSalesForecast_Test.SubTo=MasterSalesForecastUpdate.Item) AND( dbo_MasterSalesForecast_Test.Planner=MasterSalesForecastUpdate.Planner)
IIF(Month(Date()) ="1",( SET
dbo_MasterSalesForecast_Test.[YY:1] = [MasterSalesForecastUpdate].[YYJan],
dbo_MasterSalesForecast_Test.[YY:2] = [MasterSalesForecastUpdate].[YYFan],
dbo_MasterSalesForecast_Test.[YY:3] = [MasterSalesForecastUpdate].[YYMar])
,
IIF(Month(Date())="2",( SET
dbo_MasterSalesForecast_Test.[YY:2]=[MasterSalesForecastUpdate].[YYFeb],
dbo_MasterSalesForecast_Test.[YY:3]=[MasterSalesForecastUpdate].[YYMar],
dbo_MasterSalesForecast_Test.[YY:4]=[MasterSalesForecastUpdate].[YYApr])
,
IIF(Month(Date())="3",( SET
dbo_MasterSalesForecast_Test.[YY:3]=[MasterSalesForecastUpdate].[YYMar],
dbo_MasterSalesForecast_Test.[YY:4]=[MasterSalesForecastUpdate].[YYApr],
dbo_MasterSalesForecast_Test.[YY:5]=[MasterSalesForecastUpdate].[YYMay])
,
IIF(Month(Date())="4",(SET
dbo_MasterSalesForecast_Test.[YY:4]=[MasterSalesForecastUpdate].[YYApr],
dbo_MasterSalesForecast_Test.[YY:5]=[MasterSalesForecastUpdate].[YYMay],
dbo_MasterSalesForecast_Test.[YY:6]=[MasterSalesForecastUpdate].[YYJun])
,
IIF(Month(Date())="5",(SET
dbo_MasterSalesForecast_Test.[YY:5]=[MasterSalesForecastUpdate].[YYMay],
dbo_MasterSalesForecast_Test.[YY:6]=[MasterSalesForecastUpdate].[YYJun],
dbo_MasterSalesForecast_Test.[YY:7]=[MasterSalesForecastUpdate].[YYJul])
,
IIF(Month(Date())="6",(SET
dbo_MasterSalesForecast_Test.[YY:6]=[MasterSalesForecastUpdate].[YYJun],
dbo_MasterSalesForecast_Test.[YY:7]=[MasterSalesForecastUpdate].[YYJul],
dbo_MasterSalesForecast_Test.[YY:8]=[MasterSalesForecastUpdate].[YYAug])
,
IIF(Month(Date())="7",(SET
dbo_MasterSalesForecast_Test.[YY:7]=[MasterSalesForecastUpdate].[YYJul],
dbo_MasterSalesForecast_Test.[YY:8]=[MasterSalesForecastUpdate].[YYAug],
dbo_MasterSalesForecast_Test.[YY:9]=[MasterSalesForecastUpdate].[YYSep])
,
IIF(Month(Date())="8",(SET
dbo_MasterSalesForecast_Test.[YY:8]=[MasterSalesForecastUpdate].[YYAug],
dbo_MasterSalesForecast_Test.[YY:9]=[MasterSalesForecastUpdate].[YYSep],
dbo_MasterSalesForecast_Test.[YY:10]=[MasterSalesForecastUpdate].[YYOct])
,
IIF(Month(Date())="9",(SET
dbo_MasterSalesForecast_Test.[YY:9]=[MasterSalesForecastUpdate].[YYSep],
dbo_MasterSalesForecast_Test.[YY:10]=[MasterSalesForecastUpdate].[YYOct],
dbo_MasterSalesForecast_Test.[YY:11]=[MasterSalesForecastUpdate].[YYNov])
,
IIF(Month(Date())="10",(SET
dbo_MasterSalesForecast_Test.[YY:10]=[MasterSalesForecastUpdate].[YYOct],
dbo_MasterSalesForecast_Test.[YY:11]=[MasterSalesForecastUpdate].[YYNov],
dbo_MasterSalesForecast_Test.[YY:12]=[MasterSalesForecastUpdate].[YYDec])
,
IIF(Month(Date())="11",(SET
dbo_MasterSalesForecast_Test.[YY:11]=[MasterSalesForecastUpdate].[YYNov],
dbo_MasterSalesForecast_Test.[YY:12]=[MasterSalesForecastUpdate].[YYDec],
dbo_MasterSalesForecast_Test.[YY+1:1]=[MasterSalesForecastUpdate].[YY+1Jan])
,
IIF(Month(Date())="12"(SET
dbo_MasterSalesForecast_Test.[YY:12]=[MasterSalesForecastUpdate].[YYDec],
dbo_MasterSalesForecast_Test.[YY+1:1]=[MasterSalesForecastUpdate].[YY+1Jan],
dbo_MasterSalesForecast_Test.[YY+1:02]=[MasterSalesForecastUpdate].[YY+1Feb])
)))))))))));
Cannot conditionally reference fields. Field must be static in query object then value to update with can be determined in an IIf() for each field. Use VBA to build and execute action SQL statement.
CurrentDb.Execute "UPDATE dbo_MasterSalesForecast_Test " & _
"INNER JOIN MasterSalesForecastUpdate " & _
"ON (dbo_MasterSalesForecast_Test.SubTo=MasterSalesForecastUpdate.Item) " & _
"AND (dbo_MasterSalesForecast_Test.Planner=MasterSalesForecastUpdate.Planner) " & _
"SET [YY:" & Month(Date()) & "] = [YY" & Format(Date(),"mmm") & "]," & _
"[YY:" & Month(DateAdd("m",1,Date())) & "] = [YY" & Format(DateAdd("m",1,Date()),"mmm") & "]," & _
"[YY:" & Month(DateAdd("m",2,Date())) & "] = [YY" & Format(DateAdd("m",2,Date()),"mmm") & "]"

MS ACCESS SQL Join Subquery

I have two tables: newparts, storedparts
I insert the parts of the newparts, which are not jet in the storedparts into the storedparts:
SQL_String = "INSERT INTO storedparts " & _
"SELECT newparts.* " & _
"FROM storedparts " & _
"RIGHT JOIN newparts ON (storedparts.identifier = newparts.identifier) AND (storedparts.timeStamp = newparts.timeStamp) " & _
"WHERE ((storedparts.AutoID) Is Null);"
This is working fine so far. Now the Problem: Table storedparts is getting so big that the programm is taking too Long for the join process. My solution: Just compare the newparts not to all parts of the storedparts, but just to parts that aren't older than 4 days... I tried a subquery like this, but i can't get it to run.
SQL_String = "INSERT INTO storedparts " & _
"SELECT newparts.* " & _
"FROM storedparts (WHERE storedparts.timestamp > Now() - 4) " & _
"RIGHT JOIN newparts ON (storedparts.identifier = newparts.identifier) AND (storedparts.timeStamp = newparts.timeStamp) " & _
"WHERE ((storedparts.AutoID) Is Null);"
Any help is appreciated.
This wouldn't be a problem if your tables have indexes.
CREATE INDEX ndx_sp_identifier ON storedparts (identifier);
CREATE INDEX ndx_np_identifier ON newparts (identifier);
Then I suggest you change your query to something like this as #jarlh pointed out.
INSERT INTO storedparts
SELECT newparts.*
FROM newparts
LEFT JOIN storedparts
ON newparts.identifier = storedparts.identifier
AND newparts.timeStamp = storedparts.timeStamp
WHERE storedparts.AutoID Is Null;
You could add the where clause after the join statements and see if it improves the performance of the query . Else Try this and see if it works
SQL_String = "INSERT INTO storedparts " & _
"SELECT newparts.* " & _
"FROM ( SELECT * FROM storedparts WHERE
storedparts.timestamp > DateAdd ( 'd', -4, Now()) )sparts" & _
"RIGHT JOIN newparts ON (sparts.identifier = newparts.identifier) AND
(sparts.timeStamp = newparts.timeStamp) " & _
"WHERE ((sparts.AutoID) Is Null);"

MS Access vba query where

Simple Question.
For example, i have a Customer that have 10 Orders and each order include 6-10 Items.
i want to create a vba query that will desplay all the items of a specific customer.
My query is:
x = CustomerNum
Query = "Select OrderNum from CustomerOrderT Where CustomerNum = " & x
Set result = CurrentDb.OpenRecordset(Query)
y = result!OrderNum
'(there is a lot of orders on y)
Query = "Select Product From OrderProducts Where OrderNum = " & y
Set result = CurrentDb.OpenRecordset(Query)
The problem is that i only see the Products of the first order and i cannot see the products of all the orders that i select on the first query.
Need some help to handke this situation.
Thank you very much.
You can just execute a single query for all orders:
x = CustomerNum
Query = " SELECT CustomerOrderT.CustomerNum, " & _
CustomerOrderT.OrderNum, " & _
" OrderProducts.Product " & _
" FROM CustomerOrderT INNER JOIN OrderProducts " & _
ON CustomerOrderT.OrderNum = OrderProducts.OrderNum " & _
" WHERE (((CustomerOrderT.CustomerNum)=" & x & ")) " & _
"ORDER BY CustomerOrderT.OrderNum, " & _
" OrderProducts.Product;"
And then loop over all the records, noting each change in OrderNum
But beware of building SQL like this if you don't control how variable CustomerNum is assigned, as you open yourself to SQL injection attacks.

Query syntax on a large inner-joined select query linked to a control giving runtime 3071 error message

I am getting Runtime error message 3071 on the following query stating that the query is too complex. I have created queries in the past which have seemed more complex than this one. I would like to understand what generates this message:
sql_get = "SELECT tblValueChain01.IDMacroProcesso, tblValueChain02.IDMicroProcesso02, tblValueChain03.ID, tblDependencies01.ID AS DependencyID, tblValueChain02.MicroProcesso02, tblValueChain01.MacroProcess, tblValueChain01.TeamLead, tblValueChain01.LastOrganisationDate, tblValueChain01.TempiIndeterminati, tblValueChain01.TempiDeterminati, tblValueChain01.Interinali, tblValueChain01.PartTime, tblValueChain01.DailyMinutesAverage AS Minutes01, tblValueChain01.DailyMinutesHigh AS Minutes01H, tblValueChain01.DailyMinutesLow AS Minutes01L, tblValueChain02.MicroProcesso02, " & _
"tblValueChain02.DailyMinutesAverage AS Minutes02, tblValueChain02.DailyMinutesHigh AS Minutes02H, tblValueChain02.DailyMinutesLow AS Minutes02L, tblValueChain03.MicroProcess, tblValueChain03.MinutesPerDay AS Minutes03, tblValueChain03.MinutesPerDayHigh AS Minutes03H, tblValueChain03.MinutesPerDayLow AS Minutes03L, tblDependencies01.FlowDescription, tblDependencies01.FlowType, tblTeamsDepartments.Department, tblTeams.Team, tblDependencies01.Precision, " & _
"tblDependencies01.ServiceDelivery , tblDependencies01.RiskReduction, tblDependencies01.CapacityCreation, tblDependencies01.TargetCapacityCreation, tblDependencies01.Feasibility, tblDependencies01.Timeframe, tblDependencies01.Priority, tblDependencies01.Note, tblDependencies01.RedundantControls, tblDependencies01.RedundantControlsNotes, tblDependencies01.RedundantControlsPotSolution, tblDependencies01.RedundantControlsNotesSymbol, tblDependencies01.RedundantControlsPotSolSymbol, tblDependencies01.RolesAndResponsibilities, tblDependencies01.RolesAndResponsibilitiesNotes, " & _
"tblDependencies01.RolesAndResponsibilitiesPotSolution , tblDependencies01.RolesResponNotesSymbol, tblDependencies01.RolesRespPotSolSymbol, tblDependencies01.SubstandardSvcs, tblDependencies01.SubstandardSvcsNotes, tblDependencies01.SubStandardSvcsPotSolution, tblDependencies01.SubStandardSvcsNotesSymbol, tblDependencies01.SubStandardSvcsPotSolSymbol, tblDependencies01.KnowledgeGaps, tblDependencies01.KnowledgeGapsNotes, tblDependencies01.KnowledgeGap, " & _
"PotSolution , tblDependencies01.KnowledgeGapsNotesSymbol, tblDependencies01.KnowledgeGapsPotSolSymbol, tblDependencies01.ExcessiveOversight, tblDependencies01.ExcessiveOversightNotes, tblDependencies01.ExcessiveOversightPotSolution, tblDependencies01.ExcessiveOversightNotesSymbol, tblDependencies01.ExcessiveOversightPotSolSymbol, tblDependencies01.UpstreamErrors, tblDependencies01.UpstreamErrorsNotes, tblDependencies01.UpstreamErrorsPotSolution, tblDependencies01.UpstreamErrorsNotesSymbol, tblDependencies01.UpstreamErrorsPotSolSymbol, tblDependencies01.DefectsRework, " & _
"tblDependencies01.DefectsReworkNotes , tblDependencies01.DefectsReworkPotSolution, tblDependencies01.DefectsReworkNotesSymbol, tblDependencies01.DefectsReworkPotSolSymbol, tblDependencies01.OverProduction, tblDependencies01.OverproductionNotes, tblDependencies01.OverproductionPotSolution, tblDependencies01.OverproductionNotesSymbol, tblDependencies01.OverproductionPotSolSymbol, tblDependencies01.MotionTransport, " & _
"tblDependencies01.MotionTransportNotes , tblDependencies01.MotionTransportPotSolution, tblDependencies01.MotionNotesSymbol, tblDependencies01.MotionSolSymbol, tblDependencies01.DowntimeWaiting, tblDependencies01.DowntimeWaitingNotes, tblDependencies01.DowntimeWaitingPotSolution, tblDependencies01.WaitDowntimeNotesSymbol, tblDependencies01.WaitDowntimeSolSymbol, tblDependencies01.ExcessiveHandoffs, tblDependencies01.ExcessiveHandoffNotes, tblDependencies01.ExcessiveHandoffPotSolution, " & _
"tblDependencies01.ExcessiveHandoffsSymbol , tblDependencies01.ExcessiveHandoffsPotSolSymbol, tblDependencies01.RCSLABreachInternal, tblDependencies01.RCSLABreachExternal, tblDependencies01.RCCorporatePolicyBreach, tblDependencies01.RCOperatingModelDiscrepancy, tblDependencies01.RRSLABreachInternal, tblDependencies01.RRSLABreachExternal, tblDependencies01.RRCorporatePolicyBreach, tblDependencies01.RROperatingModelDiscrepancy, tblDependencies01.SSSLABreachInternal, tblDependencies01.SSSLABreachExternal, tblDependencies01.SSCorporatePolicyBreach, " & _
"tblDependencies01.SSOperatingModelDiscrepancy , tblDependencies01.KGSLABreachInternal, tblDependencies01.KGSLABreachExternal, tblDependencies01.KGCorporatePolicyBreach, tblDependencies01.KGOperatingModelDiscrepancy, tblDependencies01.EOSLABreachInternal, tblDependencies01.EOSLABreachExternal, tblDependencies01.EOCorporatePolicyBreach, tblDependencies01.EOOperatingModelDiscrepancy, tblDependencies01.UESLABreachInternal, tblDependencies01.UESLABreachExternal, tblDependencies01.UECorporatePolicyBreach, tblDependencies01.UEOperatingModelDiscrepancy, tblDependencies01.DefSLABreachInternal, " & _
"tblDependencies01.DefSLABreachExternal , tblDependencies01.DefCorporatePolicyBreach, tblDependencies01.DefOperatingModelDiscrepancy, tblDependencies01.OPSLABreachInternal, tblDependencies01.OPSLABreachExternal, tblDependencies01.OPCorporatePolicyBreach, tblDependencies01.OPOperatingModelDiscrepancy, tblDependencies01.EHSLABreachInternal, tblDependencies01.EHSLABreachExternal, tblDependencies01.EHCorporatePolicyBreach, " & _
"tblDependencies01.EHOperatingModelDiscrepancy , tblDependencies01.DTSLABreachInternal, tblDependencies01.DTSLABreachExternal, tblDependencies01.DTCorporatePolicyBreach, tblDependencies01.DTOperatingModelDiscrepancy, tblDependencies01.ECSLABreachInternal, tblDependencies01.ECSLABreachExternal, tblDependencies01.ECCorporatePolicyBreach, tblDependencies01.ECOperatingModelDiscrepancy " & _
"FROM (tblTeamsDepartments INNER JOIN tblTeams ON tblTeamsDepartments.ID = tblTeams.Department) INNER JOIN (tblValueChain01 INNER JOIN ((tblValueChain03 INNER JOIN tblValueChain02 ON tblValueChain03.IDMacroProcess = tblValueChain02.IDMicroProcesso02) INNER JOIN tblDependencies01 ON tblValueChain03.ID = tblDependencies01.IDSubProcess) ON tblValueChain01.IDMacroProcesso = tblValueChain02.IDMacroProcesso01) ON tblTeams.ID = tblDependencies01.Group WHERE [tblDependencies01].[ID]= '" & ID & "'"
Form_frmValueChainDynamic01.Form.RecordSource = sql_get
Possible typo:
...
INNER JOIN tblValueChain02 ON tblValueChain03.IDMacroProcess = tblValueChain02.IDMicroProcesso02)`
...
...perhaps should be IDMacroProcesso (missing "o" at the end of this field)?
Is [tblDependencies01].[ID] a number field? If so, then in your WHERE clause you've used single quotes denoting you're expecting to match a string; so this:
WHERE [tblDependencies01].[ID]= '" & ID & "'"
...should be written like this, if ID is a number:
WHERE [tblDependencies01].[ID]=" & ID
You've referred to most fields in your SELECT clause using the tableName.fieldName convention except for PotSolution (this may not be an issue, but it's good to be consistent!)
Some more things to try if the above doesn't work: http://allenbrowne.com/subquery-02.html#QueryTooComplex

SQLite terribly slow compared to MS Access

Following the advice of fellow SO-ers, I converted an MS Access database I had (a small one, for test reasons) to SQLite. It has two tables, one with 5k entries and another with 50k entries.
Now, the queries I will present below QuLimma and QLexeis took about 60ms (total time of the function below) with Access, but a whopping 830ms with SQLite.
Dim i As Integer
Dim ms As Integer
ResultPin(0) = ""
ResultPin(1) = ""
ResultPin(2) = ""
ResultPin(3) = ""
ResultPin(4) = ""
i = 0
Multichoice = 0
ms = 0
Dim rsTblEntries As ADODB.Recordset
Set rsTblEntries = New ADODB.Recordset
Dim QuLimma As String, QLexeis As String
QuLimma = "SELECT Words.limma, Words.limmabody, Words.limmapro " & _
"FROM Words " & _
"GROUP BY Words.limma, Words.limmabody, Words.limmapro " & _
"HAVING (((Words.limma)='" & StrLexeis & "'));"
QLexeis = "SELECT Limma.limmalexeis, Words.limma, Limma.limmabody, Words.limmapro, Limma.limmaexp " & _
"FROM Limma INNER JOIN Words ON Limma.limmabody = Words.limmabody " & _
"GROUP BY Limma.limmalexeis, Words.limma, Limma.limmabody, Words.limmapro, Limma.limmaexp " & _
"HAVING (((Limma.limmalexeis)='" & StrLexeis & "'));"
rsTblEntries.Open QuLimma, CnDataParSQLite ', adOpenStatic, adLockOptimistic
If rsTblEntries.EOF = True Then
rsTblEntries.Close
rsTblEntries.Open QLexeis, CnDataParSQLite ', adOpenStatic, adLockOptimistic
If rsTblEntries.EOF = True Then
SearchQParagSQLite = False
Else
SearchQParagSQLite = True
Do While rsTblEntries.EOF = False
ms = ms + 1
rsTblEntries.MoveNext
Loop
rsTblEntries.MoveFirst
If ms > 1 Then
Do While rsTblEntries.EOF = False
ResultTemp(0, i) = rsTblEntries.Fields("limma").Value & "" 'rsWordPar!limma
ResultTemp(1, i) = rsTblEntries.Fields("limmalexeis").Value & "" 'rsWordPar!limmalexeis
ResultTemp(2, i) = rsTblEntries.Fields("limmabody").Value 'rsWordPar!limmabody
If IsNull(rsTblEntries.Fields("limmapro").Value) = False Then ResultTemp(3, i) = rsTblEntries.Fields("limmapro").Value 'rsWordPar!limmapro
rsTblEntries.MoveNext
i = i + 1
Multichoice = 1
Loop
Else
Do While rsTblEntries.EOF = False
ResultPin(0) = rsTblEntries.Fields("limma").Value & "" 'rsWordPar!limma
ResultPin(1) = rsTblEntries.Fields("limmalexeis").Value & "" 'rsWordPar!limmalexeis
ResultPin(2) = rsTblEntries.Fields("limmabody").Value 'rsWordPar!limmabody
If IsNull(rsTblEntries.Fields("limmapro").Value) = False Then ResultPin(3) = rsTblEntries.Fields("limmapro").Value 'rsWordPar!limmapro
rsTblEntries.MoveNext
Multichoice = 0
Loop
End If
End If
Else
SearchQParagSQLite = True
rsTblEntries.MoveFirst
Do While rsTblEntries.EOF = False
ResultPin(0) = rsTblEntries.Fields("limma").Value & "" 'rsWordPar!limma
ResultPin(1) = "#"
ResultPin(2) = rsTblEntries.Fields("limmabody").Value 'rsWordPar!limmabody
If IsNull(rsTblEntries.Fields("limmapro").Value) = False Then ResultPin(3) = rsTblEntries.Fields("limmapro").Value 'rsWordPar!limmapro
rsTblEntries.MoveNext
i = i + 1
Loop
End If
i = 0
rsTblEntries.Close
Set rsTblEntries = Nothing
With connection string:
CnDataParSQLite.ConnectionString = "DRIVER=SQLite3 ODBC Driver;" & _
"Database=" & strDataPath & "u.sl3;LongNames=0;Timeout=1000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;"
CnDataParSQLite.Open
Now, before someone asks "wasn't 60ms fast enough?", I'd like to say that I did this because I have other Access files and queries which take 3-4 seconds and would like to lower them down, so yes, I was hoping to go down from 60ms to 30 or less in this one.
Do I have a misconfiguration or is it just that SQLite is not faster? I have checked, both return correct results, there is no weird looping issue.
Edit: most of the time is consumed by the second query.
Edit 2: (copy/paste from the db.sql)
Table Limma:
CREATE TABLE Limma ( id INTEGER PRIMARY KEY, limmabody INTEGER DEFAULT 0, limmalexeis VARCHAR2(100), limmastat VARCHAR2(50), limmaexp VARCHAR2(250));
INSERT INTO Limma VALUES (1, 1, 'υψικάμινος', 'ΣΠ', NULL);
INSERT INTO Limma VALUES (2, 1, 'υψίκορμος', 'ΣΠ', NULL);
INSERT INTO Limma VALUES (3, 1, 'υψίπεδο', 'ΑΠ', '<αρχ. υψίπεδον, ουδ. του επιθ. υψίπεδος<ύψι "ψηλά" + πέδον');
Total: 64k entries
Table Words:
CREATE TABLE Words ( id INTEGER PRIMARY KEY, limma VARCHAR2(100), limmabody INTEGER DEFAULT 0, limmapro VARCHAR2(200));
INSERT INTO Words VALUES (1, 'υψι (αχώριστο μόριο)', 1, NULL);
INSERT INTO Words VALUES (2, 'ομο (αχώριστο μόριο)', 2, NULL);
INSERT INTO Words VALUES (3, 'διχο (αχώριστο μόριο)', 3, NULL);
Total: 6k entries
The first field "id" is unique.
You almost never want to use HAVING where you can use WHERE criteria. You're evaluating all possible results and then culling them down after aggregation. You mainly want to use HAVING criteria where you're trying to cull down based upon the aggregated results. You can achieve the same thing by moving the HAVING logic to a WHERE criteria before the aggregation in this case. This should greatly speed up your query.
There is also no need to use GROUP BY logic since you're not returning any aggregates, just use DISTINCT.
I would write it like this:
QuLimma = "SELECT DISTINCT Words.limma, Words.limmabody, Words.limmapro " & _
"FROM Words " & _
"WHERE Words.limma ='" & StrLexeis & "';"
QLexeis = "SELECT DISTINCT Limma.limmalexeis, Words.limma, Limma.limmabody, Words.limmapro, Limma.limmaexp " & _
"FROM Limma INNER JOIN Words ON Limma.limmabody = Words.limmabody " & _
"WHERE Limma.limmalexeis ='" & StrLexeis & "';"
For these two queries with your table schema these indexes should optimize the queries:
CREATE NONCLUSTERED INDEX ix_words_1 ON Words (Limma) INCLUDE (Limmabody, Limmapro)
CREATE NONCLUSTERED INDEX ix_words_2 ON Words (Limmabody) INCLUDE (Limma, Limmapro)
CREATE NONCLUSTERED INDEX ix_limma_1 ON Limma (Limmabody, Limmalexeis) INCLUDE (Limmaexp)
Keep in mind there is a cost at the time of insert for each additional index you have. You have to weigh this cost against the benefit of the index. If your tables contain static data then there is no harm.