Re: [sqlite] [EXTERNAL] Re: Remove row to insert new one on a full database

2019-04-07 Thread Hick Gunter
First, your rows are NOT "all the same size". SQLite uses a compressed format 
to store values that, among other things, uses less space for smaller integers.

IIRC your records will be (values before colon are the "manifest", values after 
the colon are the "payload" of a record):
(value is 0) (value is 1) (value is INT) (value is INT) : (2) (3) = 6 bytes in 
length
(value is 1) (value is 1) (value is INT) (value is INT) : (2) (3) = 6 bytes in 
length
(value is INT) (value is 1) (value is INT) (value is INT) : (2) (2) (3) = 7 
bytes in length
...
(value is INT) (value is 1) (value is INT) (value is INT) : (127) (2) (3) = 7 
bytes in length
(value is INT) (value is 1) (value is INT) (value is INT) : (127) (2) (3) = 8 
bytes in length

Second, a BTree stores records in the leaf pages only, in sort order and so 
that the last record of any page sorts before the first record of the next 
page. Thus the "parent" page only needs to store the first key of it's "child" 
pages. When a page is completely filled up, it needs to be split. If sorted 
inserts (like you are performing) are detected, literature recommends splitting 
90/10 so the resulting pages willl be 90% full; random inserts result in 50/50 
splits, since it is equally likely which side of the spilt the next insert is 
going to be.

Deleting a record only allows space to be re-used in a limited number of cases:
a) the record to be inserted "belongs" on the same page as the record deleted
b) the record to be deleted is the last one on a leaf page (and BTree software 
is set up to re-use leaf pages automatically)
c) the record to be deleted causes an internal page to become empty (and BTree 
Software is set up to re-use internal pages automatically)
d) the record causes 2 adjacent pages to hold less records than would fit into 
1 page (and BTree Software is set up to merge adjacent pages automatically)
e) the BTree structure is "defragmented" by whatever method implemented 
(implicit reload and/or cases b to d)

With your algorithm of deleting the oldest record(s) to make space for new 
records at "the other end" of the list, you are excluding case a and relying on 
some combination of b thru d for re-use of pages.

Similar considerations apply to the pages holding the index created by the 
PRIMARY KEY clause, but the key records are smaller and data is stored in 
internal nodes, so the effects are smaller..

Note the pattern:

- After the file is "full", you need a new page every 84 records (this will 
change when your test exceeds 32k records)
- The number of records required to free a page drops from 109 (#of records on 
first page) to 84 (#of records on sixth thru last used page)

With a "fan out" of 84, your BTree probably has a leaf level of 60 pages of 84 
records each, plus 1 internal level of 1 node holding 60 pointers to leaf pages.

>Von: sqlite-users [mailto:sqlite-users-boun...@mailinglists.sqlite.org] Im 
>Auftrag von Arthur Blondel
>...
>OK, I wasn't clear.
>I'm limited in space so when the DB is full (when sqlite3_exec() returns 
>SQLITE_FULL when I try to insert a new row), I remove the oldest row and retry 
>to insert the new one.
>The data is always the same. That's why removing one row should be enough to 
>insert a new one.
>My problem is that some times I need to remove many rows to add one new one.
>...
>Following the output:
>
>...
>3959 - DB full
>3960 - 109 rows was removed
>4044 - 92 rows was removed
>4128 - 86 rows was removed
>4212 - 85 rows was removed
>4296 - 85 rows was removed
>4380 - 84 rows was removed
>4464 - 84 rows was removed
>4548 - 84 rows was removed
>4632 - 84 rows was removed
>4716 - 84 rows was removed
>4800 - 84 rows was removed
>4884 - 84 rows was removed
>4968 - 84 rows was removed
>...


___
 Gunter Hick | Software Engineer | Scientific Games International GmbH | 
Klitschgasse 2-4, A-1130 Vienna | FN 157284 a, HG Wien, DVR: 0430013 | (O) +43 
1 80100 - 0

May be privileged. May be confidential. Please delete if not the addressee.
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] [EXTERNAL] Prepared Statement Without a Database Instance

2019-04-07 Thread Hick Gunter
No. The schema is required to correctly compile the statement, and if the 
schema changes, then the statement needs to be recompiled.

IOW: The schema, in the version as it existed at prepare time, is an integral 
part of the prepared statement.

-Ursprüngliche Nachricht-
Von: sqlite-users [mailto:sqlite-users-boun...@mailinglists.sqlite.org] Im 
Auftrag von Lee, Jason
Gesendet: Freitag, 05. April 2019 20:08
An: sqlite-users@mailinglists.sqlite.org
Betreff: [EXTERNAL] [sqlite] Prepared Statement Without a Database Instance

Hi. Is there any way to create a prepared statement without an associated 
database instance? I have one fixed insert that I am running on many separate 
databases that are inserting different data sets, and would like to amortize 
the cost of compiling the insert if possible.


Jason Lee
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users.


___
 Gunter Hick | Software Engineer | Scientific Games International GmbH | 
Klitschgasse 2-4, A-1130 Vienna | FN 157284 a, HG Wien, DVR: 0430013 | (O) +43 
1 80100 - 0

May be privileged. May be confidential. Please delete if not the addressee.
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] Optimization corner case with IS?

2019-04-07 Thread Wout Mertens
I noticed this, IS is not treated like = for optimization:

SQLITE> CREATE TABLE t(f INTEGER);
SQLITE> CREATE INDEX t_f ON t(f) WHERE f IS NOT NULL;
SQLITE> EXPLAIN QUERY PLAN SELECT * FROM t WHERE f = 1;
QUERY PLAN
`--SEARCH TABLE t USING COVERING INDEX t_f (f=?)
SQLITE> EXPLAIN QUERY PLAN SELECT * FROM t WHERE f IS 1;
QUERY PLAN
`--SCAN TABLE t
SQLITE> EXPLAIN QUERY PLAN SELECT * FROM t WHERE f IS 1 AND f IS NOT NULL;
QUERY PLAN
`--SEARCH TABLE t USING COVERING INDEX t_f (f=?)

Wout.
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] Bug report for rtree.c

2019-04-07 Thread richard parkins
If SQLITE_DEBUG is not defined, rtree.c found at 
https://www.sqlite.org/src/dir?ci=edb095a9a679c8c7&name=ext/rtree fails to 
compile.
This is because bCorrupt is declared (at line 132) only if SQLITE_DEBUG is 
defined, but is referenced unconditionally at line 980.
Line 980 currently is    assert( pRtree->nNodeRef==0 || pRtree->bCorrupt );

and it needs to be replaced by #ifdef SQLITE_DEBUG
    assert( pRtree->nNodeRef==0 ||  pRtree->bCorrupt );
#else
    assert( pRtree->nNodeRef==0);
#endif

Alternatively it may be better to declare and if appropriate set bCorrupt 
unconditionally, since this avoids a possible assertion error on releasing the 
Rtree if it is in fact corrupted and nNodeRef is nonzero.
Richard Parkins
http://www.zen224037.zen.co.uk
https://github.com/rparkins999/sqliteman
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] How to use ORDER BY on FTS5 table ?

2019-04-07 Thread Nik Jain
 Have a fts5 table with 2 indexed columns. Where the idea is to match by
one col and sort using the other one. Something like :

"select id from fts where col1 match '50' order by price "

This is slow. 0.07 seconds. Removing the order by clause -  0.001 seconds.
How do I fix this ? I have a feeling I am using this fts table in an
incorrect way. One way is to run 2 queries. First on the fts table, to
return ids. Second on the regular table with the order by clause. " select
* from normaltable where  id in (Ids) order by price " . This approach is
fast. But the id list could be large sometimes.
Any other way ?
Thanks

PS: This is my second attempt at mailing lists. Not sure if this one will
go through.
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users