Note that there are some explanatory texts on larger screens.

plurals
  1. POGrouping data slows query despite 25% decrease in the number of results
    primarykey
    data
    text
    <p>I have an Access database of 4M rows, each representing an individual customer order.</p> <p>I need to run a query from Excel (I use VBA) in order to retrieve only the orders from customers in <code>REGION1</code>.</p> <p>I tried the following (names should be pretty self-explanatory):</p> <pre><code>Sub Query() Dim cn As Object Dim strFile As String Dim strCon As String Dim strSQL As String strFile = "C:\Users\MyName\Desktop\DataBase.accdb" strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &amp; strFile Set cn = CreateObject("ADODB.Connection") Set rs = CreateObject("ADODB.Recordset") cn.Open strCon strSQL = "SELECT [CUSTOMER], [DATE], [REVENUE]" _ &amp; "FROM [SALES DB]" _ &amp; "WHERE [REGION]='REGION1'" rs.Open strSQL, cn, 0, 1 Worksheets(1).Cells(2, 1).CopyFromRecordset rs rs.Close Set rs = Nothing cn.Close Set cn = Nothing End Sub </code></pre> <p>This works nicely but it's a bit slow, as it returns ~600k rows.</p> <p>So I thought: "Who cares about the detailed list of all customer orders? I just need the monthly aggregate. This should reduce the number of returned rows and hence inprove speed!".</p> <p>So I changed my code to:</p> <pre><code>strSQL = "SELECT [CUSTOMER], MONTH([DATE]), YEAR([DATE]), SUM([REVENUE])" _ &amp; "FROM [SALES DB]" _ &amp; "WHERE [REGION]='REGION1'" &amp; "GROUP BY [CUSTOMER], MONTH([DATE]), YEAR([DATE])" </code></pre> <p>As I expected, now only ~450K results show up. The thing is, the query actually became slower.</p> <p>I'm actually better off extracting the ungrouped data and then aggregating it with a simple pivot table.</p> <p>How can less data be slower to extract? I know there's some calculations to be performed in between, but still. </p> <p>Does anybody out there have any idea how I can overcome this problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload