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

Query with RETURNING clause is extremely slow to compile #2083

Closed
j-tai opened this issue Sep 2, 2022 · 2 comments
Closed

Query with RETURNING clause is extremely slow to compile #2083

j-tai opened this issue Sep 2, 2022 · 2 comments
Labels

Comments

@j-tai
Copy link

j-tai commented Sep 2, 2022

Bug Description

The following query, when used with query_scalar!, takes a very long time to compile.

INSERT INTO users (username, password_hash) VALUES (?, ?) RETURNING id

On my laptop, this takes 39.6 seconds to compile. If I remove the RETURNING clause, then it takes 1.1 seconds.

Also, rustc uses 1.8 GB of memory, which I don't think is normal.

Minimal Reproduction

  1. git clone https://github.com/j-tai/sqlx-repro
  2. cd sqlx-repro
  3. sqlite3 db.sqlite '.read dump.sql' (create the database, this is necessary to reproduce)
  4. cargo build
    → Compiling the sqlx-repro crate takes a very long time.
  5. Edit src/main.rs (e.g., add a comment or something)
  6. cargo build
    → Compiling takes a very long time again.

Output of EXPLAIN

sqlite> EXPLAIN INSERT INTO users (username, password_hash) VALUES (?, ?) RETURNING id;
addr  opcode         p1    p2    p3    p4             p5  comment      
----  -------------  ----  ----  ----  -------------  --  -------------
0     Init           0     31    0                    0   Start at 31
1     OpenWrite      0     2     0     5              0   root=2 iDb=0; users
2     OpenWrite      1     3     0     k(1,)          0   root=3 iDb=0; sqlite_autoindex_users_1
3     SoftNull       2     0     0                    0   r[2]=NULL
4     Variable       1     3     0                    0   r[3]=parameter(1,)
5     Variable       2     4     0                    0   r[4]=parameter(2,)
6     Function       0     0     5     current_timestamp(0)  0   r[5]=func()
7     NewRowid       0     1     0                    0   r[1]=rowid
8     HaltIfNull     1299  2     3     users.username  1   if r[3]=null halt
9     HaltIfNull     1299  2     4     users.password_hash  1   if r[4]=null halt
10    HaltIfNull     1299  2     5     users.created  1   if r[5]=null halt
11    HaltIfNull     1299  2     6     users.privacy  1   if r[6]=null halt
12    Affinity       2     5     0     DBBCC          0   affinity(r[2..6])
13    Noop           0     0     0                    0   prep index sqlite_autoindex_users_1
14    SCopy          3     8     0                    0   r[8]=r[3]; username
15    IntCopy        1     9     0                    0   r[9]=r[1]; rowid
16    MakeRecord     8     2     7                    0   r[7]=mkrec(r[8..9]); for sqlite_autoindex_users_1
17    NoConflict     1     19    8     1              0   key=r[8]
18    Halt           2067  2     0     users.username  2   
19    MakeRecord     2     5     10                   0   r[10]=mkrec(r[2..6])
20    IdxInsert      1     7     8     1              16  key=r[7]
21    Insert         0     10    1     users          57  intkey=r[1] data=r[10]
22    SCopy          1     12    0                    0   r[12]=r[1]
23    MakeRecord     12    1     13                   0   r[13]=mkrec(r[12])
24    NewRowid       3     14    0                    0   r[14]=rowid
25    Insert         3     13    14                   0   intkey=r[14] data=r[13]
26    Rewind         3     30    0                    0   
27      Column         3     0     12                   0   r[12]=
28      ResultRow      12    1     0                    0   output=r[12]
29    Next           3     27    0                    0   
30    Halt           0     0     0                    0   
31    Transaction    0     1     10    0              1   usesStmtJournal=1
32    Integer        0     6     0                    0   r[6]=0
33    OpenEphemeral  3     1     0                    0   nColumn=1
34    Goto           0     1     0                    0

Info

  • SQLx version: 0.6.1
  • SQLx features enabled: "runtime-tokio-native-tls", "sqlite", "chrono", "migrate", "macros", "offline"
  • Database server and version: SQLite 3.37.2 2022-01-06 13:25:41 872ba256cbf61d9290b571c0e6d82a20c224ca3ad82971edc46b29818d5dalt1
  • Operating system: Pop!_OS 22.04 LTS
  • rustc --version: rustc 1.63.0 (4b91a6ea7 2022-08-08)
@j-tai j-tai added the bug label Sep 2, 2022
@abonander
Copy link
Collaborator

Duplicate of #1921

There's a fix in #1946, was waiting on a response from the author.

@abonander abonander closed this as not planned Won't fix, can't repro, duplicate, stale Sep 3, 2022
@j-tai
Copy link
Author

j-tai commented Sep 3, 2022

Thanks. Did a lot of searches and somehow didn't find that.

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

No branches or pull requests

2 participants