I had a similar problem, importing a lot of data into a database (import
very infrequently but read a lot) and then accessing it. With about 6million
rows it was taking 12 hours to get halfway through importing so I gave up.

These are the things that massively helped me:
* Increased default_page_cache to 60000 (people have a lot of RAM now on
non-embedded systems so why not use it)
* Increased page_size to 32768 (maximum amount)
* Set synchronous = OFF (big difference, data integrity was not important to
us)
* Set temp_store = MEMORY (not sure of the usefulness of this, but used it
anyway)
* Set auto_vacuum = 0 (not deleting anything so probably not useful, set it
anyway)
* Using prepared statements cut import down by more than 50%

I also found if I was inserting into a multi-indexed table with "on conflict
ignore" it was actually a lot quicker to search for the record and only
insert if it didn't exist, than to rely on insert/ignore failure.

Hope this helps.




-----Original Message-----
From: Christian Smith [mailto:[EMAIL PROTECTED] 
Sent: 30 March 2006 15:54
To: sqlite-users@sqlite.org
Cc: Subhash Mangipudi; Herc Silverstein
Subject: Re: [sqlite] improving query performance

On Wed, 29 Mar 2006, Andy Spencer wrote:

>I have a sqlite database with about 3 GB of data, most of which is stored
>in a data table with about 75 million records, having three columns
>(EntryId INTEGER, PropertyId INTEGER, Value NUMERIC) and
>PRIMARY KEY(EntryId, PropertyId).
>
>This table is not indexed, to allow faster updates.


It is indexed. The primary key clause creates an implied index on
(EntryId,PropertyId).


>
>The problem is that it takes over an hour to access all Values, for a
>specified PropertyId, when the value is obtained for each EntryId
>separately (using "SELECT Value FROM Data WHERE PropertyId=? AND
>EntryId=?", bound to the specified PropertyId and EntryId) and
>the EntryId values for successive database queries are in essentially
>random order (taken from an external list of entries that has been
>sorted by property values).
>
>This same query (getting the property value for each EntryId,
>separately) only takes about 7 minutes when the EntryId values for
>successive database queries are in the same ascending order as
>the data orginally inserted into the table.


Yes. You're accessing the database in about as inefficient way as is
possible with your data, resulting in much thrashing of caches. Under
UNIX, if you're thrashing the OS cache, you can monitor this using vmstat.


>
>I assume that this has to do with better pager caching of successive
>records in the database, whereas random access may re-read the same
>page multiple times (due to the limited cache).


If you're not thrashing the OS cache (do you have lots of RAM?) try
increasing the size of your SQLite cache. Use:

PRAGMA cache_size=20000;

This will make your cache 10x bigger, and may increase hit rate.


>
>My question is whether it should be faster to
>
>A) create an index for the table before the query,
>   query the value (for the specified PropertyId) for each EntryId
>   (in essentially random order, from external list of entries),
>   and delete the index after the queries (for each EntryId) are done


Won't help. You already have an index from the primary key.


>
>or
>
>B) issue a single "SELECT EntryId, Value FROM Data WHERE PropertyId=?"
query
>   (bound to the specified PropertyId) and step through the results,
>   using something like a hash table lookup to map the EntryId values
>   (returned from the query) back to an index into the external list of
>   entries.


This may help, as you'll not be using the primary key index, and thus the
index pages will not be competing with the table pages for memory.


>
>The values extracted from the database are to be copied into an entry
>property data structure, having the same order as the external list of
>entries.
>

If you must group the values by PropertyId rather than EntryId, then
insert them into the database in that order. Is that possible?

That, or increase the amount of RAM you have.

Christian


-- 
    /"\
    \ /    ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL
     X                           - AGAINST MS ATTACHMENTS
    / \


Reply via email to