Skip to content

Low performance on subqueries #2280

Closed

Description

@mathiasrw

We are running this in test. We are getting results like 7.82 ops/s where most other tests we are doing are things like 2.40K ops/s or 8.36K ops/s

It indicates there is something terribly wrong. Very wrong. I understand that we got no indexes on, but can this really be right?

look into if index adding in the example is really the only thing we can do or if there are some clear performance improvements we can do in the code that would spit up such a scenario. Or in worst case: can we log a warning if we detect that a massive workload seems to be entering the pipeliens and no indexes on the right things, basically warning the user this will take a long time and probably could improve from indexes on x, y, z to improve performance.

  // Test 5: Subqueries
  {
    name: 'Subquery Operation',
    description: 'SELECT with subquery for filtering',
    setup: (alasql) => {
      alasql('CREATE TABLE IF NOT EXISTS users (id INT, name STRING, age INT, department STRING, salary NUMBER)');
      alasql('CREATE TABLE IF NOT EXISTS orders (id INT, userId INT, product STRING, amount NUMBER, date STRING)');
      
      const users = generateUsers(1000);
      const orders = generateOrders(3000, 1000);
      
      alasql('DELETE FROM users');
      alasql('DELETE FROM orders');
      
      users.forEach(u => alasql('INSERT INTO users VALUES (?, ?, ?, ?, ?)', [u.id, u.name, u.age, u.department, u.salary]));
      orders.forEach(o => alasql('INSERT INTO orders VALUES (?, ?, ?, ?, ?)', [o.id, o.userId, o.product, o.amount, o.date]));
    },
    run: (alasql) => {
      return alasql('SELECT * FROM users WHERE id IN (SELECT userId FROM orders WHERE amount > 800)');
    },
    teardown: (alasql) => {
      alasql('DROP TABLE IF EXISTS users');
      alasql('DROP TABLE IF EXISTS orders');
    },
  },

Activity

  1. mathiasrw commented on Nov 30, 2025

    @mathiasrw
    MemberAuthor

    Problem

    IN (SELECT ...) and NOT IN (SELECT ...) subqueries were re-executed for every row in the outer table, resulting in O(n×m) complexity. This caused ~7.82 ops/s for 1000 users × 3000 orders scenario—orders of magnitude slower than expected.

    Solution

    • Cache subquery results: Execute subquery once on first access, store in a subqueryCache object on the query
    • Use Set for O(1) lookups: Replace indexOf() with Set.has() for constant-time membership checks
    • Clear cache between executions: Reset subqueryCache = {} at start of queryfn() to ensure fresh data on each query run

    Changes

    • src/50expression.js: Lazy-cache subquery results as Set for IN/NOT IN operators
    • src/38query.js: Reset single subqueryCache object at query execution start
    • test/test2987.js: Tests for correctness and cache isolation

    Performance

    Before After Improvement
    ~7.82 ops/s ~900-1250 ops/s ~100-160× faster
    -- This query type is now significantly faster
    SELECT * FROM users WHERE id IN (SELECT userId FROM orders WHERE amount > 800)
  2. added a commit that references this issue on Dec 3, 2025
    6497825
  3. mathiasrw commented on Dec 6, 2025

    @mathiasrw
    MemberAuthor

    Fix released in v4.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions