Is sqlite3 smart enough to remove redundant command?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Is sqlite3 smart enough to remove redundant command?

Peng Yu
Suppose that I have the following command, which writes two entries
with the same key. So the 1st entry will be overwritten by the 2nd
entry. Therefore, there is no need to write the 1st entry. Is sqlite3
smart enough to not to write the 1st entry? Or it will write both the
1st entry and the 2nd entry? Thanks.

conn=sqlite3.connect("my.db")
c=conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS sqlar(
name TEXT PRIMARY KEY
, mode INT
, mtime INT
, sz INT
, data BLOB)
''')
c.execute('REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)', ["a", 0, 0, 1, "1"])
c.execute('REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)', ["a", 0, 0, 1, "2"])
conn.commit()

--
Regards,
Peng
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Is sqlite3 smart enough to remove redundant command?

Igor Tandetnik-2
On 1/29/2020 12:04 AM, Peng Yu wrote:

> Suppose that I have the following command, which writes two entries
> with the same key. So the 1st entry will be overwritten by the 2nd
> entry. Therefore, there is no need to write the 1st entry. Is sqlite3
> smart enough to not to write the 1st entry? Or it will write both the
> 1st entry and the 2nd entry? Thanks.
>
> conn=sqlite3.connect("my.db")
> c=conn.cursor()
> c.execute('''
> CREATE TABLE IF NOT EXISTS sqlar(
> name TEXT PRIMARY KEY
> , mode INT
> , mtime INT
> , sz INT
> , data BLOB)
> ''')
> c.execute('REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)', ["a", 0, 0, 1, "1"])
> c.execute('REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)', ["a", 0, 0, 1, "2"])
> conn.commit()

Do you expect the first c.execute() call to look into the future and somehow know that the second c.execute() call is coming next?
--
Igor Tandetnik


_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Is sqlite3 smart enough to remove redundant command?

David Raymond
In reply to this post by Peng Yu
It's going to run both since you asked it to. Even if it was in a compiled language where the compiler could look ahead and was looking to implement that sort of optimization, then for example there still might be triggers on the table which would need to be run, or other constraints on the fields which might get triggered by the second statement and not the first. There's no way it's going to know that without actually running them both.


-----Original Message-----
From: sqlite-users <[hidden email]> On Behalf Of Peng Yu
Sent: Wednesday, January 29, 2020 12:04 AM
To: SQLite mailing list <[hidden email]>
Subject: [sqlite] Is sqlite3 smart enough to remove redundant command?

Suppose that I have the following command, which writes two entries
with the same key. So the 1st entry will be overwritten by the 2nd
entry. Therefore, there is no need to write the 1st entry. Is sqlite3
smart enough to not to write the 1st entry? Or it will write both the
1st entry and the 2nd entry? Thanks.

conn=sqlite3.connect("my.db")
c=conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS sqlar(
name TEXT PRIMARY KEY
, mode INT
, mtime INT
, sz INT
, data BLOB)
''')
c.execute('REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)', ["a", 0, 0, 1, "1"])
c.execute('REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)', ["a", 0, 0, 1, "2"])
conn.commit()

--
Regards,
Peng
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users