On 6/8/17, s1341 <[email protected]> wrote:
> Hi all,
>
> I've recently been looking into the implementation of sqlite3. I've added
> some prints to instrument various parts of the implementation in order to
> get a better picture of how things work.
>
> I've noticed some strange behaviour when performing ALTER TABLE ADD COLUMN
> queries. It appears that sqlite3AddColumn (in build.c) is called for all
> existing columns in the table first, and only then for the new column.
>
> I would have expected that sqlite3AddColumn would only be called once, for
> the new column.
>
> This behaviour has performance implications, especially with tables which
> have a large number of columns.
>
> Is this expected behaviour? If so, why? If not, is it a bug?

This is expected behavior.  It is not a bug.  There are no
(significant) performance implications.

The way ALTER TABLE ADD COLUMN works is that it takes the original
CREATE TABLE text and modifies it to insert the new column.  It then
discards its internal symbol table for that table and reparses the
modified CREATE TABLE text to rebuild that symbol table.  The
sqlite3AddColumn() routine is a method in the parser that processes
each column name encountered while the CREATE TABLE statement is being
reparsed.

The parser takes a trivial amount of time.  And ALTER TABLE is an
uncommon SQL statement.  We could, perhaps, make ALTER TABLE ADD
COLUMN run slightly faster by avoiding the reparse, but that would add
complexity and the resulting speed increase would be too small to
measure.

-- 
D. Richard Hipp
[email protected]
_______________________________________________
sqlite-users mailing list
[email protected]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to