Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP)
<p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>

<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>

<p>Is this normal behaviour when using a SQL database?</p>

<p>The schema (the table holds responses to a survey):</p>

<pre><code>CREATE TABLE tuples (id integer primary key, a integer, b integer, c integer, d integer);

\copy tuples from '350,000 responses.csv' delimiter as ','
</code></pre>

<p>I wrote some tests in Java and Python for context and they crush SQL (except for pure python):</p>

<pre><code>java   1.5 threads ~ 7 ms    
java   1.5         ~ 10 ms    
python 2.5 numpy   ~ 18 ms  
python 2.5         ~ 370 ms
</code></pre>

<p>Even sqlite3 is competitive with Postgres despite it assumping all columns are strings (for contrast: even using just switching to numeric columns instead of integers in Postgres results in 10x slowdown)</p>

<p>Tunings i've tried without success include (blindly following some web advice):</p>

<pre><code>increased the shared memory available to Postgres to 256MB    
increased the working memory to 2MB
disabled connection and statement logging
used a stored procedure via CREATE FUNCTION ... LANGUAGE SQL
</code></pre>

<p>So my question is, is my experience here normal, and this is what I can expect when using a SQL database?  I can understand that ACID must come with costs, but this is kind of crazy in my opinion.  I'm not asking for realtime game speed, but since Java can process millions of doubles in under 20 ms, I feel a bit jealous. </p>

<p>Is there a better way to do simple OLAP on the cheap (both in terms of money and server complexity)?  I've looked into Mondrian and Pig + Hadoop but not super excited about maintaining yet another server application and not sure if they would even help.</p>

<hr>

<p>No the Python code and Java code do all the work in house so to speak.  I just generate 4 arrays with 350,000 random values each, then take the average.  I don't include the generation in the timings, only the averaging step.  The java threads timing uses 4 threads (one per array average), overkill but it's definitely the fastest.</p>

<p>The sqlite3 timing is driven by the Python program and is running from disk (not :memory:)</p>

<p>I realize Postgres is doing much more behind the scenes, but most of that work doesn't matter to me since this is read only data.</p>

<p>The Postgres query doesn't change timing on subsequent runs.</p>

<p>I've rerun the Python tests to include spooling it off the disk.  The timing slows down considerably to nearly 4 secs.  But I'm guessing that Python's file handling code is pretty much in C (though maybe not the csv lib?) so this indicates to me that Postgres isn't streaming from the disk either (or that you are correct and I should bow down before whoever wrote their storage layer!)</p>

<p>I would say your test scheme is not really useful. To fulfill the db query, the db server goes through several steps:</p>

<ol>
<li>parse the SQL</li>
<li>work up a query plan, i. e. decide on which indices to use (if any), optimize etc.</li>
<li>if an index is used, search it for the pointers to the actual data, then go to the appropriate location in the data or</li>
<li>if no index is used, scan <i>the whole table</i> to determine which rows are needed</li>
<li>load the data from disk into a temporary location (hopefully, but not necessarily, memory)</li>
<li>perform the count() and avg() calculations</li>
</ol>

<p>So, creating an array in Python and getting the average basically skips all these steps save the last one. As disk I/O is among the most expensive operations a program has to perform, this is a major flaw in the test (see also the answers to <a href="http://stackoverflow.com/questions/26021/how-is-data-compression-more-effective-than-indexing-for-search-performance">this question</a> I asked here before). Even if you read the data from disk in your other test, the process is completely different and it's hard to tell how relevant the results are.</p>

<p>To obtain more information about where Postgres spends its time, I would suggest the following tests:</p>

<ul>
<li>Compare the execution time of your query to a SELECT without the aggregating functions (i. e. cut step 5)</li>
<li>If you find that the aggregation leads to a significant slowdown, try if Python does it faster, obtaining the raw data through the plain SELECT from the comparison.</li>
</ul>

<p>To speed up your query, reduce disk access first. I doubt very much that it's the aggregation that takes the time.</p>

<p>There's several ways to do that:</p>

<ul>
<li>Cache data (in memory!) for subsequent access, either via the db engine's own capabilities or with tools like memcached</li>
<li>Reduce the size of your stored data</li>
<li>Optimize the use of indices. Sometimes this can mean to skip index use altogether (after all, it's disk access, too). For MySQL, I seem to remember that it's recommended to skip indices if you assume that the query fetches more than 10% of all the data in the table.</li>
<li>If your query makes good use of indices, I know that for MySQL databases it helps to put indices and data on separate physical disks. However, I don't know whether that's applicable for Postgres.</li>
<li>There also might be more sophisticated problems such as swapping rows to disk if for some reason the result set can't be completely processed in memory. But I would leave that kind of research until I run into serious performance problems that I can't find another way to fix, as it requires knowledge about a lot of little under-the-hood details in your process.</li>
</ul>

<p><b>Update:</b></p>

<p><i>I just realized that you seem to have no use for indices for the above query and most likely aren't using any, too, so my advice on indices probably wasn't helpful. Sorry. Still, I'd say that the aggregation is not the problem but disk access is. I'll leave the index stuff in, anyway, it might still have some use.</i></p>
