Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimisation for truncating a table. #591

Open
sjoerdvisscher opened this issue Sep 23, 2021 · 0 comments
Open

Optimisation for truncating a table. #591

sjoerdvisscher opened this issue Sep 23, 2021 · 0 comments

Comments

@sjoerdvisscher
Copy link

As #370 shows the way to truncate a table is

delete table (const $ val_ True)

And I agree it is good to always require a where, in haskell! But it would be nice if the generated SQL statement will be optimised when (const $ val_ True) is used, because then the database (at least sqlite) can optimise it heavily.

This is the current generated sql:

sqlite> explain delete from txs where 1;
addr  opcode         p1    p2    p3    p4             p5  comment      
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     13    0                    0   Start at 13
1     Null           0     1     0                    0   r[1]=NULL
2     OpenWrite      0     8     0     2              0   root=8 iDb=0; txs
3     Explain        3     0     0     SCAN TABLE txs  0   
4     Rewind         0     12    0                    0   
5       Rowid          0     2     0                    0   r[2]=rowid
6       Once           0     8     0                    0   
7       OpenWrite      1     9     0     k(1,)          8   root=9 iDb=0; sqlite_autoindex_txs_1
8       Column         0     0     3                    0   r[3]=txs.row_tx_id
9       IdxDelete      1     3     1                    1   key=r[3]
10      Delete         0     1     0     txs            2   
11    Next           0     5     0                    1   
12    Halt           0     0     0                    0   
13    Transaction    0     1     5     0              1   usesStmtJournal=0
14    Goto           0     1     0                    0   

See the difference when the where clause is not there:

sqlite> explain delete from txs;
addr  opcode         p1    p2    p3    p4             p5  comment      
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     4     0                    0   Start at 4
1     Clear          8     0     -1    txs            0   
2     Clear          9     0     0                    0   
3     Halt           0     0     0                    0   
4     Transaction    0     1     5     0              1   usesStmtJournal=0
5     Goto           0     1     0                    0   

So I wonder if beam could just drop the where clause if the expression is just 1? Or maybe there's a good reason for it (otherwise sqlite could have done the same optimisation?)

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

No branches or pull requests

2 participants