SQL BenchMEASURED, NOT GUESSED

    ↑↓ to select, Enter to open, Esc to close

    dict merge in Python is ~1.19× faster than JOIN in SQL

    At 30,000 rows, merging in Python is 19% faster than a SQL JOIN — the join table has only 5 rows, so pushing everything into SQL is not automatically right.

    Result

    Rundict merge in PythonJOIN in SQLRatio
    Run 132.10338.1330.84×
    Run 234.15639.7890.86×
    Run 331.93038.2980.83×

    Spread over 3 runs: dict merge in Python CV 3.1% / JOIN in SQL CV 1.9% / Ratio CV 1.2%

    dict merge in Python: 32.10msJOIN in SQL: 38.13msRun1dict merge in Python: 34.16msJOIN in SQL: 39.79msRun2dict merge in Python: 31.93msJOIN in SQL: 38.30msRun3

    The statement

    tags = dict(con.execute('SELECT tag,label FROM tags'))
    rows = con.execute('SELECT id,tag FROM items LIMIT 30000').fetchall()
    sum(1 for _i,t in rows if tags.get(t)=='BETA')
    SELECT COUNT(*) FROM items i JOIN tags t ON i.tag=t.tag WHERE t.label='BETA'

    Conditions

    30,000 of 100,000 `items` rows against a 5-row `tags` table.

    Where this number does not apply

    With a larger join table, or further SQL filtering, JOIN wins.

    Reproduce

    cd bench && python sqbench.py --samples 9 --out ../data/run.json