haskell-beam / haskell-beam/beam
Optimisation for truncating a table.
- Dominant language
- Haskell
- Stars
- 635
- Forks
- 193
- PR merge metrics
- No merged PRs in 30d
Description
As https://github.com/haskell-beam/beam/issues/370 shows the way to truncate a table is
```haskell
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?)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.