title: Difference in SQLite query times
body: <p>When I run query from within C and in "DB Brownser for SQLite" I get wildly different query times. Here is an example:</p>
  
  <pre><code>sqlite3 *db;
  int rc = sqlite3_open("New.db", &amp;db);
  
  // Execute
  clock_t t0 = clock();
  sqlite3_stmt *stmt;
  rc = sqlite3_prepare(db, "SELECT * FROM Sales1M s1 join Sales1M s2 on (s1.id=s2.id) GROUP BY s1.territory_id, s2.code", -1, &amp;stmt, NULL);
  clock_t t1 = clock();
  
  // Fetch
  int rownum=1;
  while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
      // printf("Row: %d | Code: %s | TerritoryID: %s\n", rownum, sqlite3_column_text(stmt, 4), sqlite3_column_text(stmt, 3));
      rownum++;
  }
  clock_t t2 = clock();
  
  printf("ExecuteSQL: %.6f | FetchSQL: %.6f\n" ...&lt;some other info&gt;);
  </code></pre>
  
  <blockquote>
    <p>ExecuteSQL: 0.000451 | FetchSQL: 6.510507 (520 rows returned)</p>
  </blockquote>
  
  <p>And in the client (DB Browser) that exact same query takes 35s. Here's a visual comparison:</p>
  
  <p><a href="https://i.stack.imgur.com/3qpUf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3qpUf.png" alt="enter image description here"></a></p>
  
  <p>I've run the query multiple times in each and every time it takes about the same time. What accounts for the drastic difference in these two identical queries?</p>